Module: PhotoFolder

Defined in:
lib/photo_folder.rb

Class Method Summary collapse

Class Method Details

.all_paths_in_databaseObject



108
109
110
111
# File 'lib/photo_folder.rb', line 108

def self.all_paths_in_database
  return @all_paths_in_database if !@all_paths_in_database.nil?
  @all_paths_in_database = Photo.find(:all).collect(&:path)
end

.all_paths_in_filesystemObject



113
114
115
116
# File 'lib/photo_folder.rb', line 113

def self.all_paths_in_filesystem
  return @all_paths_in_filesystem if !@all_paths_in_filesystem.nil?
  @all_paths_in_filesystem = PhotoFolder::photos_list.collect{|file| file.gsub(PhotoFolder.source_directory_location + '/','')}
end

.create_schemaObject



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
# File 'lib/photo_folder.rb', line 44

def self.create_schema
  schema_created = false
  if !Photo.table_exists?
    ActiveRecord::Base.connection.create_table("photos") do |t|
      t.integer  "photo_collection_id"
      t.integer  "position"
      t.integer  "height"
      t.integer  "width"
      t.string   "guid"
      t.string   "path"
      t.string   "name"
      t.integer "date"
      t.string   "location"
      t.string   "f_stop"
      t.string   "shutter_speed"
      t.string   "focal_length"
      t.string   "iso"
      t.string   "camera"
      t.string   "lens"
      t.text     "caption"
      t.text     "meta"
      t.datetime "last_modified"
      t.integer  "nsfw"
      t.integer  "featured"
    end
    schema_created = true
  end
  
  if !PhotoCollection.table_exists?
    ActiveRecord::Base.connection.create_table("photo_collections") do |t|
      t.string  "name"
      t.string  "path"
      t.integer "parent_id"
      t.integer "position"
      t.integer "oldest_date"
      t.integer "newest_date"
      t.text "meta"
    end
    schema_created = true
  end
  
  if !PhotoTag.table_exists?
    ActiveRecord::Base.connection.create_table("photo_tags") do |t|
      t.string "tag"
    end
    schema_created = true
  end
  
  if !PhotoTagging.table_exists?
    ActiveRecord::Base.connection.create_table("photo_taggings") do |t|
      t.integer "photo_tag_id"
      t.integer "photo_id"
    end
    schema_created = true
  end
  
  schema_created
end

.establish_database_connection(database_location) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/photo_folder.rb', line 36

def self.establish_database_connection(database_location)
  ActiveRecord::Base.establish_connection(
    :adapter  => "sqlite3",
    :database => database_location,
    :timeout => 5000
  )
end

.flush_cached_path_informationObject



118
119
120
121
# File 'lib/photo_folder.rb', line 118

def self.flush_cached_path_information
  @all_paths_in_filesystem = nil
  @all_paths_in_database = nil
end

.generate(source_directory_location, database_location, json_location, include_nsfw = false, exif_tool_location = 'exiftool') ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/photo_folder.rb', line 223

