photomix/lib/scan.rb

54 lines
1.6 KiB
Ruby
Raw Permalink Normal View History

2009-05-22 14:13:46 +02:00
module ScanFiles
require "find"
2009-08-04 13:39:28 +02:00
require "fileutils"
2009-05-22 14:13:46 +02:00
2009-08-11 01:23:30 +02:00
#require "scan";ScanFiles.Scan
@photos_path = File.expand_path( './public/' + ENV['STORAGE_PATH'] + '/files' ) + '/'
2009-08-11 01:23:30 +02:00
def self.Scan(debug = true)
puts " IN DEBUG MODE " if debug
self.ScanDirectory( @photos_path , debug )
2009-08-11 01:23:30 +02:00
end
def self.ScanDirectory(path, debug)
path = File.expand_path( path )
puts "analyze directory " + path
Dir.entries( path ).each {|entry|
pathentry = path + "/" + entry
if File.directory?(pathentry) && !([".", ".."].include?( entry ))
album = Album.find_or_initialize_by_path( pathentry.sub( @photos_path, '' ) )
if album.new_record?
puts "Save album " + pathentry.sub( @photos_path, '')
2009-08-11 01:39:27 +02:00
album.save! unless debug
2009-08-11 01:23:30 +02:00
end
self.ScanDirectory(pathentry, debug)
elsif File.file?(pathentry)
self.ScanFile(pathentry, debug)
else
puts "ignoring " + pathentry
end
}
end
def self.ScanFile(path, debug)
return unless [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path).downcase )
puts "analyze file " + path
2011-04-11 11:17:08 +02:00
photo = Photo.find_or_initialize_by_path( path )
2009-08-11 01:39:27 +02:00
puts "new record " + photo.new_record?.to_s
if photo.new_record?
puts "Save file " + path.sub(@photos_path, '')
2011-04-11 11:17:08 +02:00
photo.file = File.open( path ) unless debug
photo.album = Album.find_by_path( File.dirname( path ).sub(@photos_path, '') )
2009-08-11 01:39:27 +02:00
photo.save! unless debug
photo.file.recreate_versions! unless debug
2009-08-11 01:23:30 +02:00
end
end
2009-05-22 14:13:46 +02:00
def self.RecreateThumbnails
2012-07-24 23:26:16 +02:00
Photo.all.each {|photo|
photo.file.recreate_versions!
}
end
2009-05-22 14:13:46 +02:00
end