class Roopap::Organizer

Public Class Methods

new() click to toggle source
# File lib/roopap/organizer.rb, line 8
def initialize
  @count = 0
end

Public Instance Methods

organized_file_count() click to toggle source
# File lib/roopap/organizer.rb, line 22
def organized_file_count
  @count
end
perform(source, dest) click to toggle source
# File lib/roopap/organizer.rb, line 12
def perform(source, dest)
  Pathname.new(source).each_child do |c|
    src_file_path =  c.to_s
    next unless jpg_file?(src_file_path)
    target_path = dest_for(src_file_path, dest)
    copy(src_file_path, target_path)
    inc_success_number
  end
end

Private Instance Methods

copy(filepath, dest_path) click to toggle source
# File lib/roopap/organizer.rb, line 61
def copy(filepath, dest_path)
  FileUtils.cp(filepath, dest_path)
end
dest_for(filepath, root) click to toggle source
# File lib/roopap/organizer.rb, line 36
def dest_for(filepath, root)
  photo = EXIFR::JPEG.new(filepath)
  time = get_time(photo)
  dir_name = time.utc.strftime('%Y-%m')
  dir_path = File.join(root, dir_name)
  prepare_dir(dir_path)
  filename = gen_filename(filepath)
  File.join(dir_path, filename)
end
gen_filename(filepath) click to toggle source
# File lib/roopap/organizer.rb, line 53
def gen_filename(filepath)
  "#{Digest::MD5.hexdigest(File.read(filepath))[0..9]}.jpg"
end
get_time(photo) click to toggle source
# File lib/roopap/organizer.rb, line 46
def get_time(photo)
  time = photo.date_time_original
  time = photo.date_time if (time.nil? || time == '')
  time = Time.parse(time) if time.instance_of?(String)
  time
end
inc_success_number() click to toggle source
# File lib/roopap/organizer.rb, line 65
def inc_success_number
  @count = @count + 1
end
jpg_file?(path) click to toggle source
# File lib/roopap/organizer.rb, line 27
def jpg_file?(path)
  begin
    EXIFR::JPEG.new(path)
    true
  rescue EXIFR::MalformedJPEG
    false
  end
end
prepare_dir(path) click to toggle source
# File lib/roopap/organizer.rb, line 57
def prepare_dir(path)
  FileUtils.mkdir_p(path)
end