From 0ca354cfd0ab561477fd67550902a8b978f9dc12 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Wed, 14 Sep 2011 15:11:50 +0800 Subject: [PATCH] Clean code and new image uploader --- app/models/design/image.rb | 2 + .../design_file_uploader.rb.to_be_delete | 47 ------------------- ...ign_file_uploader.rb => image_uploader.rb} | 28 ++++++++++- lib/parser.rb | 4 +- 4 files changed, 30 insertions(+), 51 deletions(-) delete mode 100644 app/uploaders/design_file_uploader.rb.to_be_delete rename app/uploaders/{design_file_uploader.rb => image_uploader.rb} (53%) diff --git a/app/models/design/image.rb b/app/models/design/image.rb index 98abeda4..c0c4881d 100644 --- a/app/models/design/image.rb +++ b/app/models/design/image.rb @@ -1,3 +1,5 @@ class Image < DesignFile embedded_in :design + + mount_uploader :file, ImageUploader end diff --git a/app/uploaders/design_file_uploader.rb.to_be_delete b/app/uploaders/design_file_uploader.rb.to_be_delete deleted file mode 100644 index 091471fd..00000000 --- a/app/uploaders/design_file_uploader.rb.to_be_delete +++ /dev/null @@ -1,47 +0,0 @@ -# encoding: utf-8 - -class DesignFileUploader < CarrierWave::Uploader::Base - - # Include RMagick or ImageScience support: - # include CarrierWave::RMagick - # include CarrierWave::ImageScience - - # Choose what kind of storage to use for this uploader: - # storage :file - # storage :s3 - - # Override the directory where uploaded files will be stored. - # This is a sensible default for uploaders that are meant to be mounted: - def store_dir - "assets/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" - end - - # Provide a default URL as a default if there hasn't been a file uploaded: - # def default_url - # "/images/fallback/" + [version_name, "default.png"].compact.join('_') - # end - - # Process files as they are uploaded: - # process :scale => [200, 300] - # - # def scale(width, height) - # # do something - # end - - # Create different versions of your uploaded files: - # version :thumb do - # process :scale => [50, 50] - # end - - # 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 - - # Override the filename of the uploaded files: - # def filename - # "something.jpg" if original_filename - # end - -end diff --git a/app/uploaders/design_file_uploader.rb b/app/uploaders/image_uploader.rb similarity index 53% rename from app/uploaders/design_file_uploader.rb rename to app/uploaders/image_uploader.rb index 091471fd..c00fdcb3 100644 --- a/app/uploaders/design_file_uploader.rb +++ b/app/uploaders/image_uploader.rb @@ -1,10 +1,20 @@ # encoding: utf-8 -class DesignFileUploader < CarrierWave::Uploader::Base +class ImageUploader < CarrierWave::Uploader::Base + + require 'mime/types' + + process :set_content_type # Include RMagick or ImageScience support: # include CarrierWave::RMagick # include CarrierWave::ImageScience + include CarrierWave::MiniMagick + + 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 # Choose what kind of storage to use for this uploader: # storage :file @@ -13,7 +23,7 @@ class DesignFileUploader < CarrierWave::Uploader::Base # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir - "assets/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + "image/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Provide a default URL as a default if there hasn't been a file uploaded: @@ -32,6 +42,10 @@ class DesignFileUploader < CarrierWave::Uploader::Base # version :thumb do # process :scale => [50, 50] # end + + version :thumb do + process :resize_to_fit => [200, 200] + end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: @@ -43,5 +57,15 @@ class DesignFileUploader < 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 end diff --git a/lib/parser.rb b/lib/parser.rb index 51e3e273..daf93e2b 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -65,8 +65,8 @@ module Parser end c.define_tag 'stylesheets' do |tag| res = '' - res << "" - res << " " + res << "" if page.design.reset_css + res << " " if page.design.default_css theme = page.design.themes.detect{ |d| d.id == page.theme_id } res << "" if theme res