Amazon S3 path or to_file wont work - ruby-on-rails

Hey guys ive got the following code in my Model
require 'RMagick'
class Upload < ActiveRecord::Base
belongs_to :card
has_attached_file :photo,
:styles => {
:thumb => ["100x100", :jpg],
:pagesize => ["500x400", :jpg],
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'your_deck',
:default_style => :pagesize
attr_accessor :x1, :y1, :width, :height
def update_attributes(att)
scaled_img = Magick::ImageList.new(self.photo.path)
orig_img = Magick::ImageList.new(self.photo.path(:original))
scale = orig_img.columns.to_f / scaled_img.columns
args = [ att[:x1], att[:y1], att[:width], att[:height] ]
args = args.collect { |a| a.to_i * scale }
orig_img.crop!(*args)
orig_img.write(self.photo.path(:original))
self.photo.reprocess!
self.save
super(att)
end
end
This code works offline however when on Heroku with Amazon S3 it fails to work, ive tried the code with to_file and it also wont work
I get the following error
can't convert Array into String

I had the same problem and it's due to an update to paperclip. I'm surprised heroku are still using this gem version as it will surely affect all their users; I installed a previous version as a plugin and it's fine. Don't forget to remove the gem from your .gems file or specify the previous version in your gems manifest.

Related

getting random error while upload photo with paperclip on my ftp server

I used gem "paperclip" for upload images and gem "paperclip-storage-ftp" for store images on my own ftp server. It worked fine for a while, but now I am getting random errors, when I upload photos.
Sometimes this
Net::FTPReplyError - 150 Connecting to port 3270
Sometime this
Net::ReadTimeout - Net::ReadTimeout:
Sometime this
Paperclip::Storage::Ftp::NoServerAvailable
.. and sometimes this:
Net::FTPPermError - 500 ?
Model
class AlbumPhoto < ActiveRecord::Base
has_attached_file :photo, {
# Choose the FTP storage backend
:storage => :ftp,
:path => "www.abcd.com/:attachment/:id/:style/:filename",
:url => "ftp://abcd#domain.com#domain.com/www.abcd.com/:attachment/:id/:style/:filename",
:ftp_servers => [
{
:host => "domain.com",
:user => "abcd#domain.com",
:password => "abcd123",
:passive => true
}
],
:styles => { :medium => "300x300^", :thumb => "100x100^" ,:large => "400x400^"},
}
end
Why is this happening?

Rails MIME::Types.type_for(photoshop_1354320001.psd) - MIME Type not found?

I'm using Rails 3, Paperclip(3.3.0), aws-sdk (1.7.1).
My paperclip attachments are being stored securely on S3.
attachment.rb
has_attached_file :attachment,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_protocol => 'https',
:s3_permissions => :private, # Sets the file, not the folder as private in S3
:use_timestamp => false,
:default_style => :original, # NEEDS to be original or download_url method below wont work
:default_url => '/images/:attachment/default_:style.png',
:path => "/:rails_env/private/s/:s_id/uuploaded_files/:basename.:extension"
In order to download the files I generate a secure URL like so:
def authenticated_url(style = nil, expires_in = 1.hour)
mime_type = MIME::Types.type_for(self.attachment_file_name)[0]
attachment.s3_object(style).url_for(:read, :secure => true, :response_content_type => mime_type.to_s, :expires => expires_in).to_s
end
The problem is for PSDs: This is return empty:
Rails MIME::Types.type_for('photoshop_1354320001.psd')
In the code it looks like:
mime_type = MIME::Types.type_for(self.attachment_file_name)[0]
It works for other files fine but not PSDs. Any idea why and how to resolve?
Thanks
Sure. MIME::Types lets you specify custom types.
Stick this into an initializer
# Not quite sure what the appropriate MIMEtype for PSDs are,
# but this is the gist of it.
# .PSB is a larger version of .PSD supporting up to 300000x300000 px
psd_mime_type = MIME::Type.new('image/x-photoshop') do |t|
t.extensions = %w(psd psb)
t.encoding = '8bit'
end
MIME::Types.add psd_mime_type
Now MIME::Types.type_for "test.psd" should give you "image/x-photoshop".

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 Amazon S3. Styled images can't be displayed, only downloaded. Original image is blank

Having some odd issues with paperclip, s3 and imagemagick.
The normal sized image will force an image download.
http://ads_honours_development.s3.amazonaws.com/assets/adverts/10/normal.jpg?1333899985
Meanwhile the original image is blank.
http://ads_honours_development.s3.amazonaws.com/assets/adverts/10/original.jpg?1333899985
Here is the model.
class Advert < ActiveRecord::Base
belongs_to :group
#paperclip
has_attached_file :photo, {
:styles => { :large => "512x512", :normal => "360x360", :small => "200x200", :tiny => "64x64", :thumbnail => "32x32" },
:convert_options => { :large => "-quality 86" },
:default_url => "/images/thumbnail/blank-recipe.png",
:url => ':s3_domain_url',
:path => 'assets/:class/:id/:style.:extension',
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_permissions => 'public-read',
:s3_protocol => 'http'}
validates_attachment_presence :photo
end
Gem file is like:
gem 'aws-sdk'
gem 'paperclip', '~> 2.0'
I can't work out what I'm doing wrong here. Any help/suggestions?
UPDATE I've got this running on heroku as well and the exact same issue occurs, so it's not down to my development machines settings. I'm guessing the problem lies within Paperclip itself, my bucket setup or the amazon aws-sdk gem.
SECOND UPDATE Ok so all of these images load in IE, but not Chrome. What gives?
Maybe you have an extension running on chrome which is blocking the images? The link shows for me in Chrome only with adblock disabled.

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