def self.generate(source_directory_location,database_location,json_location,include_nsfw = false,exif_tool_location = 'exiftool')
  Photo.send(:with_scope, :find => {:conditions => include_nsfw ? "nsfw = 1 OR nsfw = 0" : "nsfw = 0"}) do
    puts "PhotoFolder Database Generator"
    puts "=============================="
    
    @source_directory_location = source_directory_location
    
    begin
      if exif_tool_location
        MiniExiftool.configure(exif_tool_location)
        @mini_exiftool_available = true
      else
        @mini_exiftool_available = false
      end
    rescue
      @mini_exiftool_available = false
      puts "WARNING: Could not configure MiniExifTool, EXIF data will not be parsed."
    end
    
    PhotoFolder::establish_database_connection(database_location)
    if PhotoFolder::create_schema
      puts "Generated schema in #{database_location}"
    end
    
    photos_list = PhotoFolder::photos_list
    puts "Scanning #{photos_list.length} photos in #{source_directory_location}"
    
    photos_to_add, photos_to_update, photos_to_remove = PhotoFolder::scan_photos
    
    Photo.transaction do
      if photos_to_add.length > 0
        photos_to_add.each do |path|
          puts "Adding photo: #{path}"
          begin
            photo = Photo.create :path => path
            if photo.set_meta
              puts "Setting meta data for #{path} from #{photo.meta_file_path}"
            end
          rescue
            puts "Unable to add photo: #{path}"
          end
        end
        puts "Putting added photos in collections."
        PhotoCollection.find(:all).each do |photo_collection|
          photo_collection.set_children_parent_id
        end
      else
        puts "No photos to add."
      end
    
      if photos_to_update.length > 0
        photos_to_update.each do |path|
          puts "Updating photo: #{path}"
          photo = Photo.find_by_path(path)
          if photo
            photo.update_info
            if photo.set_meta
              puts "Setting meta data for #{path} from #{photo.meta_file_path}"
            end
          end
        end
      else
        puts "No photos to update."
      end
    
      if photos_to_remove.length > 0
        photos_to_remove.each do |path|
          puts "Removing photo: #{path}"
          Photo.find_all_by_path(path).each(&:destroy)
        end
      else
        puts "No photos to remove."
      end
    end
    
    PhotoFolder::flush_cached_path_information
    collections_to_add, collections_to_remove = PhotoFolder::scan_photo_collections
    
    PhotoCollection.transaction do
      if collections_to_add.length > 0
        collections_to_add.sort_by{|path| path.split('/').pop}.each do |path|
          puts "Adding collection: #{path}"
          PhotoCollection.create :path => path
        end
      else
        puts "No photo collections to add."
      end
    
      if collections_to_remove.length > 0
        collections_to_remove.each do |path|
          puts "Removing collection: #{path}"
          PhotoCollection.find_by_path(path).destroy if PhotoCollection.find_by_path(path)
        end
      else
        puts "No photo collections to remove."
      end
    
      puts "Ensuring correct tree structure of photo collections."
      PhotoCollection.find(:all).each(&:set_parent_id)
      
      puts "Ensuring correct position of photos and photo collections."
      if PhotoCollection.positions
        puts "Setting positions for root collections from #{PhotoCollection.positions_file_path}"
      end
      if PhotoCollection.add_title_card
        puts "Adding title card to root from #{PhotoCollection.title_card_path}"
      end
      PhotoCollection.set_correct_positions(PhotoCollection.find_all_by_parent_id(0))
      puts "Setting oldest and newest dates for collections."
      PhotoCollection.find(:all).each do |photo_collection|
        photo_collection.set_oldest_date
        photo_collection.set_newest_date
        if photo_collection.positions
          puts "Setting positions for #{photo_collection.name} from #{photo_collection.positions_file_path}"
          photo_collection.set_correct_positions 
        end
        if photo_collection.set_meta
          puts "Setting meta data for #{photo_collection.name} from #{photo_collection.meta_file_path}"
        end
        if photo_collection.add_title_card
          puts "Adding title card to #{photo_collection.name} from #{photo_collection.title_card_path}"
        end
      end
    end
    PhotoCollection.
    puts "Cascading meta data."
    puts "Writing database to #{json_location}"
    PhotoFolder::write_database_to_file(json_location)
  end
end

.hash_from_databaseObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/photo_folder.rb', line 189

def self.hash_from_database
  {
    :photo_collections => PhotoCollection,
    :photos => Photo,
    :photo_tags => PhotoTag,
    :photo_taggings => PhotoTagging
  }.inject(Hash.new) do |result,key_name_and_model|
    key_name = key_name_and_model[0]
    model = key_name_and_model[1]
    result[key_name] = model.find(:all).inject(Hash.new) do |inner_result,item|
      inner_result[item.id.to_s] = item.attributes.inject(Hash.new) do |attribute_result,key_and_value|
        key = key_and_value[0]
        value = key_and_value[1]
        attribute_result[key.to_s] = value.is_a?(Numeric) ? value : value.to_s
        attribute_result
      end
      inner_result
    end
    result
  end
