Module: Heathen::Filename

Defined in:
lib/heathen/filename.rb

Class Method Summary collapse

Class Method Details

.suggest(filename, mime_type) ⇒ String

Suggests a new file name base on the old one and the mime_type provided


7
8
9
10
11
12
# File 'lib/heathen/filename.rb', line 7

def self.suggest filename, mime_type
  ext = File.extname(filename)
  temp_file = filename[0..-(ext.length+1)]
  ext = MIME::Types[mime_type].first.preferred_extension rescue 'txt' # FIXME use a saner extension
  "#{temp_file}.#{ext}"
end

.suggest_in_new_dir(filename, mime_type, base_dir, new_dir) ⇒ String

Suggests a new file name base on the old one and the mime_type provided The new file name will be positioned correctly in the new dir, so for example:

suggest_in_new_dir( '/home/joe/src/fred.pdf',
                    'text/plain',
                    '/home/joe',
                    '/home/fred/Projects' )

should return: '/home/fred/Projects/src/fred.txt'

26
27
28
29
# File 'lib/heathen/filename.rb', line 26

def self.suggest_in_new_dir filename, mime_type, base_dir, new_dir
  file = self.suggest filename, mime_type
  "#{new_dir}#{file[base_dir.length..-1]}"
end