-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
54 lines (43 loc) · 1.26 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'rubygems'
require 'fileutils'
require 'exifr'
IMAGE_TYPES = %w(jpg jpeg)
namespace :stuffen do
task :setup, [:from, :to] do |task, args|
$SOURCE_DIR = args[:from]
$DESTINATION_DIR = args[:to]
FileUtils.cd $SOURCE_DIR
end
desc "Copy photos to tree structure organized by year"
task :copy, [:from, :to] => :setup do |task|
non_year_directories.each do |dir|
begin
puts "Copying: #{dir}"
source = File.join(dir, ".")
destination = FileUtils.mkdir_p(destination_path(dir))
FileUtils.cp_r(source, destination)
rescue Exception => e
puts " -- #{e.message}"
end
end
end
def non_year_directories
Dir.glob("**").reject{ |dir| dir =~ /^\d{4}$/ }
end
def folder_image_date(dir)
images = File.join(dir, "/**/*.{#{IMAGE_TYPES.join(',')}}")
files = Dir.glob(images, File::FNM_CASEFOLD)
jpg_image_date(files.first)
end
def jpg_image_date(file)
begin
EXIFR::JPEG.new(file).date_time
rescue Exception => e
raise "Unable to find valid photo creation date"
end
end
def destination_path(dir)
taken_on = folder_image_date(dir)
File.join($DESTINATION_DIR, taken_on.year.to_s, "#{taken_on.strftime('%Y-%m-%d')} - #{dir}")
end
end