From 307ec165d11cc8c34c9f4c467e436c513142cfb8 Mon Sep 17 00:00:00 2001 From: Espen Antonsen Date: Tue, 11 Aug 2009 01:23:30 +0200 Subject: [PATCH] new scan methods --- app/models/album.rb | 3 +- app/models/photo.rb | 2 +- app/models/tag.rb | 8 +-- app/views/tags/index.html.erb | 2 +- lib/scan.rb | 113 +++++++++++++++++++++++++++------- 5 files changed, 98 insertions(+), 30 deletions(-) diff --git a/app/models/album.rb b/app/models/album.rb index 8db5778..e19cb9f 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -11,7 +11,7 @@ class Album < ActiveRecord::Base after_destroy :destroy_folders 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 :unused, :conditions => "albums.id NOT IN (SELECT album_id FROM collection_albums)" @@ -19,7 +19,6 @@ class Album < ActiveRecord::Base def to_param "#{id}-#{title.parameterize}" - #self.title.gsub(/[^a-z0-9]+/i, '-') end diff --git a/app/models/photo.rb b/app/models/photo.rb index 04bf578..ae11f5c 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -35,7 +35,7 @@ class Photo < ActiveRecord::Base end def to_param - self.id.to_s + '-' + self.title.parameterize + "#{id}-#{title.parameterize}" end def path_original_public diff --git a/app/models/tag.rb b/app/models/tag.rb index 0231fbe..cc9e435 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1,5 +1,5 @@ class Tag < ActiveRecord::Base - has_many :photo_tags + has_many :photo_tags, :dependent => :destroy has_many :photos, :through => :photo_tags validates_uniqueness_of :title @@ -11,10 +11,8 @@ class Tag < ActiveRecord::Base end def to_param - #id.to_s+'-'+ - self.title.parameterize - #title.downcase.gsub(/[^a-z0-9]+/i, '-') - end + "#{id}-#{title.parameterize}" + end protected diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index 24f04a1..0f94fbb 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -1,4 +1,4 @@

Tags

<% for tag in @tags %> -<%= link_to tag.title, tag_photos_path(tag) %>  +<%= link_to t(tag.title), tag_photos_path(tag) %>  <% end %> \ No newline at end of file diff --git a/lib/scan.rb b/lib/scan.rb index b8a3eb3..923b901 100644 --- a/lib/scan.rb +++ b/lib/scan.rb @@ -2,9 +2,58 @@ module ScanFiles require "find" 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] prevalbum = "" dirs = Array.new @@ -12,44 +61,65 @@ module ScanFiles dirs.push( 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], '') 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? - 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 ) album = Album.new() album.path = relpath - unless album.save + 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 " + relfile + puts " New photo added " + photorelpathparam photo = Photo.new( ) photo.album = album - photo.path = relfile - unless photo.save + photo.path = photorelpathparam + unless debug || photo.save raise "unable to save photo" end else @@ -57,6 +127,7 @@ module ScanFiles end end } + return end def self.RecreateThumbnails