【问题标题】:Paperclip without a view没有视图的回形针
【发布时间】:2016-02-26 20:04:59
【问题描述】:

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

** 进展 **

我取得了一些进展:将音频流写入 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.

【问题讨论】:

    标签: ruby-on-rails amazon-s3 paperclip


    【解决方案1】:

    我不知道你的 get_audio 方法到底在做什么,但你需要确保它返回一个文件句柄,例如

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

    至于您的 Tempfile 方法,请确保像这样创建它

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

    这样 PaperClip 就不会抱怨文件扩展名了

    【讨论】:

      【解决方案2】:

      关于如何在不通过视图的情况下将音频流保存到 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 对文件问题的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        相关资源
        最近更新 更多