【问题标题】:Rails, Paperclip not validating content type "application/octet-stream"?Rails,Paperclip 不验证内容类型“application/octet-stream”?
【发布时间】: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 来计算它使用的文件类型 file check github.com/thoughtbot/paperclip/issues/1530
  • 可能是该文件返回了不同的内容,例如 application/x-bittorrent,但 mime 类型将其视为 octet-stream
  • 文件类型会不一样吗?我刚刚尝试使用application/x-bittorrent 进行验证,但也没有用。
  • 尝试允许所有application/*,然后检查读取该文件的回形针
  • 不,如果不匹配,即使操作系统看到它们相同,类型验证也会失败

标签: ruby-on-rails paperclip


【解决方案1】:

验证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'

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 2018-10-27
    • 2021-06-27
    • 2012-05-06
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2018-01-29
    相关资源
    最近更新 更多