Ruby on Rails - Paperclip Error
Asked Answered
E

3

16

For reference I have been following this tutorial: https://devcenter.heroku.com/articles/paperclip-s3 Except I am in localhost testing right now, so I installed the Figaro gem and placed my S3 info in application.yml.

Using Rails v4, Cocaine v0.5.3, and Paperclip v4.1.0 (unsure if any other gem version numbers need to be mentioned).

I have a model called SubmissionDetails, where in its new.html.erb I am trying to upload a jpg to a column called attachment. Here is the relevant model code:

has_attached_file :attachment, styles: {
thumb: '200x200>',
large: '800x800>'
}

validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/

When I try to upload a jpg, it returns back to the form with the following error message:

1 error prohibited this submission_detail from being saved:
Attachment translation missing:
en.activerecord.errors.models.submission_detail.attributes.attachment.spoofed_media_type

I understand some of the error, that text to display this error message is missing from my en.yml file, but what about that spoofed media type part?

This shows up in my server console, not sure if this is relevant:

[paperclip] Content Type Spoof: Filename header.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
(0.0ms)  rollback transaction
Evelynneven answered 20/2, 2014 at 15:32 Comment(2)
Apparently, PaperClip thinks that this is not a valid jpeg file, respectively couldn't determine the real file type using the file command. Have you tried different file types (png, gif)?Fowle
Yes, I tried a png just now--same error.Evelynneven
L
45

That message is raised by a validation check for content spoofing.

For Paperclip v.4 this generates a bug https://github.com/thoughtbot/paperclip/issues/1429

While for Paperclip v.3, it seems it just throws a deprecation warning, https://github.com/thoughtbot/paperclip/issues/1423

So I'd wait for Paperclip team to solve this bug before using version 4. At the moment I'd rather keep using version 3.

gem "paperclip", "~> 3.5.3"

Or add this to an initializer to disable spoofing protection:

config/initializers/paperclip_media_type_spoof_detector_override.rb

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

see Can't upload image using Paperclip 4.0 Rails 3

Letti answered 20/2, 2014 at 15:40 Comment(3)
This bug is a real stinger! Thanks for this workaround.Bisect
Confirmed to still be an issue on 4.1.1. spoofed? override fixes it for now.Horrible
Still an issue on 4.2.0. Thanks for the spoofed? fix.Stapler
B
8

As explained recently in the comments of the issue (https://github.com/thoughtbot/paperclip/issues/1429#issuecomment-49821032), adding :

Paperclip.options[:command_path] = '/usr/bin'

to config/initializers/paperclip.rb solved the problem.

Belicia answered 3/8, 2014 at 8:56 Comment(2)
I don't think the accepted answer is the better way to deal with that. One year after and using paperclip v4.3 the issue was solved setting the command_path.Toothpaste
Have you checked where the "identify" command is installed on your system ? ($ whereis identify)Belicia
A
0

On the same issue, I found another workaround that can be applied at model level, without edit any initializer:

class PaperclipModel < ActiveRecord::Base
  has_attached_file :attachment, { validate_media_type: false }

  validates_attachment :attachment, {
    # tweak as desired
    content_type: { content_type: ["text/csv", "text/plain", Paperclip::ContentTypeDetector::SENSIBLE_DEFAULT] }
  }
end

Basically, this skip media_type and content_type validations to avoid spoofed error on PaperclipModel attachment. For more details see here.

Avantgarde answered 21/11, 2018 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.