Module: FileUtils
- Included in:
- BatchAudioConvert
- Defined in:
- lib/batch_audio_convert/file_utils.rb
Instance Method Summary collapse
-
#analyze_file(file) ⇒ Object
Define destination name and transormation method to apply for a file.
- #copy(origin, destination) ⇒ Object
- #is_directory_to_process?(dir) ⇒ Boolean
- #is_image?(file) ⇒ Boolean
-
#populate_list_of_files_from_directory(file_list, entry) ⇒ Object
Adds all files in a directory structure.
-
#populate_list_of_files_from_file(file_list, entry) ⇒ Object
Adds one file + its associated images if any.
- #replace_folder_in_destination!(file) ⇒ Object
- #should_process_file?(file) ⇒ Boolean
- #verify_destination_folder(file) ⇒ Object
Instance Method Details
#analyze_file(file) ⇒ Object
Define destination name and transormation method to apply for a file.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/batch_audio_convert/file_utils.rb', line 42 def analyze_file(file) method_name = :copy destination_file = String.new(file) config[:extensions].each do |origin_ext, destination_ext| origin_ext = origin_ext.to_s destination_ext = destination_ext.to_s if file =~ /\.#{origin_ext}$/i destination_file.gsub! /\.#{origin_ext}$/i, ".#{destination_ext}" method_name = origin_ext + '_to_' + destination_ext break end end replace_folder_in_destination! destination_file return {:method_name => method_name, :destination_file => destination_file} end |
#copy(origin, destination) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/batch_audio_convert/file_utils.rb', line 58 def copy(origin, destination) puts_and_logs "Copying \"#{origin}\" to \"#{destination}\"." return if config[:simulate] unless should_process_file? destination puts_and_logs ' - File exists. Skipping copy...' return end verify_destination_folder(destination) begin FileUtils.copy_file origin, destination rescue Exception => e logger.error "An error occurred during the copy - #{e.message}" end end |
#is_directory_to_process?(dir) ⇒ Boolean
90 91 92 93 94 95 96 97 98 |
# File 'lib/batch_audio_convert/file_utils.rb', line 90 def is_directory_to_process?(dir) config[:extensions].keys.each do |filetype| Dir.foreach dir do |filename| return true if filename =~ /\.#{filetype}$/i end end logger.warn "Directory \"#{dir}\" discarded, as not containing files to convert..." false end |
#is_image?(file) ⇒ Boolean
34 35 36 37 38 39 |
# File 'lib/batch_audio_convert/file_utils.rb', line 34 def is_image?(file) IMAGE_TYPE_FILES.each do |img_ext| return true if file =~ /\.#{img_ext}$/ end false end |
#populate_list_of_files_from_directory(file_list, entry) ⇒ Object
Adds all files in a directory structure
15 16 17 18 19 20 21 22 |
# File 'lib/batch_audio_convert/file_utils.rb', line 15 def populate_list_of_files_from_directory(file_list, entry) logger.debug "\"#{entry}\" is a directory. Processing..." Find.find(entry) do |file| next unless is_directory_to_process?(File.dirname file) file_list << file if File.file?(file) end end |
#populate_list_of_files_from_file(file_list, entry) ⇒ Object
Adds one file + its associated images if any
25 26 27 28 29 30 31 32 |
# File 'lib/batch_audio_convert/file_utils.rb', line 25 def populate_list_of_files_from_file(file_list, entry) logger.debug "\"#{entry}\" is a file. Processing..." file_list << entry # Find images if any Find.find(File.dirname(entry)) do |file| file_list << file if (File.file?(file) && is_image?(file)) end end |
#replace_folder_in_destination!(file) ⇒ Object
78 79 80 |
# File 'lib/batch_audio_convert/file_utils.rb', line 78 def replace_folder_in_destination!(file) file.gsub! /^(?<base>.*\/)(?<full_file>[^\/]+\/[^\/]+\/[^\/]+)$/, "#{config[:destination]}/\\k<full_file>" end |
#should_process_file?(file) ⇒ Boolean
73 74 75 76 |
# File 'lib/batch_audio_convert/file_utils.rb', line 73 def should_process_file?(file) return true if config[:force] return !File.exists?(file) end |
#verify_destination_folder(file) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/batch_audio_convert/file_utils.rb', line 82 def verify_destination_folder(file) dir = File.dirname file unless File.directory? dir puts_and_logs "Creating directory \"#{dir}\"..." FileUtils.mkdir_p dir end end |