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
|
2011-04-11 09:55:38 +02:00
|
|
|
@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
|
2011-04-11 09:55:38 +02:00
|
|
|
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 ))
|
2011-04-11 09:55:38 +02:00
|
|
|
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
|
2011-04-11 09:55:38 +02:00
|
|
|
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
|
2011-04-11 09:55:38 +02:00
|
|
|
photo.album = Album.find_by_path( File.dirname( path ).sub(@photos_path, '') )
|
2009-08-11 01:39:27 +02:00
|
|
|
photo.save! unless debug
|
2011-04-11 09:55:38 +02:00
|
|
|
photo.file.recreate_versions! unless debug
|
2009-08-11 01:23:30 +02:00
|
|
|
end
|
|
|
|
end
|
2009-05-22 14:13:46 +02:00
|
|
|
|
2009-06-12 01:04:57 +02:00
|
|
|
def self.RecreateThumbnails
|
|
|
|
Photo.find(:all).each {|photo|
|
2011-04-11 09:55:38 +02:00
|
|
|
photo.file.recreate_versions!
|
2009-06-12 01:04:57 +02:00
|
|
|
}
|
|
|
|
end
|
2009-05-22 14:13:46 +02:00
|
|
|
|
|
|
|
end
|