Sha256: b81d7d1406b88a852f57fcd3c23a8833aab50bf372a9bf877ce8be8af1f8d51e

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

module Paperclip
  class AttachmentAdapter < AbstractAdapter
    def self.register
      Paperclip.io_adapters.register self do |target|
        Paperclip::Attachment === target || Paperclip::Style === target
      end
    end

    def initialize(target, options = {})
      super
      @target, @style = case target
                        when Paperclip::Attachment
                          [target, :original]
                        when Paperclip::Style
                          [target.attachment, target.name]
                        end

      cache_current_values
    end

    private

    def cache_current_values
      self.original_filename = @target.original_filename
      @content_type = @target.content_type
      @tempfile = copy_to_tempfile(@target)
      @size = @tempfile.size || @target.size
    end

    def copy_to_tempfile(source)
      if source.staged?
        link_or_copy_file(source.staged_path(@style), destination.path)
      else
        begin
          source.copy_to_local_file(@style, destination.path)
        rescue Errno::EACCES
          # clean up lingering tempfile if we cannot access source file
          destination.close(true)
          raise
        end
      end
      destination
    end
  end
end

Paperclip::AttachmentAdapter.register

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kt-paperclip-7.2.2 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-7.2.1 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-7.2.0 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.4.2 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-7.1.1 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-7.1.0 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-7.0.1 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-7.0.0 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.4.1 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.4.0 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.3.0 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.2.2 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.2.1 lib/paperclip/io_adapters/attachment_adapter.rb
kt-paperclip-6.2.0 lib/paperclip/io_adapters/attachment_adapter.rb