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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/photostat/plugins/01_local.rb', line 20
def import
opts = Trollop::options do
opt :path, "Local path to import", :required => true, :type => :string, :short => "-p"
opt :tags, "List of tags to classify imported pictures", :type => :strings
opt :visibility, "Choices are 'private', 'protected' and 'public'", :required => true, :type => :string
opt :move, "Move, instead of copy (better performance, defaults to false, careful)", :type => :boolean
opt :dry, "Just fake it and print the resulting files", :type => :boolean
end
Trollop::die :path, "must be a valid directory" unless File.directory? opts[:path]
Trollop::die :visibility, "is invalid. Choices are: private, protected and public" unless ['private', 'protected', 'public'].member? opts[:visibility]
opts[:tags] ||= []
activate!
source = File.expand_path opts[:path]
config = Photostat.config
files = files_in_dir(source, :match => /(.jpe?g|.mov)$/i, :absolute? => true)
count, total = 0, files.length
puts
interrupted = false
trap("INT") { interrupted = true }
files.each do |fpath|
break if interrupted
count += 1
STDOUT.print "\r - processed: #{count} / #{total}"
STDOUT.flush
if fpath =~ /.jpe?g/i
type = 'jpg'
exif = EXIFR::JPEG.new fpath
dt = (exif.date_time || File.mtime(fpath)).getgm
else
type = 'mov'
dt = File.mtime(fpath).getgm
end
md5 = partial_file_md5 fpath
uid = dt.strftime("%Y%m%d%H%M%S") + "-" + md5[0,6] + "." + type
local_dir = type == 'jpg' ? dt.strftime("%Y-%m") : 'movies'
local_path = File.join(local_dir, uid)
dest_dir = File.join(config[:repository_path], local_dir)
dest_path = File.join(config[:repository_path], local_path)
photo = @db[:photos].where(:uid => uid).first
photo_id = photo ? photo[:id] : nil
unless photo || opts[:dry]
photo_id = @db[:photos].insert(
:uid => uid,
:type => type,
:local_path => local_path,
:visibility => opts[:visibility],
:created_at => dt,
)
end
opts[:tags].each do |name|
next if opts[:dry]
next unless @db[:tags].where(:name => name, :photo_id => photo_id).empty?
@db[:tags].insert(:name => name, :photo_id => photo_id)
end
next if File.exists? dest_path
next if File.expand_path(dest_path) == File.expand_path(fpath)
unless opts[:dry]
mkdir_p dest_dir
cp fpath, dest_path unless opts[:move]
mv fpath, dest_path if opts[:move]
end
end
if !files or files.length == 0
puts " - nothing to do"
end
if interrupted
puts
puts " - interrupted by user"
puts
exit 0
end
puts
end
|