best way to process images already on S3 (rails) - ruby-on-rails-3

I have a set of images on Amazon S3, and I'd like to automatically generate thumbnails for them to serve on my site.
I've considered Cloudinary, but it seems that I'd have to copy over all my images to Cloudinary servers first. I want to keep them on S3.
I've considered Dragonfly, but it seems that Dragonfly only works with files I'd upload after installing Dragonfly. I already have uploaded all my files.
What's a good solution for me? I'm in a Rails environment (rails 3.2).
Thanks!

If it's just a 'set of images' it's not really that well structured. You're better off reorganizing the way you store and manage images.
Try Paperclip.
class ModelName < ActiveRecord::Base
attr_accessible :image #more here
has_attached_file :image, :styles => { :large => "450x450>", :medium => "300x300>", :thumb => "150x150>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "people/:style/:id/:filename",
:s3_protocol => "https"
def original_image_url
image.url
end
def large_image_url
image.url(:large)
end
def medium_image_url
image.url(:medium)
end
def small_image_url
image.url(:thumb)
end
#etc
end
Then simply do this to assign an existing image to an existing instance through the console:
require 'uri'
#instance.image = open(URI::encode(url))
#instance.save
# s3 will now contain the images in the appropriate sizes in the format specified above.
Since the original will also be saved, I'd advise to then delete the 'set of images' on s3 you started with, otherwise you'll be duplicating them.

I was wrong about Dragonfly. You can use Dragonfly on already-uploaded files. I'm using it on my project and it's working out great.

Related

Paperclip on S3 - retrieve deleted files?

I just made a massive cock-up in my rails app. I deleted half the users from the database on my dev site, thinking this would have no effect on the live site db, which seems to have led to the user avatars being removed on the live site!
I don't know much about paperclip or S3 but I'm begging for help if anyone knows if it is possible to get them back?
Does S3 have backups? Have the images really been deleted or just somehow detached from their associated user object?
I'm using paperclip in my user model file like so:
# paperclip avatars on S3
has_attached_file :avatar, {
:styles => { :medium => "200x200", :small => "100x100#", :thumb => "64x64#" },
:default_url => "/assets/profiles/avatar_default_200x200.png",
:path => "/avatars/:style/:id/:filename"
}.merge(PAPERCLIP_STORAGE_OPTIONS)
validates_attachment_size :avatar, :less_than => 2.megabytes,
:unless => Proc.new {|m| m[:image].nil?}
validates_attachment_content_type :avater, :content_type=>['image/jpeg', 'image/png', 'image/gif'],
:unless => Proc.new {|m| m[:image].nil?}
Thanks for any information!
If you haven't enabled versioning in S3, I guess you're out of luck.
In order to keep track of deleted objects from s3 you can add versioning to S3 buckets. This will cause S3 to keep versions of an object even after deletion.
Details can be found here.

Paperclip S3 image path issue

I've just upgraded from Rails 3.1 to Rails 3.2.6 and my paperclip photo paths are now broken on production.
photo.rb
if Rails.env == "production"
has_attached_file :image,
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url => "/app/public/system/images/:id/:style/:basename.:extension",
I need the resulting path to be like so: http://s3.amazonaws.com/photoramblr/app/public/system/images/5/thumb_large/image.jpg
but instead the above url settings result in: http://s3.amazonaws.com/photoramblr/app/public/app/public/system/images/5/thumb_large/image.jpg
I've also tried setting the paperclip url to :url => "/system/images/:id/:style/:basename.:extension" but that resulted in this url: http://s3.amazonaws.com/photoramblr/images/5/thumb_large/image.jpg
Any thoughts on how I can set this properly?
UPDATE: Well, I still don't understand how it's working, but I 'fixed' this by just moving the files to the location it was looking in.
Try adding a path parameter replacing the url parameter and specifying the path under the bucket name where your file will be stored and the URL will be constructed from the bucket and the path supplied. Here is a link to paperclip s3 docs for further details. If you supply a url parameter, it is treated relative to your "app/public". That explains why you're resulting image path doubles "app/public". Ryan Bate's paperclip tutorial also explains this behaviour.
In your case your code will be as shown below;
if Rails.env.production?
has_attached_file :image,
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:path => "app/public/system/images/:id/:style/:basename.:extension",
Your resulting link will be (I'm assuming your bucket name is photorambler from your link); s3.amazonaws.com/photorambler/app/public/system/images/5/thumb_large/image.jpg

rails paperclip: How to access the url of the a

I am new to rails and paperclip plugin - so I apologize if this is as a silly question.
I am trying to upload a file and later access the url for the file url. In dev, the file gets stored locally while in prod, the file is pushed to s3.
Once the model is stored in the db, how to I get the url later on to display the link?
When you specify a paperclip association in a model you do :
class ModelName < ActiveRecord::Base
has_attached_file :thumbnail, :styles => { :medium => "300x300#", :thumb => "100x100#"} , :default_url => '/images/default/missing.png'
end
To get the url, you have to do :
#model.thumbnail.url(:original)
Where :original is the file format, it can be :medium, :thumb...

Default_url in Paperclip Broke with Asset Pipeline Upgrade

I'm using Paperclip and have a default_url option like this for one of my attachments:
:default_url => '/images/missing_:style.png'
The asset pipeline obviously doesn't like this since the directories moved. What's the best way to handle this? I have two styles for this picture (:mini and :thumb).
:default_url => ActionController::Base.helpers.asset_path('missing_:style.png')
Then put the default images in app/assets/images/
Tested only on Rails 4.
To make it work in production, we have to pass the name of an existing file to the asset_path helper. Passing a string containing a placeholder like "missing_:style.png" therefore doesn't work. I used a custom interpolation as a workaround:
# config/initializers/paperclip.rb
Paperclip.interpolates(:placeholder) do |attachment, style|
ActionController::Base.helpers.asset_path("missing_#{style}.png")
end
Note that you must not prefix the path with images/ even if your image is located in app/assets/images. Then use it like:
# app/models/some_model.rb
has_attached_file(:thumbnail,
:default_url => ':placeholder',
:styles => { ... })
Now default urls with correct digest hashes are played out in production.
The default_url option also takes a lambda, but I could not find a way to determine the requested style since interpolations are only applied to the result of the lambda.
Just make sure that in your views all your paperclip images are rendered with image_tag.
<%= image_tag my_model.attachment.url(:icon) %>
That way, all of paperclip's :crazy :symbol :interpolation will have happened to the url string before Rails tries to resolve it to an asset in the pipeline.
Also, make sure your :default_url is asset compatible...if missing_icon.png is at app/assets/images/missing_icon.png, then :default_url should be simply "missing_:style.png"
<%= image_tag my_model.attachment.url(:icon) %>
# resolves to...
<%= image_tag "missing_icon.png" %>
# which in development resolves to...
<img src="/assets/missing_icon.png">
I got the error(even for a single style) at assets:precompile with
:default_url => ActionController::Base.helpers.asset_path('missing.png')
So I hooked with a method like this
# supposing this is for avatar in User model
has_attached_file :avatar,
:styles => {..},
:default_url => lambda { |avatar| avatar.instance.set_default_url}
def set_default_url
ActionController::Base.helpers.asset_path('missing.png')
end
I didn't try for multiple styles, but this works for my situation.
this works for me:
has_attached_file :avatar, :styles => { :small => "52x52",
:medium => "200x200>", :large=> "300x300", :thumb => "100x100>" },
:default_url => "missing_:style.png"
just place images in your assets/images folder named: missing_large.png, missing_medium.png, missing_small.png and missing_thumb.png
In rails 4.0.0 and paperclip 4.1.1 this worked for me:
has_attached_file :avatar,
styles: { medium: '300x300#', small: '100x100#', thumb: '25x25#' },
default_url: ->(attachment) { 'avatar/:style.gif' },
convert_options: { all: '-set colorspace sRGB -strip' }
I ended up having to use something like the following.
DEFAULT_URL = "#{Rails.configuration.action_controller.asset_host}#{Rails.configuration.assets.prefix}/:attachment/:style/missing.png"
has_attached_file :art, :styles => { :large => "398x398#", :medium => "200x200#", :small=>"100x100#", :smaller=>"50x50#", :smallest=>"25x25"}, :path=>"images/:attachment/:id/:style/:basename.:extension", :default_url => DEFAULT_URL
I statically compile the assets and was getting an error in production, this helped me.
In your model file, change this line:
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
by removing this:
/images/
Create a folder for each style, in this example medium and thumb, in assests/images and place an image called missing.png there (or whatever name you want it to have of course, as long as it matches the file name in the model)
Worked for me.
I've solved this problem by using a custom interpolator.
The problem from other solutions that suggest using
:default_url => ActionController::Base.helpers.asset_path('missing_:style.png')
is that you will get an error saying "missing_style.png" is not precompiled.
I created an initializer with the following code:
module Paperclip
module AssetPipeline
module Interpolator
def self.interpolate(pattern, *args)
ActionController::Base.helpers.asset_path Paperclip::Interpolations.interpolate(pattern, *args)
end
end
end
end
Then in my model I would do:
has_attached_file :image, interpolator: Paperclip::AssetPipeline::Interpolator, ...
Just remove the / from /images/pic.png: images/pic.png

How to store prawn PDF files into Amazon S3

I have a problem with the path. In my model I have following setup:
class Pdffiles < ActiveRecord::Base
belongs_to :user
has_attached_file :invoice_file,
:path => ":rails_root/public/pdffiles/:user_id/:style/:basename.:extension",
:url => "/pdffiles/:user_id/:style/:basename.:extension",
:storage => :s3,
:bucket => '...',
:s3_credentials => {
:access_key_id => '...',
:secret_access_key => '...'
}
end
and in a controller looks my action this:
pdf = Prawn::Document.new
pdf.move_down 70
pdf.text("Prawn Rocks")
pdf.render_file('prawn.pdf')
pdf_file = File.open('prawn.pdf')
pdff = Pdffile.new()
pdff.pdffile_file = pdf_file
pdff.user_id = todays_user.id
pdff.save
And my problem is, that this PDF file is stored to the S3 server, but on the bad place. Instead the directory app/public/pdff/id_of_a_user/file_name_of_pdf_file is the file saved to
Users/my_name/my_ruby_root_directory/name_of_my_project/public/pdffiles/id_of_a_user/file_name_of_pdf_file.
I am not totally sure, if I use the prawn for saving PDF files right, but I think the problem could be in the controller, where I have set up the place, where the created file have to be saved...
I would like to ask you, what I should change for saving PDF files into the right directory in S3... All helps will be appreciated!
Manny thanks, Sep
You could use something like CarrierWave ( https://github.com/jnicklas/carrierwave ) -- it makes uploading to S3 extremely easy with the FOG library https://github.com/jnicklas/carrierwave
The Users/my_name/my_ruby_root_directory/name_of_my_project/public portion of the path came from :rails_root/public portion of the path you configured in paperclip. So if you really want the s3 "directory" to be app/public/pdff/id_of_a_user/file_name_of_pdf_file you need to give paperclip the following path: app/public/pdffiles/:user_id/:style/:basename.:extension
Also, according to your model, you should use pdff.invoice_file = pdf_file instead of pdff.pdffile_file = pdf_file
I hope this helps.