Class: Photostat::Local

Inherits:
Plugins::Base show all
Includes:
FileUtils, OSUtils
Defined in:
lib/photostat/plugins/01_local.rb

Instance Method Summary collapse

Methods included from OSUtils

#exec, #file_md5, #files_in_dir, #input, #partial_file_md5, #string_md5

Methods inherited from Plugins::Base

exposes, exposes_help, extended, #help, help_text, included, inherited, plugin_name, register_plugin

Instance Method Details

#activate!Object



12
13
14
15
16
17
18
# File 'lib/photostat/plugins/01_local.rb', line 12

def activate!
  unless @activated
    @db = Photostat::DB.instance
    Photostat::DB.migrate!
    @activated = true
  end
end

#configObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/photostat/plugins/01_local.rb', line 112

def config
  puts
  config_file = File.expand_path "~/.photostat"

  config = {}
  config = YAML::load(File.read config_file) if File.exists? config_file

  config[:repository_path] ||= "~/Photos"
  config[:repository_path] = input(
    "Wanted location for your Photostat repository", 
    :dir? => true, :default => config[:repository_path])
  config[:repository_path] = File.expand_path(config[:repository_path])

  puts
  unless File.directory? config[:repository_path]
    Dir.mkdir config[:repository_path]
    puts " >>>> repository #{config[:repository_path]} created"
  end

  File.open(config_file, 'w') do |fh|
    fh.write(YAML::dump(config))
    puts " >>>> generated ~/.photostat config"
  end  

  puts
end

#importObject



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