Ruby on rails RubyonRails。回形针mp3验证失败
好的,我允许用户上传mp3。现在由于某些原因,只有一些mp3文件会上传,而其他的不会。我找不到工作文件和非工作文件之间的任何区别Ruby on rails RubyonRails。回形针mp3验证失败,ruby-on-rails,paperclip,mp3,Ruby On Rails,Paperclip,Mp3,好的,我允许用户上传mp3。现在由于某些原因,只有一些mp3文件会上传,而其他的不会。我找不到工作文件和非工作文件之间的任何区别 class Song < ActiveRecord::Base belongs_to :user has_attached_file :audio, :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%#]/, dependent: :destroy valida
class Song < ActiveRecord::Base
belongs_to :user
has_attached_file :audio, :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%#]/, dependent: :destroy
validates_attachment_presence :audio
validates_attachment_content_type :audio, :content_type => [ 'audio/mpeg', 'audio/mp3' ]
validates_attachment_size :audio, :less_than => 20.megabytes
end
因此,该文件被视为应用程序/octet流
,而不是音频/mp3
,我不知道为什么
我注意到了阅读文档的建议,并找到了一个可能的解决方案:
回形针
Paperclip.options[:content_type_mappings] = {
:audio=> 'application/octet-stream'
}
这没用。(我重新启动了服务器)
我不明白为什么它不起作用,我现在感到非常沮丧。任何帮助都将不胜感激,谢谢
更新:
指定更多的音频文件类型似乎没有任何区别
validates_attachment_content_type :audio, :content_type => [ 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]
我还尝试将应用程序/octet流
添加到验证附件内容类型
例如
不走运
我看见有人加了一句
Paperclip.options[:content_type_mappings] = {
audio: "application/octet-stream"
}
到他们的environment.rb文件。这对我也不管用
更新2:
在paperclip.rb中,添加:
module Paperclip
# do not require any validations
REQUIRED_VALIDATORS = []
# do not complain when missing validations
class Attachment
def missing_required_validator?
false
end
end
# skip media type spoof detection
module Validators
class MediaTypeSpoofDetectionValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
true
end
end
end
end
到我的
曲别针.rb
。这似乎工作正常。如果有人能想出更好的解决方案,那么我很乐意听到,否则我会回答我自己的问题。也许你必须更具体地进行mp3类型验证。
比如说,
你应该
validates_content_type :audio :content_type => [ 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]
希望这能解决你的问题。请告诉我们。我通过删除“更新”中的所有内容并添加以下内容解决了此问题:
Paperclip.options[:content_type_mappings] = {
mp3: 'application/octet-stream'
}
发送到
paperclip.rb
嘿,谢谢你的回复,但是没有帮助。我得到了完全相同的结果。成功了!还可使用回形针4.x制作导轨4
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
validates_content_type :audio :content_type => [ 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]
Paperclip.options[:content_type_mappings] = {
mp3: 'application/octet-stream'
}