【发布时间】:2016-12-03 22:22:24
【问题描述】:
我正在使用回形针上传 mime 类型“application/octet-stream”的文件,但它们没有正确验证。
在控制器中,当我调用replay.save! 时,出现以下错误:
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
这是模型:
class Replay < ApplicationRecord
has_attached_file :r_file
validates_attachment_content_type :r_file, content_type: { content_type: "application/octet-stream" }
end
以及replay控制器中的create方法:
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
我检查了我要上传的文件的 MIME 类型,它肯定是 "application/octet-stream" 类型。 Paperclip 只是读取文件类型不正确吗?
编辑:
这是架构:
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
【问题讨论】:
-
Paperclip 不使用 mime 类型 gem 来计算它使用的文件类型
filecheck github.com/thoughtbot/paperclip/issues/1530 -
可能是该文件返回了不同的内容,例如
application/x-bittorrent,但 mime 类型将其视为octet-stream -
文件类型会不一样吗?我刚刚尝试使用
application/x-bittorrent进行验证,但也没有用。 -
尝试允许所有
application/*,然后检查读取该文件的回形针 -
不,如果不匹配,即使操作系统看到它们相同,类型验证也会失败