Module: FileUtils

Included in:
BatchAudioConvert
Defined in:
lib/batch_audio_convert/file_utils.rb

Instance Method Summary collapse

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
# 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|
    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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/batch_audio_convert/file_utils.rb', line 56

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

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/batch_audio_convert/file_utils.rb', line 88

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

Returns:

  • (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



76
77
78
# File 'lib/batch_audio_convert/file_utils.rb', line 76

def replace_folder_in_destination!(file)
  file.gsub! /^(?<base>.*\/)(?<full_file>[^\/]+\/[^\/]+\/[^\/]+)$/, "#{config[:destination]}/\\k<full_file>"
end

#should_process_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/batch_audio_convert/file_utils.rb', line 71

def should_process_file?(file)
  return true if config[:force]
  return !File.exists?(file)
end

#verify_destination_folder(file) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/batch_audio_convert/file_utils.rb', line 80

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