Rails, Paperclip not validating content type "application/octet-stream"?
Asked Answered
I

1

1

I'm using paperclip to upload files of the mime-type "application/octet-stream", but they aren't validating properly.

In the controller, when I call replay.save!, I get the following error:

Validation failed: r_file has contents that are not what they are reported to be, r_file is invalid, r_file content type is invalid

Here is the model:

class Replay < ApplicationRecord
    has_attached_file :r_file
    validates_attachment_content_type :r_file, content_type: { content_type: "application/octet-stream" }
end

and the create method in the replay controller:

def create
    @replay = Replay.new(replay_params)
    if @replay.save
        # This never runs because it won't validate.
        puts "REPLAY SAVED."
        redirect_to @replay
    else
        puts "REPLAY NOT SAVED."
        render 'new'
    end
end

I checked the mime-type of the file I'm trying to upload, and it is definitely of type "application/octet-stream". Is Paperclip just reading the file type incorrectly?

EDIT:

Here is the schema:

ActiveRecord::Schema.define(version: 20161203161351) do

  create_table "replays", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "map"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "r_file_file_name"
    t.string   "r_file_content_type"
    t.integer  "r_file_file_size"
    t.datetime "r_file_updated_at"
  end

end
Intendment answered 3/12, 2016 at 22:22 Comment(11)
Paperclip doesn't use mime type gem to figure the file type it uses file check github.com/thoughtbot/paperclip/issues/1530Officiary
It might be that file is returning something different like application/x-bittorrent but mime type treat it as octet-streamOfficiary
Would the file type not be the same either way? I just tried validating using application/x-bittorrent and that didn't work either.Intendment
Try allowing all application/* and then check what paperclip read that fileOfficiary
No it would fail the type validation if it's not a match even if the os sees them the sameOfficiary
It didn't even validate using application/*.Intendment
@ArunKumar that reduced the error so something a little more specific. New error: Validation failed: r_file has contents that are not what they are reported to beIntendment
@JoshArts After changing the validation?Silver
This error is responding to content type spoofing... in a nutshell renaming .mp3 file to .txt and uploading it as txt fileOfficiary
That makes sense since my files are of type .replay , is there a way around this?Intendment
@ArunKumar yes, after changing the validation. I can also confirm that paperclip is reading the type as "application/octet-stream", as that's what it says in the output.Intendment
B
1

Validate all formats in rails Video/Image

validates_attachment_content_type :media,
      content_type: [
        'image/jpg',
        'image/jpeg',
        'image/pjpeg',
        'image/png',
        'image/x-png',
        'video/avi',
        'video/mov',
        'video/mp4',
        'video/x-flv',
        'video/3gpp',
        'video/quicktime',
        'video/x-msvideo',
        'video/x-ms-wmv',
        'flv-application/octet-stream',
        'application/octet-stream',
        'video/x-flv',
        'video/mpeg',
        'video/mpeg4',
        'video/x-la-asf',
        'video/x-ms-asf'
      ],
        :message => 'file type is not allowed'
Belated answered 15/11, 2017 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.