Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/md5'
- module AudioLib
- # Currently only checks for mp3 and m4a files
- def self.is_audio_file?(file_name)
- File.file?(file_name) && File.basename(file_name) =~ AudioLib.file_format[:regexp]
- end
- def self.file_format
- {
- :regexp => /^[-a-z0-9 ]+\.(mp3|m4a)$/i,
- :glob => "*.m[p4][34a]"
- }
- end
- class AudioFile
- attr_accessor :full_path, :name, :hash_name
- def self.find(options={})
- # Determine the group or path to search
- if options.include? :group
- path = options[:group].full_path
- elsif options.include? :path
- path = options[:path]
- else
- raise ArgumentError, "You must specify the :group or :path to search."
- end
- # Determine the name glob to use
- if options.include? :name
- name_glob = options[:name]
- else
- name_glob = AudioLib.file_format[:glob]
- end
- if options.include? :recursive
- path = File.join(path, "**")
- end
- file_glob = File.join(path, name_glob)
- print "file glob #{file_glob}"
- # Walk the file tree
- files = Dir[file_glob].map do |file|
- unless file.nil? || !AudioLib.is_audio_file?(file)
- AudioLib::AudioFile.new(file)
- end
- end
- files.uniq!
- files.delete nil
- if options.include? :name
- unless files.nil? || files.empty?
- files[0]
- else
- nil
- end
- else
- files
- end
- end
- # Initializes a new file based on a full_path name.
- # The file folder must be a valid file and match a specific named format
- def initialize(file_path)
- raise ArgumentError, "File name or path is invalid" unless AudioLib.is_audio_file?(file_path)
- @full_path = File.expand_path file_path
- @name = File.basename file_path
- end
- def size
- File.size @full_path
- end
- end
- end
Add Comment
Please, Sign In to add comment