Review of the uploaders

Rake task to recreate version: uploader:recreate or uploader:recreate name1 name2 ...
This commit is contained in:
chris 2013-10-20 16:07:20 +08:00
parent cce8f1be9b
commit 9f5bce9351
8 changed files with 90 additions and 62 deletions

View File

@ -1,15 +1,12 @@
# encoding: utf-8 # encoding: utf-8
require 'carrierwave/processing/mime_types'
class AssetUploader < CarrierWave::Uploader::Base class AssetUploader < CarrierWave::Uploader::Base
require 'mime/types'
include CarrierWave::MimeTypes
process :set_content_type 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 RMagick or ImageScience support: # Include RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # include CarrierWave::ImageScience
@ -52,4 +49,8 @@ class AssetUploader < CarrierWave::Uploader::Base
# model.filename # model.filename
# end # end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end end

View File

@ -1,4 +1,5 @@
# encoding: utf-8 # encoding: utf-8
require 'carrierwave/processing/mime_types'
class AvatarUploader < CarrierWave::Uploader::Base class AvatarUploader < CarrierWave::Uploader::Base
@ -6,6 +7,9 @@ class AvatarUploader < CarrierWave::Uploader::Base
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # 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: # Choose what kind of storage to use for this uploader:
# storage :file # storage :file
@ -43,23 +47,17 @@ class AvatarUploader < CarrierWave::Uploader::Base
# Add a white list of extensions which are allowed to be uploaded. # Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this: # For images you might use something like this:
# def extension_white_list def extension_white_list
# %w(jpg jpeg gif png) %w(jpg jpeg gif png)
# end end
# Override the filename of the uploaded files: # Override the filename of the uploaded files:
# def filename # def filename
# "something.jpg" if original_filename # "something.jpg" if original_filename
# end # end
# def manipulate! def cache_dir
# raise current_path.inspect "#{Rails.root}/tmp/uploads"
# image = ::MiniMagick::Image.open(current_path) end
# 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
end end

View File

@ -33,4 +33,8 @@ class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
def extension_white_list def extension_white_list
Ckeditor.attachment_file_types Ckeditor.attachment_file_types
end end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end end

View File

@ -44,4 +44,8 @@ class CkeditorPictureUploader < CarrierWave::Uploader::Base
def extension_white_list def extension_white_list
Ckeditor.image_file_types Ckeditor.image_file_types
end end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end end

View File

@ -1,15 +1,12 @@
# encoding: utf-8 # encoding: utf-8
require 'carrierwave/processing/mime_types'
class FileAssetUploader < CarrierWave::Uploader::Base class FileAssetUploader < CarrierWave::Uploader::Base
require 'mime/types'
include CarrierWave::MimeTypes
process :set_content_type 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 RMagick or ImageScience support: # Include RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # include CarrierWave::ImageScience
@ -52,4 +49,8 @@ class FileAssetUploader < CarrierWave::Uploader::Base
# model.filename.force_encoding("UTF-8") # model.filename.force_encoding("UTF-8")
# end # end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end end

View File

@ -1,22 +1,15 @@
# encoding: utf-8 # encoding: utf-8
require 'carrierwave/processing/mime_types'
class ImageUploader < CarrierWave::Uploader::Base 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 RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # include CarrierWave::ImageScience
include CarrierWave::MiniMagick include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
def set_content_type(*args) process :set_content_type
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
# Choose what kind of storage to use for this uploader: # Choose what kind of storage to use for this uploader:
# storage :file # storage :file
@ -67,16 +60,4 @@ def cache_dir
"#{Rails.root}/tmp/uploads" "#{Rails.root}/tmp/uploads"
end 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 end

View File

@ -1,9 +1,14 @@
require 'carrierwave/processing/mime_types'
class LocationUploader < CarrierWave::Uploader::Base class LocationUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support: # Include RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # 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: # Choose what kind of storage to use for this uploader:
# storage :file # storage :file
@ -47,15 +52,9 @@ class LocationUploader < CarrierWave::Uploader::Base
# "something.jpg" if original_filename # "something.jpg" if original_filename
# end # end
# def manipulate! def cache_dir
# raise current_path.inspect "#{Rails.root}/tmp/uploads"
# image = ::MiniMagick::Image.open(current_path) end
# 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
end end

40
lib/tasks/uploader.rake Normal file
View File

@ -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