Created
December 20, 2010 14:00
-
-
Save guimello/748404 to your computer and use it in GitHub Desktop.
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 characters
class Widget < ActiveRecord::Base | |
validates_numericality_of :custom_width | |
has_attached_file :file, | |
:storage => :s3, | |
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml", | |
:path => "widgets/:id/file/:normalized_file_file_name_:style.:extension", | |
:url => ":s3_alias_url", | |
:s3_host_alias => "#{S3Config[:bucket]}", | |
:styles => lambda { |attachment| attachment.instance.file_styles } | |
# http://blog.wyeworks.com/2009/7/13/paperclip-file-rename | |
Paperclip.interpolates :normalized_file_file_name do |attachment, style| | |
attachment.instance.normalized_file_file_name | |
end | |
def normalized_file_file_name | |
self.file_file_name[/.*(?=\..+$)/].gsub( /[^a-zA-Z0-9_]/, '_') | |
end | |
# Conditional paperclip styles | |
# http://groups.google.com/group/paperclip-plugin/browse_thread/thread/983875401f91bb6b?fwc=1 | |
def file_styles | |
styles = {} | |
# At the very least, every image is going to be resized to a thumbnail | |
styles[:thumb] = { :geometry => '100x67#', :source_file_options => "-resample 72"} | |
# Then, if a there is a custom width set, resize that too | |
if !custom_width.blank? | |
styles[:custom] = { :geometry => "#{custom_width}x9999", :source_file_options => "-resample 72"} | |
end | |
return styles | |
end | |
# This halts the processing of the styles unless the upload is an image | |
# Handles the non-processing of non-images and still allows me to have the styles param | |
# http://stackoverflow.com/questions/2919811/styles-in-paperclip-only-if-its-an-image-rails | |
before_file_post_process :process_only_images | |
private | |
def process_only_images | |
if !(file.content_type =~ %r{^(image|(x-)?application)/(x-png|pjpeg|jpeg|jpg|png|gif)$}) | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment