-
-
Save bhushangahire/266037 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
# Patch Paperclip reprocess! and to_file methods to use binary mode (for Windows compatibility) | |
# Fixup content type on assignment | |
# Ensure the :original style is always processed first | |
module Paperclip | |
module Storage | |
module Filesystem | |
def to_file style = default_style | |
@queued_for_write[style] || (File.new(path(style), 'rb') if exists?(style)) | |
end | |
alias_method :to_io, :to_file | |
end | |
end | |
class Attachment | |
def assign_with_mimetype_fu(uploaded_file) | |
uploaded_file = uploaded_file.to_file(:original) if uploaded_file.is_a?(Paperclip::Attachment) | |
uploaded_file.content_type = File.mime_type?(uploaded_file.original_filename) if uploaded_file.respond_to?(:content_type) && uploaded_file.content_type.to_s.strip == 'application/octet-stream' | |
assign_without_mimetype_fu(uploaded_file) | |
end | |
alias_method_chain :assign, :mimetype_fu | |
def extra_options_for(style) #:nodoc: | |
convert_options_for_style = convert_options[style].respond_to?(:call) ? convert_options[style].call(instance) : convert_options[style] | |
[ convert_options_for_style, convert_options[:all] ].compact.join(" ") | |
end | |
def reprocess! | |
new_original = Tempfile.new("paperclip-reprocess") | |
new_original.binmode | |
if old_original = to_file(:original) | |
@queued_for_write = { :original => old_original.stream_to(new_original) } | |
post_process | |
old_original.close if old_original.respond_to?(:close) | |
save | |
else | |
true | |
end | |
end | |
private | |
def post_process #:nodoc: | |
return if @queued_for_write[:original].nil? | |
logger.info("[paperclip] Post-processing #{name}") | |
@styles.sort { |a, b| a.first == :original ? -1 : 1 }.each do |name, args| | |
begin | |
dimensions, format = args | |
dimensions = dimensions.call(instance) if dimensions.respond_to? :call | |
@queued_for_write[name] = Thumbnail.make(@queued_for_write[:original], | |
dimensions, | |
format, | |
extra_options_for(name), | |
@whiny_thumbnails) | |
rescue PaperclipError => e | |
@errors << e.message if @whiny_thumbnails | |
end | |
end | |
end | |
end | |
class Thumbnail | |
def transformation_command | |
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?) | |
if convert_options? | |
trans = " #{convert_options}" | |
else | |
trans = "-resize \"#{scale}\"" | |
trans << " -crop \"#{crop}\" +repage" if crop | |
end | |
trans | |
end | |
end | |
end |
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
before_update :crop_photo | |
has_attached_file :photo, | |
:styles => { :original => Proc.new { |u| u.crop_photo? ? '' : '100%x100%' }, | |
:inbox => '20x20!' }, | |
:convert_options => { :original => Proc.new { |u| u.crop_photo? ? "-crop #{u.photo_crop_width}x#{u.photo_crop_height}+#{u.photo_crop_left}+#{u.photo_crop_top} +repage" : '' } } | |
[ :photo_crop_width, :photo_crop_height, :photo_crop_top, :photo_crop_left ].each do |attr_name| | |
attr_writer attr_name | |
attr_accessible attr_name | |
define_method(attr_name) do | |
instance_variable_get("@#{attr_name}").to_i | |
end | |
end | |
def crop_photo? | |
!(photo.nil? || photo.dirty? || [ photo_crop_width, photo_crop_height, photo_crop_top, photo_crop_left ].all?(&:zero?)) | |
end | |
def crop_photo | |
photo.reprocess! if crop_photo? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment