Uploading and Resizing Multiple Images Using Mercury Editor - ruby-on-rails

I'm using the latest and greatest Mercury editor in my Rails 3.2.9 application.
I have adapted the Image model to allow uploading to s3:
has_attached_file :image,
:styles => { :medium => "200x115" },
:storage => :s3,
:s3_protocol => 'https',
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style/:filename",
:url => ":attachment/:id/:style/:filename",
:bucket => 'ps-wifi'
This is working fine.
I'm trying to figure out how I can allow the user to upload multiple images, each with different attributes.
For example, the above method works for a logo but we're need another, larger image at 600 x 350. Previously, I had another "has_attached_file" but am not sure how to achieve with Mercury.

Related

Delayed Paperclip with s3 Missing Bucket Option in Production

I'm trying to used delayed_paperclip to process image resize in the background. This process works in development but in production I get this in my delayed job's last error.
missing required :bucket option\n/home/server_username/project_folder_name/shared/bundle/ruby/2.2.0/gems/paperclip-4.3.0/lib/paperclip/storage/s3.rb:218:in 'bucket_name'...etc
In application.rb
config.paperclip_defaults = {
:storage => :s3,
:url =>':s3_domain_url',
:s3_protocol => 'https',
:path => '/:class/:attachment/:id_partition/:style/:filename',
:bucket => ENV['s3_bucket_name'],
:s3_credentials => {
:access_key_id => ENV['s3_id'],
:secret_access_key => ENV['s3_key']
}
}
In my class file
has_attached_file :uploaded_file,
:styles => { original: "990x990#",large: "300x300#", medium: "200x200#", thumb: "100x100#"},
only_process: [:medium]
process_in_background :uploaded_file, queue: "queue_name",
only_process: [:original, :large, :thumb]
Uploading to s3 works without using delayed_paperclip. I just wanted to use this library so people didn't have to wait for these to be uploaded/resiszed. I'm working on Ubuntu14.04. Deploying w/ Capistrano.
I saw a couple places that the bucket should be outside of the s3_credentials, so I moved it to where it is now. So I've tried it there and inside s3_credentials -- no change either way.
The medium image is uploaded and resized immediately and works.
Let me know if there is some info I didn't provide.

Rails, PaperClip, S3, Heroku: Model icon fields not being saved

I am using Rails 3.2 + Heroku + S3 + Paperclip to store an icon on my User model. The model is not saving the 4 icon fields though. The images are getting processed and saved on S3 correctly and no errors are occurring. I also have another model that has a document being stored via Paperclip and S3. That model works perfectly in all cases. The User icon works locally but not on Heroku.
production.rb relevant configuration
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
User model code:
class User < ActiveRecord::Base
attr_accessible :icon
has_attached_file :icon, :url => "/system/:rails_env/:attachment/:style/:hash.:extension",
:hash_data => ":class/:attachment/:id",
:hash_secret => "superSecretThing",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/blank.png"
...
Controller code: (This code is kind of crazy because I am AJAXing files Base64 encoded.)
params[:user][:icon_data]
decoded_file = Base64.decode64(data)
begin
split_name = params[:user][:icon_file_name].split(".")
file = Tempfile.new([split_name[0..-2].join("."), ".#{split_name[-1]}"])
file.binmode
file.write(decoded_file)
file.close
#user.icon = open(file)
#user.icon_file_name = params[:user][:icon_file_name]
ensure
file.unlink
end
#user.save
I do an almost identical process on another model with a Paperclip attachment and it works flawlessly. In both cases the attachment is being saved correctly to S3 and no errors are being raised. This gist has example output for a controller action from the Heroku logs.
I am pretty baffled because the other model works fine. The only real difference is that the User attachment does image processing but that part appears to be working fine.
The problem is the same as this one, but the solution there does not apply.
Thoughts?
So the problem is that not including the :path argument makes it try to use the :url parameter for both the url and the path. The real fix is to include the :path parameter in addition to the url.
So for example a fixed configuration that works both locally and on Heroku:
has_attached_file :icon,
:url => "/system/:rails_env/:attachment/:style/:hash.:extension",
:path => "public/system/:rails_env/:attachment/:style/:hash.:extension",
:hash_data => ":class/:attachment/:id",
:hash_secret => "superDuperSecret",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/blank.png"

