使用回形针的字体内容类型验证

时间:2016-03-20 18:27:46

标签: ruby-on-rails paperclip paperclip-validation

我试图在回形针中上传字体文件作为附件,我收到此错误。

ActiveRecord::RecordInvalid: Validation failed: File content type is invalid, File is invalid

这是我试过的

validates_attachment_content_type :file, content_type:['application/x-font-opentype','application/x-font-truetype','application/octet-stream']

我的模特

class OtherFont < ActiveRecord::Base
    has_many :texts
  has_attached_file :file, default_url: "/images/:style/missing.png"
   validates_attachment_content_type :file, content_type:['application/x-font-opentype','application/x-font-truetype','application/octet-stream']
  has_attached_file :file,
                    :storage => :s3,
                    :path => "fonts/:id/:style_:extension",
                    :s3_credentials => Proc.new{|a| a.instance.s3_credentials }
  def s3_credentials
    {:bucket => ENV['bucket'], :access_key_id => ENV['access_key_id'], :secret_access_key => ENV['secret_access_key']}
  end                  
end

1 个答案:

答案 0 :(得分:0)

.otf和.ttf的content_type被检测为'application / octet-stream'

您必须在/initialize/paperclip.rb

中创建内容类型映射
Paperclip.options[:content_type_mappings] = {
   otf: 'application/x-font-opentype',
   ttf: 'application/x-font-truetype'
}

对于.woff,content_type是'application / font-woff'。正确检测.woff的内容类型,不需要包含在content_type_mappings中。