Created
March 11, 2012 15:29
-
-
Save robban/2016821 to your computer and use it in GitHub Desktop.
Revisions
-
robban created this gist
Mar 11, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ class ConvertImagesToPaperclip def self.convert_all # Update table information Photo.reset_column_information # Delete all attachement_fu image sizes Photo.delete_all("parent_id IS NOT NULL") # Migrate data Photo.all.each do |photo| populate_paperclip_from_attachment_fu(photo, photo, 'photo', 'photos') if photo photo.photo.reprocess! if photo.photo end end def self.populate_paperclip_from_attachment_fu(model, attachment, prefix, path_prefix) unless attachment.photo_file_name.nil? # The columns have already been renamed by migration #model.send("#{prefix}_file_name=", attachment.photo_filename) #model.send("#{prefix}_content_type=", attachment.content_type) #model.send("#{prefix}_file_size=", attachment.size) puts "# data_file_name: #{model.send("#{prefix}_file_name")}" puts "# data_content_type: #{model.send("#{prefix}_content_type")}" puts "# data_file_size: #{model.send("#{prefix}_file_size")}" # Get file path from attachment_fu file_path = ("%08d" % model.id).scan(/..../).join('/') old_path = File.join(Rails.root, 'public', path_prefix, file_path, attachment.photo_file_name) # Need to use URI::decode to handle files with åäö in new_path = model.send(prefix).path(:original) new_folder = File.dirname(new_path) if File.exists?(old_path) unless File.exists?(new_folder) FileUtils.mkdir_p(new_folder) end puts "Copying #{old_path} to #{new_path}" system("cp #{old_path} #{new_path}") model.save else puts "No such file: #{old_path}" end end end end