mattpolito (owner)

Fork Of

Revisions

gist: 92008 Download_button fork
public
Public Clone URL: git://gist.github.com/92008.git
Embed All Files: show embed
paperclip.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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
user.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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