回形针无视

och

我的应用程序中有回形针(带有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

回形针似乎不知道如何处理音频流。

 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

** 进步 **

我取得了一些进展:将音频流写入了Tempfile ...但是现在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方法到底在做什么,但是您需要确保它返回文件句柄,例如

sentence.audio = File.new(path_to_your_file, "r")
sentence.save

至于您的Tempfile方法,请确保按以下方式创建它

Tempfile.new([ 'foobar', '.mp3' ])

这样PaperPlip不会抱怨文件扩展名

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章