Akhil Latta
Akhil Latta

Reputation: 558

Error uploading non image files with paperclip in rails 4.2

I am getting this error while uploading non-image files through paperclip.

Paperclip error - NotIdentifiedByImageMagickError

Here is my model code:

has_attached_file :attachment, :styles => { :medium => "236x236>", :thumb => "150x150>", :large => "1000x500" }

Here is the version of my gems: paperclip (4.2.1) activemodel (>= 3.0.0) activesupport (>= 3.0.0) cocaine (~> 0.5.3) mime-types

It works fine if I remove the styles from model. But I need to resize the images as well.

Upvotes: 3

Views: 560

Answers (2)

animatedgif
animatedgif

Reputation: 1109

Try using the before_post_process model hook. It allows you to change or cancel the image processing. From an example described here https://github.com/thoughtbot/paperclip#events

if you return false (specifically - returning nil is not the same) in a before_filter, the post processing step will halt.

class Message < ActiveRecord::Base
  has_attached_file :asset, styles: {thumb: "100x100#"}

  before_post_process :skip_for_audio

  def skip_for_audio
    ! %w(audio/ogg application/ogg).include?(asset_content_type)
  end
end

Upvotes: 2

Shahzad Tariq
Shahzad Tariq

Reputation: 2857

You need to remove styles from model. The error is due to fact that when you upload non-image file, paperclip send this file to imageMagick for resizing according to style specified. As file is not image file, so imageMagick failed to convert file to given resolution and system crashed.

You may seprate fields for image and non-image attachments.

has_attached_file :image_attachment, :styles => { :medium => "236x236>", :thumb => "150x150>", :large => "1000x500" }
has_attached_file :attachment

For image attachment field, specify styles and dont specify styles for non-image attachment.

Upvotes: 1

Related Questions