From d9607ea0c8f65d42b6194e4a4c7830a52f6ce737 Mon Sep 17 00:00:00 2001 From: Espen Antonsen Date: Tue, 4 Aug 2009 13:39:28 +0200 Subject: [PATCH] parameterize folder names --- app/models/album.rb | 1 + app/models/photo.rb | 2 +- config/initializers/load_settings.rb | 3 ++- lib/scan.rb | 17 ++++++++++++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/album.rb b/app/models/album.rb index 39da749..7c97946 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -24,6 +24,7 @@ class Album < ActiveRecord::Base def ensure_path self.path = self.title unless self.path + self.path.parameterize end def set_title diff --git a/app/models/photo.rb b/app/models/photo.rb index 4ca7f0c..04bf578 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -87,7 +87,7 @@ class Photo < ActiveRecord::Base def swf_uploaded_data=(data) data.content_type = MIME::Types.type_for(data.original_filename) self.title = data.original_filename - self.path = self.album.path + "/" + data.original_filename + self.path = self.album.path + "/" + data.original_filename.parameterize File.open(APP_CONFIG[:photos_path] + self.path, 'wb') { |f| f.write(data.read) } end diff --git a/config/initializers/load_settings.rb b/config/initializers/load_settings.rb index 58b75ff..cafed14 100644 --- a/config/initializers/load_settings.rb +++ b/config/initializers/load_settings.rb @@ -1 +1,2 @@ -APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/settings.yml")[RAILS_ENV].symbolize_keys \ No newline at end of file +#APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/settings.yml")[RAILS_ENV].symbolize_keys +APP_CONFIG = YAML.load(ERB.new(File.read("#{RAILS_ROOT}/config/settings.yml")).result)[RAILS_ENV].symbolize_keys diff --git a/lib/scan.rb b/lib/scan.rb index 814f033..e635912 100644 --- a/lib/scan.rb +++ b/lib/scan.rb @@ -1,10 +1,11 @@ module ScanFiles require "find" - + require "fileutils" supported_files = ["jpeg", "jpg", "gif", "png"] def self.FullScan + puts "Scanning " + APP_CONFIG[:photos_path] prevalbum = "" dirs = Array.new Find.find( APP_CONFIG[:photos_path] ) { |path| @@ -14,6 +15,20 @@ module ScanFiles if File.file?(path) && [".jpeg", ".jpg", ".gif", ".png"].include?( File.extname(path) ) relpath = File.dirname( path ).sub(APP_CONFIG[:photos_path], '') relfile = path.sub(APP_CONFIG[:photos_path], '') + puts relpath + 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 album = Album.find_by_path( relpath ) if prevalbum != relpath puts relpath