From 9f5bce935134e08dfc0ebd6151a025496e7b7581 Mon Sep 17 00:00:00 2001 From: chris Date: Sun, 20 Oct 2013 16:07:20 +0800 Subject: [PATCH] Review of the uploaders Rake task to recreate version: uploader:recreate or uploader:recreate name1 name2 ... --- app/uploaders/asset_uploader.rb | 15 +++---- app/uploaders/avatar_uploader.rb | 26 ++++++------ .../ckeditor_attachment_file_uploader.rb | 4 ++ app/uploaders/ckeditor_picture_uploader.rb | 4 ++ app/uploaders/file_asset_uploader.rb | 15 +++---- app/uploaders/image_uploader.rb | 27 ++----------- app/uploaders/location_uploader.rb | 21 +++++----- lib/tasks/uploader.rake | 40 +++++++++++++++++++ 8 files changed, 90 insertions(+), 62 deletions(-) create mode 100644 lib/tasks/uploader.rake diff --git a/app/uploaders/asset_uploader.rb b/app/uploaders/asset_uploader.rb index 7b5749c9..54e06099 100644 --- a/app/uploaders/asset_uploader.rb +++ b/app/uploaders/asset_uploader.rb @@ -1,14 +1,11 @@ # encoding: utf-8 +require 'carrierwave/processing/mime_types' class AssetUploader < CarrierWave::Uploader::Base - require 'mime/types' - - process :set_content_type - def set_content_type(*args) - content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type - self.file.instance_variable_set(:@content_type, content_type) - end + include CarrierWave::MimeTypes + + process :set_content_type # Include RMagick or ImageScience support: # include CarrierWave::RMagick @@ -52,4 +49,8 @@ class AssetUploader < CarrierWave::Uploader::Base # model.filename # end + def cache_dir + "#{Rails.root}/tmp/uploads" + end + end diff --git a/app/uploaders/avatar_uploader.rb b/app/uploaders/avatar_uploader.rb index c2aa3d7b..a1174c22 100644 --- a/app/uploaders/avatar_uploader.rb +++ b/app/uploaders/avatar_uploader.rb @@ -1,11 +1,15 @@ # encoding: utf-8 +require 'carrierwave/processing/mime_types' class AvatarUploader < CarrierWave::Uploader::Base # Include RMagick or ImageScience support: # include CarrierWave::RMagick # include CarrierWave::ImageScience - include CarrierWave::MiniMagick + include CarrierWave::MiniMagick + include CarrierWave::MimeTypes + + process :set_content_type # Choose what kind of storage to use for this uploader: # storage :file @@ -43,23 +47,17 @@ class AvatarUploader < CarrierWave::Uploader::Base # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: - # def extension_white_list - # %w(jpg jpeg gif png) - # end + def extension_white_list + %w(jpg jpeg gif png) + end # Override the filename of the uploaded files: # def filename # "something.jpg" if original_filename # end - -# def manipulate! -# raise current_path.inspect -# image = ::MiniMagick::Image.open(current_path) -# image = yield(image) -# image.write(current_path) -# ::MiniMagick::Image.open(current_path) -# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e -# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}") -# end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end end diff --git a/app/uploaders/ckeditor_attachment_file_uploader.rb b/app/uploaders/ckeditor_attachment_file_uploader.rb index 8d0e4773..f70c66e2 100644 --- a/app/uploaders/ckeditor_attachment_file_uploader.rb +++ b/app/uploaders/ckeditor_attachment_file_uploader.rb @@ -33,4 +33,8 @@ class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base def extension_white_list Ckeditor.attachment_file_types end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end end diff --git a/app/uploaders/ckeditor_picture_uploader.rb b/app/uploaders/ckeditor_picture_uploader.rb index 4a5a56a8..e18952e2 100644 --- a/app/uploaders/ckeditor_picture_uploader.rb +++ b/app/uploaders/ckeditor_picture_uploader.rb @@ -44,4 +44,8 @@ class CkeditorPictureUploader < CarrierWave::Uploader::Base def extension_white_list Ckeditor.image_file_types end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end end diff --git a/app/uploaders/file_asset_uploader.rb b/app/uploaders/file_asset_uploader.rb index 1f60f00b..14d12012 100644 --- a/app/uploaders/file_asset_uploader.rb +++ b/app/uploaders/file_asset_uploader.rb @@ -1,14 +1,11 @@ # encoding: utf-8 +require 'carrierwave/processing/mime_types' class FileAssetUploader < CarrierWave::Uploader::Base - require 'mime/types' - - process :set_content_type - def set_content_type(*args) - content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type - self.file.instance_variable_set(:@content_type, content_type) - end + include CarrierWave::MimeTypes + + process :set_content_type # Include RMagick or ImageScience support: # include CarrierWave::RMagick @@ -52,4 +49,8 @@ class FileAssetUploader < CarrierWave::Uploader::Base # model.filename.force_encoding("UTF-8") # end + def cache_dir + "#{Rails.root}/tmp/uploads" + end + end diff --git a/app/uploaders/image_uploader.rb b/app/uploaders/image_uploader.rb index ef789d0f..751933a2 100644 --- a/app/uploaders/image_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -1,22 +1,15 @@ # encoding: utf-8 +require 'carrierwave/processing/mime_types' class ImageUploader < CarrierWave::Uploader::Base - require 'mime/types' - require 'carrierwave/processing/mini_magick' - include Sprockets::Helpers::RailsHelper - - process :set_content_type - # Include RMagick or ImageScience support: # include CarrierWave::RMagick # include CarrierWave::ImageScience include CarrierWave::MiniMagick + include CarrierWave::MimeTypes - def set_content_type(*args) - content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type - self.file.instance_variable_set(:@content_type, content_type) - end + process :set_content_type # Choose what kind of storage to use for this uploader: # storage :file @@ -63,20 +56,8 @@ class ImageUploader < CarrierWave::Uploader::Base # "something.jpg" if original_filename # end -def cache_dir + def cache_dir "#{Rails.root}/tmp/uploads" end - - - # def manipulate! - # cache_stored_file! if !cached? -#raise File.extname(current_path.to_s).to_s - # image = ::MiniMagick::Image.open(current_path) -# image = yield(image) -# image.write(current_path) -# ::MiniMagick::Image.open(current_path) -# rescue -# nil - # end end diff --git a/app/uploaders/location_uploader.rb b/app/uploaders/location_uploader.rb index 1c2ec99d..e5e24355 100644 --- a/app/uploaders/location_uploader.rb +++ b/app/uploaders/location_uploader.rb @@ -1,9 +1,14 @@ +require 'carrierwave/processing/mime_types' + class LocationUploader < CarrierWave::Uploader::Base # Include RMagick or ImageScience support: # include CarrierWave::RMagick # include CarrierWave::ImageScience - include CarrierWave::MiniMagick + include CarrierWave::MiniMagick + include CarrierWave::MimeTypes + + process :set_content_type # Choose what kind of storage to use for this uploader: # storage :file @@ -46,16 +51,10 @@ class LocationUploader < CarrierWave::Uploader::Base # def filename # "something.jpg" if original_filename # end - -# def manipulate! -# raise current_path.inspect -# image = ::MiniMagick::Image.open(current_path) -# image = yield(image) -# image.write(current_path) -# ::MiniMagick::Image.open(current_path) -# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e -# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}") -# end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end end diff --git a/lib/tasks/uploader.rake b/lib/tasks/uploader.rake new file mode 100644 index 00000000..3c06558d --- /dev/null +++ b/lib/tasks/uploader.rake @@ -0,0 +1,40 @@ +namespace :uploader do + task :recreate => :environment do + mongoid_collections = Mongoid.database.collections.reject{|c| c.name.include?('.')} + ARGV.shift + if ARGV.present? + uploaders = [] + ARGV.each{ |arg| + task arg.to_sym do ; end + uploaders << arg + } + else + uploaders = %w(AvatarUploader ImageUploader) + end + klasses = mongoid_collections.inject([]) { |klasses, coll| + if klasses.blank? + klasses = coll.find().map{|c| c['_type']}.uniq.compact + else + klasses += coll.find().map{|c| c['_type']}.uniq.compact + end + klasses + } + to_recreate = klasses.inject([]) { |names, name| + klass = name.constantize rescue nil + klass.uploaders.each_value{ |value| + if uploaders.include?(value.to_s) && !names.include?(name) + names << name + end + } unless klass.nil? + names + } + to_recreate.each { |name| + klass = name.constantize + klass.all.each{ |object| + klass.uploaders.each_key{ |key| + object.send(key).recreate_versions! if object[key] rescue nil + } + } + } + end +end