new scan methods

This commit is contained in:
Espen Antonsen 2009-08-11 01:23:30 +02:00
parent 27bd06c0c8
commit 307ec165d1
5 changed files with 98 additions and 30 deletions

View file

@ -11,7 +11,7 @@ class Album < ActiveRecord::Base
after_destroy :destroy_folders after_destroy :destroy_folders
attr_accessor :tags attr_accessor :tags
attr_protected :path #attr_protected :path
named_scope :untouched, :conditions => "albums.id IN ( SELECT DISTINCT photos.album_id FROM photos WHERE photos.description IS NULL AND photos.id NOT IN ( SELECT photo_id FROM photo_tags) )", :order => 'title' named_scope :untouched, :conditions => "albums.id IN ( SELECT DISTINCT photos.album_id FROM photos WHERE photos.description IS NULL AND photos.id NOT IN ( SELECT photo_id FROM photo_tags) )", :order => 'title'
named_scope :unused, :conditions => "albums.id NOT IN (SELECT album_id FROM collection_albums)" named_scope :unused, :conditions => "albums.id NOT IN (SELECT album_id FROM collection_albums)"
@ -19,7 +19,6 @@ class Album < ActiveRecord::Base
def to_param def to_param
"#{id}-#{title.parameterize}" "#{id}-#{title.parameterize}"
#self.title.gsub(/[^a-z0-9]+/i, '-')
end end

View file

@ -35,7 +35,7 @@ class Photo < ActiveRecord::Base
end end
def to_param def to_param
self.id.to_s + '-' + self.title.parameterize "#{id}-#{title.parameterize}"
end end
def path_original_public def path_original_public

View file

@ -1,5 +1,5 @@
class Tag < ActiveRecord::Base class Tag < ActiveRecord::Base
has_many :photo_tags has_many :photo_tags, :dependent => :destroy
has_many :photos, :through => :photo_tags has_many :photos, :through => :photo_tags
validates_uniqueness_of :title validates_uniqueness_of :title
@ -11,10 +11,8 @@ class Tag < ActiveRecord::Base
end end
def to_param def to_param
#id.to_s+'-'+ "#{id}-#{title.parameterize}"
self.title.parameterize end
#title.downcase.gsub(/[^a-z0-9]+/i, '-')
end
protected protected

View file

@ -1,4 +1,4 @@
<h1>Tags</h1> <h1>Tags</h1>
<% for tag in @tags %> <% for tag in @tags %>
<%= link_to tag.title, tag_photos_path(tag) %>&nbsp; <%= link_to t(tag.title), tag_photos_path(tag) %>&nbsp;
<% end %> <% end %>

View file

@ -2,9 +2,58 @@ module ScanFiles
require "find" require "find"
require "fileutils" require "fileutils"
supported_files = ["jpeg", "jpg", "gif", "png"] #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 ))
album = Album.find_by_path( pathentry ) || 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
end
album.path = pathentry
album.save! unless debug
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
photo = Photo.find_by_path( path ) || Photo.new()
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
end
photo.path = pathentry
photo.save! unless debug
end
def self.FullScan def self.FullScan(debug = false)
if debug
puts "DEBUG"
end
puts "Scanning " + APP_CONFIG[:photos_path] puts "Scanning " + APP_CONFIG[:photos_path]
prevalbum = "" prevalbum = ""
dirs = Array.new dirs = Array.new
@ -12,44 +61,65 @@ module ScanFiles
dirs.push( path ) dirs.push( path )
} }
dirs.sort.each{|path| dirs.sort.each{|path|
if File.file?(path) && [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path) ) if File.file?(path) && [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path).downcase )
relpath = File.dirname( path ).sub(APP_CONFIG[:photos_path], '') relpath = File.dirname( path ).sub(APP_CONFIG[:photos_path], '')
relfile = path.sub(APP_CONFIG[:photos_path], '') relfile = path.sub(APP_CONFIG[:photos_path], '')
puts relpath puts relpath
album = Album.find_by_path( 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 if prevalbum != relpath
puts relpath puts relpath
prevalbum = relpath prevalbum = relpath
end end
if album.nil? if album.nil?
relpathdirs = relpath.split("/")
relpathparam = ""
relpathdirs.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
puts "reload!"
self.FullScan
return
end
puts "New album : " + File.basename( relpath ) puts "New album : " + File.basename( relpath )
album = Album.new() album = Album.new()
album.path = relpath album.path = relpath
unless album.save unless debug || album.save
raise "unable to save album" raise "unable to save album"
end end
puts "reload!"
self.FullScan unless debug
return unless debug
end end
photo = Photo.find_by_path( relfile ) 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? if photo.nil?
puts " New photo added " + relfile puts " New photo added " + photorelpathparam
photo = Photo.new( ) photo = Photo.new( )
photo.album = album photo.album = album
photo.path = relfile photo.path = photorelpathparam
unless photo.save unless debug || photo.save
raise "unable to save photo" raise "unable to save photo"
end end
else else
@ -57,6 +127,7 @@ module ScanFiles
end end
end end
} }
return
end end
def self.RecreateThumbnails def self.RecreateThumbnails