Paperclip: proper file name is not validate while saving - ruby-on-rails

I am using rails 3.1.
I am trying to upload .docx file without proper file name (it contains only extension 'docx'), while saving it should validate like "file name is invalid".
In model,
validates_attachment_presence :document
validates_attachment_size :document, :less_than => 5.megabytes, :message => "should be less than 5Mb"
validates_attachment_content_type :document, :content_type => ['application/txt', 'text/plain',
'application/pdf', 'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.oasis.opendocument.text',
'application/x-vnd.oasis.opendocument.text',
'application/rtf', 'application/x-rtf', 'text/rtf', 'text/richtext', 'application/doc', 'application/x-soffice', 'application/octet-stream']
Paperclip gem which i am using,
paperclip (3.0.4, 2.8.0, 2.4.5)
Eg : I am trying to upload ' .docx' file.
Please suggest how to avoid saving these kind of files.

Related

Adding Paperclip Attachment to Spree Orders Table,

I am working on an ecommerce website using Solidus, Rails. The site allows you to order photo frames & prints from a variety of options.
To print a photo a user must upload the jpg file of the photo. So, to allow that I modified the orders table and added a paperclip attachment called 'attachment'
I ran the following command
rails generate paperclip SpreeOrder attachment
Which generated the migrations, then I ran rake db:migrate
Then I created a spree/order_decorator.rb file, and added has_attached_file
module Spree::OrderDecorator
has_attached_file :attachment, styles: {
:medium => {
:geometry => "640x480",
:format => 'jpeg'
},
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
Spree::Order.prepend self
end
After this I ran the server, and ended up getting this error
undefined method `has_attached_file' for Spree::OrderDecorator:Module (NoMethodError)
I have configured solidus for use with paperclip only, so I am really confused as to why I am getting this error, even later I manually went and generated a paperclip.rb file in the config/initializers directory, but still I get the same error.
Please help with this!!
Thank You!!
You should add those paperclip method at class level in the prepended module:
def self.prepended(base)
base.has_attached_file
end

How to update attachment using paperclip

I used paperclip to upload images to S3,
has_attached_file :attachment,
styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>',larger: '860x1280>' },
default_style: :product
validates_attachment :attachment,
:presence => true,
:content_type => { :content_type => %w(image/jpeg image/jpg image/png image/gif) }
Now , I want to compress the images which are already uploaded to S3 using gem "paperclip-compression", so I added processors: [:thumbnail, :compression], How would I update all the attachments using a ruby script??. I am able to read and store image into file but unable to update the attachment with the file.
According to paperclip wiki you should use reprocess! method:
Model.each do |model|
model.attachment.reprocess!
end
Another option is to use rake task:
# only thumbnails style
rake paperclip:refresh:thumbnails CLASS=Model
# or all styles
rake paperclip:refresh CLASS=Model
# only missing styles
rake paperclip:refresh:missing_styles

Photo has an extension that does not match its contents, rails 4.1.1

i have a problem with using paperclip. when i resart my rails server i can once upload a image using paperclip(success), but when i try to upload another image i'm always getting the error message: "Photo has an extension that does not match its contents".
for file validation i use:
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png']
i use ruby 2.0 and rails 4.1.1 and the file i try to upload is ofc an image.
can someone tell me how to fix this issue?
thanks, felix.
Replace your code with this validates_attachment_content_type :asset, :content_type => /\Aimage\/.*\Z/
It should work

Paperclip Content Type Validation Failing in Rspec

I'm using Rails 4.0.0 with Paperclip 4.1.1 for attaching mp3 and pdf files. I'm writing my integration tests in Rspec with Capybara.
I have content type and file name validations in place for both types of files.
class Song < ActiveRecord::Base
validates :title, presence: true
validates :writeup, presence: true
has_attached_file :mp3
validates_attachment :mp3,
:content_type => { :content_type => "audio/mp3" },
:file_name => { :matches => [/mp3\Z/] }
has_attached_file :chords
validates_attachment :chords,
:content_type => { :content_type => 'application/pdf' },
:file_name => { :matches => [/pdf\Z/] }
end
I use this in my integration test to fill in attributes for a valid song:
def fill_in_valid_song
fill_in("Title", with: "Valid Song")
fill_in("Writeup", with: "Description of song")
attach_file("Mp3", File.join(Rails.root, "/spec/factories/Amazing_Grace.mp3" ))
attach_file("Chords", File.join(Rails.root, "/spec/factories/FakeChordChart.pdf" ))
end
When I run the integration test for creating a valid song, the pdf file is accepted, but the mp3 file fails the content type validation.
When I follow the same steps myself in the browser, the song uploads successfully without errors. The model spec also passes using the same file.
I thought the problem might be the capital "M" in "Mp3" when I attach the file, but this is just to specify which file field the attachment goes with. When I tried changing it to a lowercase "m", the error changed to Capybara not being able to find the field.
Change the content type validation for the mp3 file to be:
validates_attachment :mp3,
:content_type => { :content_type => ["audio/mpeg", "audio/mp3"] },
:file_name => { :matches => [/mp3\Z/] }
The RFC defined MIME type for mp3 files is audio/mpeg. However some browsers load them as audio/mp3 which is probably why it works through the browser.
If this doesn't work you could also add audio/mpeg3 and audio/x-mpeg-3 as I've seen these used too.

Ruby on Rails / Paperclip / AWS::S3::NoSuchBucket error

I installed the paperclip plugin and was able to use it locally. When I configured it to work with amazon S3 I keep getting the NoSuchBucket (The specified bucket does not exist) error. Paperclip documentation states that the bucket will be created if it doesn't exist but clearly
something is going wrong in my case.
I first insalled aws-s3 gem (v0.6.2)
then also installed right_aws gem (v1.9.0)
both have corresponding
config.gem "aws-s3", :lib => "aws/s3"
config.gem 'right_aws', :version => '1.9.0'
lines in environment.rb file
The code for the image.rb file with paperclip is as follows:
class Image < ActiveRecord::Base
belongs_to :work
has_attached_file :photo, :styles => {:big => "612x1224>", :small => "180X360>", :thumb => "36x36#"},
:storage => 's3',
:s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/s3.yml")[RAILS_ENV],
:path => ":attachment/:id/:style/:basename.:extension",
:bucket => 'my-unique-image-bucket'
attr_protected :photo_file_name, :photo_content_type, :photo_size
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 3.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png', 'image/gif']
end
I'm not entirely sure this is it, but your loading of the s3_credentials is different than what I'm using on my production sites.
My config line is:
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml"
Instead of
:s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/s3.yml")[RAILS_ENV]
it should create but the bucket but this was a bug at one point :
http://groups.google.com/group/paperclip-plugin/browse_thread/thread/42f148cee71a0477
i recently had this problem and it turned out to be the servers time was hugely off and s3 wouldnt allow any updates "that far in the future" or similar but the rails error was NoSuchBucket...confusing
..
I have installed the s3fox plugin for firefox and created the bucket with the plugin. Now Paperclip works fine with S3 as the bucket identified is already created.
But I am still curious about paperclip's inability to create new buckets with the code above.
In case anyone winds up here via google: I saw this same error when I mistakenly switched the order of the 2nd and 3rd parameters I was passing to AWS::S3::S3Object.store.
It's not your case, but AWS doesn't allow upper case letters in bucket name and paperclip doesn't check that, failing in create_bucket.

Resources