Question

This code works fine if the uploads are stored locally, but sometimes they are on S3 so it's not possible to just source: "#{File.expand_path(src.path)}[0]". How do I make the Paperclip's run method load images from S3 and replace them afterwards?

module Paperclip

  class KskCrop < Processor

    def initialize(file, options = {}, attachment = nil)
      super
      @crop = options
      @format = File.extname(@file.path)
      @basename = File.basename(@file.path, @format)
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format])
      dst.binmode

        parameters = []
        parameters << ":source"
        parameters << "-crop '#{@crop[2]}x#{@crop[3]}+#{@crop[0]}+#{@crop[1]}'"
        parameters << ":dest"

        parameters = parameters.flatten.compact.join(' ').strip.squeeze(' ')

        success = Paperclip.run('convert', parameters, source: "#{File.expand_path(src.path)}[0]", dest: File.expand_path(dst.path))

      dst
    end

  end
end
Was it helpful?

Solution 3

Looks like this is the solution!

module Paperclip

  class KskCrop < Processor

    def initialize(file, options = {}, attachment = nil)
      super
      @crop = options
      @format = File.extname(@file.path)
      @basename = File.basename(@file.path, @format)
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format])
      dst.binmode

      parameters = []
      parameters << ":source"
      parameters << "-crop '#{@crop[2]}x#{@crop[3]}+#{@crop[0]}+#{@crop[1]}'"
      parameters << ":dest"

      parameters = parameters.flatten.compact.join(' ').strip.squeeze(' ')

      path = if @file.options && @file.options[:storage] == :s3
        src.url
      else
        File.expand_path(src.path)
      end
      success = Paperclip.run('convert', parameters, source: path, dest: File.expand_path(dst.path))

      dst
    end

  end
end

OTHER TIPS

So, as per the conversation. It turned out that path has to be a complete url instead of a relative path. Hence, this line will look something like this:

success = Paperclip.run('convert', parameters, source: src.url, dest: File.expand_path(dst.path)

As, OP has already answered his question with an if.. else. I think that's a better approach to check if the attachment is on a local file or on a CDN.

P.S.: I also learned that Paperclip.run(..) method can actually locate and download a file for processing if it's a url without making an IO operations at developer's end.

well i didnt got your question well but for Amazon S3 READ/WRITE, this is what is use...

this is my s3.yml

development:
  bucket: app_development
  access_key_id: xxxxxxxxxxxxxx
  secret_access_key: xxxxxxxxxxxxx+xxxxxxxxxxx
production:
  bucket: app_production
  access_key_id: xxxxxxxxxxxxxxxxx
  secret_access_key: xxxxxxxxxxxx+xxxxxxxxxxxxx

my config/initializers/paperclip.rb

    Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
    Paperclip::Attachment.default_options[:path] = '/:class/:id/:style/:filename'
##will store in the foll way shown below in the bucket u specify in s3.yml
##http://app_development.s3.amazonaws.com/videos/1/original/small.mp4

    ##if Rails.env == "production" 
       #S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "ourbucket"} 
     ##else 
       S3_CREDENTIALS = Rails.root.join("config/s3.yml")
    ##end

so after this configuration,any paperclip model with has_attached_file:avatar will get uploaded on s3

##upload to s3..code snippet (can also use in _form.html.erb as f.file_field :avatar)
@video=Video.create!({ :avatar => File.new("#{Rails.root}/app/assets/images/dashboard/default_avatar.jpg")      
        })
##this is how u can display/access any uploaded object/model using url(which shows original unless you specify some styles -thumbnail,small)
@video.avatar.url
@video.avatar.url(:thumbnail)
@video.avatar.url(:small)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top