Rails, Paperclip not validating content type “application/octet-stream”?

送分小仙女□ 提交于 2019-12-11 06:14:41

问题


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

回答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'


来源:https://stackoverflow.com/questions/40953122/rails-paperclip-not-validating-content-type-application-octet-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!