photomix/lib/scan.rb

173 lines
6.5 KiB
Ruby
Raw 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
def self.Scan(debug = true)
puts " IN DEBUG MODE " if debug
self.ScanDirectory( APP_CONFIG[:photos_path], debug )
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 ))
2009-08-11 01:39:27 +02:00
album = Album.find_by_path( pathentry.sub(APP_CONFIG[:photos_path], '') ) || Album.new()
2009-08-11 01:23:30 +02:00
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
2009-08-11 01:39:27 +02:00
moved = true
end
if moved || album.new_record?
album.path = pathentry.sub(APP_CONFIG[:photos_path], '')
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
pathentry = path
2009-08-11 01:39:27 +02:00
photo = Photo.find_by_path( path.sub(APP_CONFIG[:photos_path], '') ) || Photo.new()
puts "new record " + photo.new_record?.to_s
2009-08-11 01:23:30 +02:00
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
2009-08-11 01:39:27 +02:00
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], '') )
photo.save! unless debug
2009-08-11 01:23:30 +02:00
end
end
2009-05-22 14:13:46 +02:00
2009-08-11 01:23:30 +02:00
def self.FullScan(debug = false)
2009-08-11 12:12:00 +02:00
puts "not in use anymore. use Scan"
return
2009-08-11 01:23:30 +02:00
if debug
puts "DEBUG"
end
2009-08-04 13:39:28 +02:00
puts "Scanning " + APP_CONFIG[:photos_path]
2009-06-02 00:08:57 +02:00
prevalbum = ""
2009-08-01 20:32:47 +02:00
dirs = Array.new
Find.find( APP_CONFIG[:photos_path] ) { |path|
dirs.push( path )
}
dirs.sort.each{|path|
2009-08-11 01:23:30 +02:00
if File.file?(path) && [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path).downcase )
2009-05-22 14:13:46 +02:00
relpath = File.dirname( path ).sub(APP_CONFIG[:photos_path], '')
relfile = path.sub(APP_CONFIG[:photos_path], '')
2009-08-04 13:39:28 +02:00
puts relpath
2009-08-10 22:17:58 +02:00
album = Album.find_by_path( relpath )
2009-08-11 01:23:30 +02:00
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
2009-06-02 00:08:57 +02:00
if prevalbum != relpath
puts relpath
prevalbum = relpath
end
2009-05-22 14:13:46 +02:00
if album.nil?
puts "New album : " + File.basename( relpath )
2009-08-01 20:12:34 +02:00
album = Album.new()
album.path = relpath
2009-08-11 01:23:30 +02:00
unless debug || album.save
2009-08-01 20:03:49 +02:00
raise "unable to save album"
end
2009-08-11 01:23:30 +02:00
puts "reload!"
self.FullScan unless debug
return unless debug
2009-05-22 14:13:46 +02:00
end
2009-06-02 00:08:57 +02:00
photo = Photo.find_by_path( relfile )
2009-08-11 01:23:30 +02:00
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
2009-06-02 00:08:57 +02:00
if photo.nil?
2009-08-11 01:23:30 +02:00
puts " New photo added " + photorelpathparam
2009-08-01 20:12:34 +02:00
photo = Photo.new( )
photo.album = album
2009-08-11 01:23:30 +02:00
photo.path = photorelpathparam
unless debug || photo.save
2009-08-01 20:03:49 +02:00
raise "unable to save photo"
end
2009-06-02 00:08:57 +02:00
else
puts " Found photo " + relfile
2009-05-22 14:13:46 +02:00
end
end
}
2009-08-11 01:23:30 +02:00
return
2009-05-22 14:13:46 +02:00
end
def self.RecreateThumbnails
Photo.find(:all).each {|photo|
photo.create_thumbnails()
}
end
2009-05-22 14:13:46 +02:00
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