gpt4 book ai didi

ruby-on-rails - ruby 在 rails 上。回形针 mp3 验证失败

转载 作者:行者123 更新时间:2023-12-04 00:07:08 25 4
gpt4 key购买 nike

好的,我允许用户上传 mp3。现在由于某种原因,只有一些 mp3 文件会上传,而其他文件则不会。我找不到工作文件和非工作文件之间的任何明显区别。

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

失败文件的服务器输出是;

Command :: file -b --mime '/tmp/acf7bcfce06ffcaa55511087ea2e486f20160427-7322-y1lyj6.mp3'
[paperclip] Content Type Spoof: Filename leyinnata.mp3 (audio/mp3 from Headers, ["audio/mpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.

所以文件被视为 application/octet-stream 而不是 audio/mp3 我不知道为什么。

我听取了阅读文档的建议并找到了可能的解决方案:

回形针.rb

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' ]

我也尝试将 application/octet-stream 添加到 validates_attachment_content_type例如。

validates_attachment_content_type :audio, :content_type => [ 'application/octet-stream', '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] = {
audio: "application/octet-stream"
}

到他们的 environment.rb 文件。这对我也不起作用。

更新 2:

在回形针.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

让我上传我需要的东西,但这会跳过欺骗验证,我想这可能很危险。我的用户还可以在网站的单独部分上传图片,我知道我现在很容易受到攻击。

更新 3:

我已经添加了

module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end

到我的 paperclip.rb。这似乎工作正常。如果有人能提出更好的解决方案,那么我很乐意听到,否则我会回答我自己的问题。

最佳答案

我通过删除我放在“更新”中的所有内容并添加来解决它:

Paperclip.options[:content_type_mappings] = {
mp3: 'application/octet-stream'
}

paperclip.rb

关于ruby-on-rails - ruby 在 rails 上。回形针 mp3 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com