没有视图的回形针
Paperclip without a view
我的应用程序中有回形针(带有 S3),用于音频文件。模型定义用回形针连接S3。
# attachments
has_attached_file :audio, storage: :s3, s3_credentials: Proc.new{|a| a.instance.s3_credentials}
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' ]
我可以使用以下代码通过 rails simple_form 上传文件:
<%= simple_form_for(@sentence) do |f| %>
<%= f.error_notification %>
.
<%= f.input :audio, as: :file %>
.
<% end %>
我还想使用后台 (Resque) 进程创建音频。此代码从 Web API 检索音频流并尝试将其保存到现有模型实例。没用。
sentences.each do |sentence|
sentence.audio = get_audio(sentence.sentence)
sentence.save
end
Paperclip 似乎不知道如何处理音频流。
failed: #<Paperclip::AdapterRegistry::NoHandlerError: No handler found for "\xFF\xF3\xC8\xC4\x00\x00\x00\x03H\x00\x00\x00\x00LAME3.99.5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
** 进度 **
我取得了一些进展:将音频流写入临时文件...但现在 Paperclip 抱怨编码问题
def get_audio_tempfile(target)
audio = translator.speak "#{target}", :language => "#{@language_cd}", :format => 'audio/mp3', :options => 'MaxQuality'
tempfile = Tempfile.new("target_temp.mp3")
tempfile.binmode
tempfile.write(audio)
tempfile.close
tempfile.open
tempfile
end
错误:
[paperclip] Content Type Spoof: Filename target_temp.mp320160226-32064- r391y9 (audio/mpeg from Headers, [] from Extension), content type discovered from file command: audio/mpeg. See documentation to allow this combination.
我不明白你的 get_audio 方法到底在做什么,但你需要确保它 returns 是一个文件句柄,例如
sentence.audio = File.new(path_to_your_file, "r")
sentence.save
至于你的临时文件方法,确保像这样创建它
Tempfile.new([ 'foobar', '.mp3' ])
这样 PaperClip 就不会抱怨文件扩展名了
关于如何在不通过视图的情况下将音频流保存到 Paperclip/S3 的摘要。
假设 Paperclip 正在工作并写入 S3,需要执行以下步骤从视图以外的其他地方上传文件(使用 Paperclip)(在我的例子中,这是一个 Resque 进程)。
为什么这样做:为了解决前后台处理问题,或者批量上传大量数据。
型号
# attachments
has_attached_file :audio, storage: :s3, s3_credentials: Proc.new{|a| a.instance.s3_credentials}
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' ]
查看
<%= simple_form_for(@sentence) do |f| %>
<%= f.error_notification %>
.
<%= f.input :audio, as: :file %>
.
<% end %>
工作
sentence.audio = get_audio_tempfile(sentence.sentence)
sentence.save
get_audio_tempfile
def get_audio_tempfile(target)
audio = translator.speak "#{target}", :language => "#{@language_cd}", :format => 'audio/mp3', :options => 'MaxQuality'
tempfile = Tempfile.new(['target_temp','.mp3'])
tempfile.binmode
tempfile.write(audio)
tempfile.rewind
tempfile
end
重要提示:
- 包括正确的文件扩展名
- 倒回临时文件
- 使用前不要关闭临时文件
感谢@tobmatth 对文件问题的帮助。
我的应用程序中有回形针(带有 S3),用于音频文件。模型定义用回形针连接S3。
# attachments
has_attached_file :audio, storage: :s3, s3_credentials: Proc.new{|a| a.instance.s3_credentials}
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' ]
我可以使用以下代码通过 rails simple_form 上传文件:
<%= simple_form_for(@sentence) do |f| %>
<%= f.error_notification %>
.
<%= f.input :audio, as: :file %>
.
<% end %>
我还想使用后台 (Resque) 进程创建音频。此代码从 Web API 检索音频流并尝试将其保存到现有模型实例。没用。
sentences.each do |sentence|
sentence.audio = get_audio(sentence.sentence)
sentence.save
end
Paperclip 似乎不知道如何处理音频流。
failed: #<Paperclip::AdapterRegistry::NoHandlerError: No handler found for "\xFF\xF3\xC8\xC4\x00\x00\x00\x03H\x00\x00\x00\x00LAME3.99.5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
** 进度 **
我取得了一些进展:将音频流写入临时文件...但现在 Paperclip 抱怨编码问题
def get_audio_tempfile(target)
audio = translator.speak "#{target}", :language => "#{@language_cd}", :format => 'audio/mp3', :options => 'MaxQuality'
tempfile = Tempfile.new("target_temp.mp3")
tempfile.binmode
tempfile.write(audio)
tempfile.close
tempfile.open
tempfile
end
错误:
[paperclip] Content Type Spoof: Filename target_temp.mp320160226-32064- r391y9 (audio/mpeg from Headers, [] from Extension), content type discovered from file command: audio/mpeg. See documentation to allow this combination.
我不明白你的 get_audio 方法到底在做什么,但你需要确保它 returns 是一个文件句柄,例如
sentence.audio = File.new(path_to_your_file, "r")
sentence.save
至于你的临时文件方法,确保像这样创建它
Tempfile.new([ 'foobar', '.mp3' ])
这样 PaperClip 就不会抱怨文件扩展名了
关于如何在不通过视图的情况下将音频流保存到 Paperclip/S3 的摘要。
假设 Paperclip 正在工作并写入 S3,需要执行以下步骤从视图以外的其他地方上传文件(使用 Paperclip)(在我的例子中,这是一个 Resque 进程)。
为什么这样做:为了解决前后台处理问题,或者批量上传大量数据。
型号
# attachments
has_attached_file :audio, storage: :s3, s3_credentials: Proc.new{|a| a.instance.s3_credentials}
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' ]
查看
<%= simple_form_for(@sentence) do |f| %>
<%= f.error_notification %>
.
<%= f.input :audio, as: :file %>
.
<% end %>
工作
sentence.audio = get_audio_tempfile(sentence.sentence)
sentence.save
get_audio_tempfile
def get_audio_tempfile(target)
audio = translator.speak "#{target}", :language => "#{@language_cd}", :format => 'audio/mp3', :options => 'MaxQuality'
tempfile = Tempfile.new(['target_temp','.mp3'])
tempfile.binmode
tempfile.write(audio)
tempfile.rewind
tempfile
end
重要提示:
- 包括正确的文件扩展名
- 倒回临时文件
- 使用前不要关闭临时文件
感谢@tobmatth 对文件问题的帮助。