removed legacy code in scanning. and also revisted and fixed

master
Espen Antonsen 2011-04-11 15:55:38 +08:00
parent 7d88a531f0
commit 76d7ecb079
1 changed files with 12 additions and 131 deletions

View File

@ -3,10 +3,11 @@ module ScanFiles
require "fileutils"
#require "scan";ScanFiles.Scan
@photos_path = File.expand_path( './public/' + ENV['STORAGE_PATH'] + '/files' ) + '/'
def self.Scan(debug = true)
puts " IN DEBUG MODE " if debug
self.ScanDirectory( APP_CONFIG[:photos_path], debug )
self.ScanDirectory( @photos_path , debug )
end
@ -16,16 +17,9 @@ module ScanFiles
Dir.entries( path ).each {|entry|
pathentry = path + "/" + entry
if File.directory?(pathentry) && !([".", ".."].include?( entry ))
album = Album.find_by_path( pathentry.sub(APP_CONFIG[:photos_path], '') ) || Album.new()
unless entry == entry.parameterize
puts pathentry + " will now be moved to " + path + "/" + entry.parameterize
#FileUtils.mv( pathentry, entry.parameterize)
File.rename( pathentry, path + "/" + entry.parameterize ) unless debug
pathentry = path + "/" + entry.parameterize
moved = true
end
if moved || album.new_record?
album.path = pathentry.sub(APP_CONFIG[:photos_path], '')
album = Album.find_or_initialize_by_path( pathentry.sub( @photos_path, '' ) )
if album.new_record?
puts "Save album " + pathentry.sub( @photos_path, '')
album.save! unless debug
end
self.ScanDirectory(pathentry, debug)
@ -40,134 +34,21 @@ module ScanFiles
def self.ScanFile(path, debug)
return unless [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path).downcase )
puts "analyze file " + path
pathentry = path
photo = Photo.find_by_path( path.sub(APP_CONFIG[:photos_path], '') ) || Photo.new()
photo = Photo.find_or_initialize_by_file( path )
puts "new record " + photo.new_record?.to_s
unless File.basename( path, File.extname(path) ) == File.basename(path, File.extname(path)).parameterize
pathentry = File.dirname(path) + "/" + File.basename( path, File.extname(path) ).parameterize + File.extname(path).downcase
puts path + " will now be moved to " + pathentry
#FileUtils.mv( path, File.basename( path, File.extname(path) ).parameterize + File.extname(path).downcase )
#File.move( path, File.dirname(path) + "/" + File.basename( path, File.extname(path) ).parameterize + File.extname(path) )
File.rename( path, pathentry ) unless debug
moved = true
end
if photo.new_record? || moved
puts "save file"
photo.path = pathentry.sub(APP_CONFIG[:photos_path], '')
photo.album = Album.find_by_path( File.dirname( pathentry ).sub(APP_CONFIG[:photos_path], '') )
if photo.new_record?
puts "Save file " + path.sub(@photos_path, '')
photo.file = File.open( path )
photo.album = Album.find_by_path( File.dirname( path ).sub(@photos_path, '') )
photo.save! unless debug
photo.file.recreate_versions! unless debug
end
end
def self.FullScan(debug = false)
puts "not in use anymore. use Scan"
return
if debug
puts "DEBUG"
end
puts "Scanning " + APP_CONFIG[:photos_path]
prevalbum = ""
dirs = Array.new
Find.find( APP_CONFIG[:photos_path] ) { |path|
dirs.push( path )
}
dirs.sort.each{|path|
if File.file?(path) && [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path).downcase )
relpath = File.dirname( path ).sub(APP_CONFIG[:photos_path], '')
relfile = path.sub(APP_CONFIG[:photos_path], '')
puts relpath
album = Album.find_by_path( relpath )
relpathparam = ""
relpath.split("/").each{|d|
relpathparam += d.parameterize + "/"
}
relpathparam = relpathparam.slice(0..relpathparam.length-2)
if relpath != relpathparam
puts APP_CONFIG[:photos_path] + relpath + " will now be moved to " + APP_CONFIG[:photos_path] + relpathparam
FileUtils.mv(APP_CONFIG[:photos_path] + relpath, APP_CONFIG[:photos_path] + relpathparam) unless debug
FileUtils.mv(APP_CONFIG[:thumbs_path] + relpath, APP_CONFIG[:thumbs_path] + relpathparam) unless debug
puts "reload!"
self.FullScan unless debug
return unless debug
end
if prevalbum != relpath
puts relpath
prevalbum = relpath
end
if album.nil?
puts "New album : " + File.basename( relpath )
album = Album.new()
album.path = relpath
unless debug || album.save
raise "unable to save album"
end
puts "reload!"
self.FullScan unless debug
return unless debug
end
photo = Photo.find_by_path( relfile )
photorelpathparam = ""
relfile.split("/").each{|d|
photorelpathparam += d.parameterize + "/"
}
photorelpathparam = photorelpathparam.slice(0..photorelpathparam.length-2)
puts "check if photo " + relfile + " = " + photorelpathparam
if relfile != photorelpathparam
puts APP_CONFIG[:photos_path] + relfile + " will now be moved to " + APP_CONFIG[:photos_path] + photorelpathparam
unless photo.nil?
photo.path = photorelpathparam
photo.save! unless debug
end
FileUtils.mv(APP_CONFIG[:photos_path] + photo.path, APP_CONFIG[:photos_path] + photorelpathparam) unless debug
FileUtils.mv(APP_CONFIG[:thumbs_path] + photo.path, APP_CONFIG[:thumbs_path] + photorelpathparam) unless debug
end
if photo.nil?
puts " New photo added " + photorelpathparam
photo = Photo.new( )
photo.album = album
photo.path = photorelpathparam
unless debug || photo.save
raise "unable to save photo"
end
else
puts " Found photo " + relfile
end
end
}
return
end
def self.RecreateThumbnails
Photo.find(:all).each {|photo|
photo.create_thumbnails()
photo.file.recreate_versions!
}
end
def self.CreateThumbnail(photo,image,thumbname,width,height)
puts "Create thumb of " + photo.path
thumb = image.first.resize_to_fill( width, height)
thumb2 = image.first.change_geometry!("#{width}x#{ height }") { |cols, rows, img|
if cols < width.to_i || rows < height.to_i
puts "first if"
img.resize!(cols, rows)
bg = Magick::Image.new( width, height){self.background_color = "white"}
bg.composite(img, Magick::CenterGravity, Magick::OverCompositeOp)
else
puts "second if"
img.resize!(cols, rows)
end
}
puts "write... " + APP_CONFIG[:thumbs_path] + photo.album.path + "/" + photo.id.to_s + "_" + thumbname + File.extname( APP_CONFIG[:photos_path] + photo.path )
thumb.write(APP_CONFIG[:thumbs_path] + photo.album.path + "/" + photo.id.to_s + "_" + thumbname + File.extname( APP_CONFIG[:photos_path] + photo.path ) ) { self.quality = 100 }
#image.change_geometry!(MAINSITE_SIZE) { |cols, rows, img|
# img.resize!(cols, rows)
#}
#image.write(mainsite_file)
end
end