Paperclip and S3: Multiple thumbnail sizes not saving

I'm using Paperclip to handle image uploads for my Rails app, and it's working great when I use system storage: multiple thumbnail sizes ("styles" in Paperclip parlance) are saved to file, and I can access any of them by passing the style name to the url method.
When I set up the app to store images on S3 (using the aws-s3 gem), however, only one image is stored in my S3 bucket. For what it's worth, only the last style listed is stored. So, if in my model, I've got:
has_attached_file :photo,
:styles => { :large => "1000x1000>",
:medium => "600x600>",
:thumb => "200x200>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => AppConstants.bucket,
:path => "pictures/:id/:filename"
Only the "thumb" size will be saved to S3.
Has anybody encountered a similar problem? How can I fix this?
I'm not sure why this works locally, but you didn't specify :style in your path declaration.

Paperclip Amazon S3 setup with Heroku

has_attached_file :image, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => "/:style/:filename"
I'm not sure what :path => "/:style/:filename" is.
I also want to to include the style for this attached image, is that what the :path is?
the style I want is this: :styles => { :medium => "275x275>", :thumb => "175x155>" }
Basically what's going on here is that I'm setting up on heroku and I'm having to use S3 which seems straightforward just not used to this attachment convention stuff.
Also, I just signed up for an S3 account... but heroku was spouting that its free or something. What's the deal with that?
The 'path' specifies the location on S3 where the files will be stored. Thus, if you specify an attachment as:
has_attached_file :image,
:styles => { :medium => "275x275>", :thumb => "175x155>" },
:storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
:path => "user/:attachment/:style/:id.:extension"
A sample URL will be:
http://s3.amazonaws.com/bucket/user/image/thumb/347853856.jpg
Finally, S3 is NOT free (Heroku simply states transfer / uploads are not counted in the usage based calculations). Heroku's documentation is excellent if you need further information.
Note that in Rails 3.1 and above, it should be Rails.root and not RAILS_ROOT

How to pass additional convert options to paperclip on Heroku?

class User < ActiveRecord::Base
has_attached_file :photo, :styles => { :square => "100%", :large => "100%" },
:convert_options => {
:square => "-auto-orient -geometry 70X70#",
:large => "-auto-orient -geometry X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
validates_attachment_size :photo,
:less_than => 5.megabyte
end
Works great on local machine, but gives me an error on Heroku: There was an error processing the thumbnail for stream.20143
The thing is I want to auto-orient photos before resizing, so they resized properly.
The only working variant now(thanks to jonnii) is resizing without auto-orient:
...
as_attached_file :photo, :styles => { :square => "70X70#", :large => "X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
...
How to pass additional convert options to paperclip on Heroku?
UPD
I discover, the trouble in "-auto-orient" option. It seems like this option is broken in version of ImageMagick used by Heroku. I created custom paperclip image processor inherited from paperclip's standard thumbnail:
module Paperclip
class Ao < Thumbnail
def transformation_command
super + " -auto-orient"
end
end
end
It works perfect on local machine, but fails on Heroku.
These are the sizes I use. They all work fine on heroku:
SIZES = {
:original => "640x480>",
:thumb => "150x150#",
:mini => "60x60#",
:micro => "30x30#"
}
Make sure your gem version of paperclip is the same as heroku's. You can specify the specific gem version in your .gems file and in your environment.rb to make sure they line up.
I'm not sure exactly why your convert_options are causing problems, but if I remember correctly paperclip uses ImageScience directly and your chosen options might be incompatible with the read only heroku file system.
If this is critical and you need an answer right now I'd raise a support ticket on heroku. If you get a response make sure you post it back here!

Resources