Can't upload zip files using ruby on rails and paperclip gem - ruby-on-rails

I'm working on a rails web application. Just created and prepared some models and stuff. The application is very simple. But now I have a problem by setting up the gem paperclip to add attachments to a model. Almost everything works fine, like attaching images/jpg or even pdf.
But I can't upload zip files. I tried different zip files, but I'm always getting:
"Attachment Paperclip::Errors::NotIdentifiedByImageMagickError"
This is my model:
class Order < ActiveRecord::Base
has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","image/jpg","image/png"]
belongs_to :client
has_one :status
end
I'm developing on a Mac (Yosemite), installed imagemagick via brew and using SQLite.
I added this to my Gemfile:
gem "paperclip", "~> 4.2"
gem 'cocaine', '~> 0.5.4'
I did research on google the last hours, and there are many people struggling with paperclip, but I didn't find anybody with problems uploading zip files.
Maybe someone can help here.
Thanks

ImageMagick can't read .zip files. See the acceptable file types here:
http://www.imagemagick.org/script/formats.php
If you're trying to generate a thumbnail from a zip file ImageMagick should fail every time.
Try adding this to your model:
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(asset_content_type)
end
Then your app won't try to process zip files as images

Related

fixture_file_upload gives a "{***} has an extension that does not match its contents" from paperclip model rails 4.2.1

I'm having this issue with uploading paperclip documents with rails fixture_file_upload (after upgrading rails version)
Knowing that paperclip 4.0 and above requires validating content_type, My specs still fails with the error: File has an extension that does not match its contents even after either after the validation or specifically requesting do_not_validate_attachment_file_type.
I'm having this scenario in multiple(all my) paperclip models...
An example is as follow:
Model:
class SignatureFile < ActiveRecord::Base
...
has_attached_file :file
VALID_CONTENT_TYPES = ["image/jpeg", "image/jpg", "image/png", "image/gif"]
validates_attachment :file, content_type: { content_type: VALID_CONTENT_TYPES },
convert_options: { all: '-auto-orient' },
processors: [:compression]
...
end
Spec:
image = fixture_file_upload('spec/fixtures/mobile_api/sample.jpg', 'image/jpg')
When I try to save image above, it fails, and image.errors.full_messages gives:
"File has an extension that does not match its contents"
Suffice to say I have a bunch of scenarios like this one which was previously passing before the upgrade.
Below are versions of the related gems I have:
gem 'rails', '4.2.7.1'
gem 'paperclip', '4.2.1'
gem 'paperclip-compression', '0.3.7'
gem 'rspec-rails', '3.4.2'
What can I be missing here?
You might be missing file, sadly the error isn't more precise in those cases. Read in the paperclip docs.

Paperclip with sidekiq

I am using paperclip for uploading files. Does anybody have use it with sidekiq for background jobs?
I was trying to achieve something similar to railscast 383 (uploading to Amazon S3) but with paperclip and sidekiq.
I didn't find too much information for using it with sidekiq and I am thinking if I should change to carrierwave or if there is any example for paperclip and sidekiq (not with FancyUploader and delayed_jobs).
I think what you're looking for is the delayed_paperclip gem:
https://github.com/jrgifford/delayed_paperclip/
According to the gem it has a nice ActiveRecord api so you can do something like:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
process_in_background :avatar
end

Image file is not recognized by the 'identify' command. (heroku)

I'm trying to have Paperclip working with Heroku and Amazon S3.
Everything works fine on localhost (mac OS and Amazon), but when I'm deploying to heroku and trying the feature, I have this error :
2 errors prohibited this area from being saved:
Asset /tmp/paris20121005-2-2cwxgx.jpg is not recognized by the 'identify' command.
Asset /tmp/paris20121005-2-2cwxgx.jpg is not recognized by the 'identify' command.
It works when I remove the :styles => { } option in my model, but the file isn't processed (I need different image sizes).
I also have the rmagick gem in my gemfile.
Here is my gemfile (only the paperclip part) :
gem "paperclip"
gem "rmagick", :require => 'RMagick'
gem 'aws-sdk', '~> 1.3.4'
I don't have Paperclip.options[:command_path] set in my environment.rb or production.rb so no problem on this side.
Here is my model :
class Area < ActiveRecord::Base
require 'RMagick'
has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "180x190>" },
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:url => :s3_domain_url.to_s,
:path => "/:style/:id/:filename"
end
Any clue on that ? I've crawled every topics about it and nothing seems to work...
Thanks
Apparently the new update to Cocaine gem (0.4.0) breaks the file names for Paperclip and ImageMagick. try rolling back to the previous version (0.3.2), it worked for me.
See here:
https://github.com/thoughtbot/paperclip/issues/1038
PS I believe RMagick is no longer needed on Heroku, works fine for me without it

The perfect way to upload file in Rails 3.1

I'm new in Ruby/Rails, so, my question: what is the perfect way to upload file and save filename in database with rails? Are there any Gem? Or maybe there are good build-in feature?
Also try carrierwave, Ryan has a good screencast about this gem
Just take a look on the links to choose between paperClip & carrierwave :
Rails 3 paperclip vs carrierwave vs dragonfly vs attachment_fu
And
http://bcjordan.github.com/posts/rails-image-uploading-framework-comparison/
You can try Paperclip.It is most popular gem in rails community...
https://github.com/thoughtbot/paperclip
these lines do the all stuff
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
Try it......
Most people use the Paperclip Gem.

Rails 3 app + paperclip gem + production mode = download of empty files

I've installed the paperclip gem for a Rails 3 application. Everything works fine in development mode. However, when running in production mode, if I upload a file and then try to download it again, it downloads a file with the correct name and extension, but it is an empty file. When looking on the server, the file does get uploaded and is in the correct directory. (I have an "uploads" folder in my application root.)
Anyone had this happen?
My model:
# app/models/document.rb
class Document < ActiveRecord::Base
belongs_to :kase
has_attached_file :document, :path => (Rails.root + "uploads/:class/:kase_id/:id").to_s, :url => ":class/:id"
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => [
'application/pdf',
'image/png',
'image/jpeg',
'image/pjpeg',
'text/plain'
]
end
My controller:
# app/controllers/documents_controller.rb
class DocumentsController < ApplicationController
respond_to :html
before_filter :initialize_kase # Sets the #kase instance
def show
#document = #kase.documents.find(params[:id])
send_file #document.document.path, :filename => #document.document_file_name, :content_type => #document.document_content_type
end
end
And my initializer (setting the :kase_id placeholder used in has_attached_file above:
# config/initializers/paperclip.rb
Paperclip.interpolates('kase_id') do |attachment, style|
"kases/#{attachment.instance.kase.id.to_s}"
end
I should probably mention, too, that I am accessing this as a nested controller (/kases/XX/documents/XX). Not sure if that has an effect or not...
If you are using Apache and Passenger, (possibly other servers as well) and have the line:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
in your production.rb env file, then you have two options:
Install the apache module mod-xsendfile
Comment out that line and let Rails send the files instead of Apache, like it does in development mode.
Are you carrying over the uploads directory each time you deploy your app to production? Assuming that you're using capistrano (or similar) for deployment, each time you deploy you might be creating a new uploads directory in the newly-deployed release directory. In that case, the previously-uploaded files are residing in older deployed releases (if you didn't delete those) and would no longer be accessible to your app.
You want to create e.g. shared/uploads directory that is symlinked into your app on each deploy.

Resources