end

.mini_exiftool_available?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/photo_folder.rb', line 219

def self.mini_exiftool_available?
  @mini_exiftool_available
end

.photos_listObject



103
104
105
106
# File 'lib/photo_folder.rb', line 103

def self.photos_list
  return @photos_list if !@photos_list.nil?
  @photos_list = Dir[PhotoFolder.source_directory_location + '/**/*.{png,jpg,markdown}']
end

.scan_photo_collectionsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/photo_folder.rb', line 158

def self.scan_photo_collections
  collection_paths_in_database = PhotoCollection.find(:all).collect(&:path)
  
  collection_paths_in_filesystem = []
  PhotoFolder::all_paths_in_database.each do |path|
    collection = path.gsub(/\/[^\/]+$/,'')
    collection_bits = collection.split('/')
    collection_bits.each_with_index do |part,i|
      joined = collection_bits[0,i].join('/')
      collection_paths_in_filesystem.push(joined) if !collection_paths_in_filesystem.include?(joined) && joined != ''
    end
    collection_paths_in_filesystem.push(collection) if !collection_paths_in_filesystem.include?(collection)
  end
  
  to_add = []
  to_remove = []
  
  collection_paths_in_filesystem.each do |path_in_filesystem|
    to_add.push(path_in_filesystem) if !collection_paths_in_database.include?(path_in_filesystem)
  end
  
  collection_paths_in_database.each do |path_in_database|
    to_remove.push(path_in_database) if !collection_paths_in_filesystem.include?(path_in_database)
  end
  
  #ensure root photo is not marked as a collection
  to_add.reject!{|item| item == 'index.markdown'}
  
  [to_add,to_remove]
end

.scan_photosObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/photo_folder.rb', line 123

def self.scan_photos
  to_add = []
  to_update = []
  to_remove = []
  
  PhotoFolder::all_paths_in_filesystem.each do |path_in_filesystem|
    to_add.push(path_in_filesystem) if !PhotoFolder::all_paths_in_database.select{|path_in_database| path_in_database == path_in_filesystem}[0]
  end
  
  PhotoFolder::all_paths_in_database.each do |path_in_database|
    to_remove.push(path_in_database) if !PhotoFolder::all_paths_in_filesystem.select{|path_in_filesystem| path_in_filesystem == path_in_database}[0]
  end
  
  PhotoFolder::all_paths_in_filesystem.each do |path_in_filesystem|
    if !to_add.include?(path_in_filesystem) && !to_remove.include?(path_in_filesystem)
      entry = Photo.find_by_path(path_in_filesystem)
      if entry && File.mtime(entry.full_path) > entry.last_modified || File.exists?(entry.meta_file_path)
        to_update.push(path_in_filesystem)
      end
    elsif to_add.include?(path_in_filesystem)
      entry = Photo.find_by_path(path_in_filesystem)
      if entry
        to_update.push(path_in_filesystem)
      end
    end
  end
  
  to_add.reject!{|item| to_update.include?(item)}
  
  to_add.reject!{|item| item.match(/index\.markdown$/)}
  to_update.reject!{|item| item.match(/index\.markdown$/)}
  
  [to_add,to_update,to_remove]
end

.source_directory_locationObject



215
216
217
# File 'lib/photo_folder.rb', line 215

def self.source_directory_location
  @source_directory_location
end

.write_database_to_file(path) ⇒ Object



211
212
213
# File 'lib/photo_folder.rb', line 211

def self.write_database_to_file(path)
  File.open(path,'w'){|f| f.write(PhotoFolder::hash_from_database.to_json) }
end