Guest User

Untitled

a guest
Apr 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. require 'digest/md5'
  2.  
  3. module AudioLib
  4.  
  5. # Currently only checks for mp3 and m4a files
  6. def self.is_audio_file?(file_name)
  7. File.file?(file_name) && File.basename(file_name) =~ AudioLib.file_format[:regexp]
  8. end
  9.  
  10. def self.file_format
  11. {
  12. :regexp => /^[-a-z0-9 ]+\.(mp3|m4a)$/i,
  13. :glob => "*.m[p4][34a]"
  14. }
  15. end
  16.  
  17. class AudioFile
  18. attr_accessor :full_path, :name, :hash_name
  19.  
  20. def self.find(options={})
  21. # Determine the group or path to search
  22. if options.include? :group
  23. path = options[:group].full_path
  24. elsif options.include? :path
  25. path = options[:path]
  26. else
  27. raise ArgumentError, "You must specify the :group or :path to search."
  28. end
  29.  
  30. # Determine the name glob to use
  31. if options.include? :name
  32. name_glob = options[:name]
  33. else
  34. name_glob = AudioLib.file_format[:glob]
  35. end
  36.  
  37. if options.include? :recursive
  38. path = File.join(path, "**")
  39. end
  40.  
  41. file_glob = File.join(path, name_glob)
  42. print "file glob #{file_glob}"
  43.  
  44. # Walk the file tree
  45. files = Dir[file_glob].map do |file|
  46. unless file.nil? || !AudioLib.is_audio_file?(file)
  47. AudioLib::AudioFile.new(file)
  48. end
  49. end
  50. files.uniq!
  51. files.delete nil
  52.  
  53. if options.include? :name
  54. unless files.nil? || files.empty?
  55. files[0]
  56. else
  57. nil
  58. end
  59. else
  60. files
  61. end
  62.  
  63. end
  64.  
  65. # Initializes a new file based on a full_path name.
  66. # The file folder must be a valid file and match a specific named format
  67. def initialize(file_path)
  68. raise ArgumentError, "File name or path is invalid" unless AudioLib.is_audio_file?(file_path)
  69.  
  70. @full_path = File.expand_path file_path
  71. @name = File.basename file_path
  72. end
  73.  
  74. def size
  75. File.size @full_path
  76. end
  77.  
  78. end
  79.  
  80. end
Add Comment
Please, Sign In to add comment