【问题标题】:attachment_fu custom processor and s3: file seems to be converted ok, but isn't savedattachment_fu 自定义处理器和 s3: 文件似乎转换正常,但没有保存
【发布时间】:2015-08-10 16:46:15
【问题描述】:

设置:

ruby 1.8.6
rails 2.2.2
attachment_fu - not sure (it's vendorized), but last entry in CHANGELOG is "Apr 17 2008"
aws-s3 (0.6.3)
aws-sdk (2.1.13)
aws-sdk-core (2.1.13)
aws-sdk-resources (2.1.13)

我有一个使用 attachment_fu 的模型,如下所示:

has_attachment :storage => :s3, 
               :path_prefix => "vip_uploads#{("_"+RAILS_ENV) if RAILS_ENV != "production"}",
               :max_size => 100.megabytes,
               :processor => :mp3

s3 的东西都设置好了 - 如果我删除 processor 选项,那么上传到 s3 就可以了。

我的 mp3 处理器将 wav 文件转换为 mp3,如下所示:

module Technoweenie # :nodoc:
  module AttachmentFu # :nodoc:
    module Processors
      module Mp3Processor
        def self.included(base)
          base.send :extend, ClassMethods
          base.alias_method_chain :process_attachment, :processing
        end

        module ClassMethods
        end

      protected
        def process_attachment_with_processing
          self.convert_to_mp3
        end

        # Convert to mp3 and set some metadata
        def convert_to_mp3(options={})            
          #do the conversion with ffmpeg
          mp3_temp_path = "#{self.temp_path}.mp3"

          cmd = "ffmpeg -i #{self.temp_path} -metadata title=\"#{self.filename.gsub("\.wav","")}\" -metadata artist=\"Vip Studio Sessions\" -metadata album=\"Uploads\" -vn -ar 44100 -ac 2 -ab 320k -f mp3 #{mp3_temp_path}"

          `#{cmd}`

          #copy this file back into temp_data
          self.copy_to_temp_file(mp3_temp_path)

          #update attributes
          self.filename = self.filename.gsub(/\.wav$/,".mp3")
          self.content_type = "audio/mpeg"
          self.set_size_from_temp_path
        end
      end
    end
  end
end

所有转换的东西似乎都在工作,因为它在 tmp 文件夹中创建了一个新的 mp3 文件,文件名保存在mp3_temp_path 中,并在数据库中记录。但由于某种原因,生成的文件不会被推送到 s3。我有一种感觉,我只需要设置一些访问器来处理 temp_data 或 temp_file 什么的。这个我试过了

self.temp_path = mp3_temp_path

self.temp_data = File.read(mp3_temp_path)

self.temp_path = write_to_temp_file(File.read(mp3_temp_path))

目前,正如您在我的代码中看到的,我正在尝试这个:

self.copy_to_temp_file(mp3_temp_path)

但它们都不起作用。这些尝试是基于查看预先存在的处理器(例如 rmagick)并查看它们的功能。他们似乎和我做同样的事情,但由于他们都是关于缩略图的,所以很容易在翻译中丢失一些东西。

谁能看到我错过了什么?谢谢,马克斯

【问题讨论】:

  • 只是在黑暗中的一次野刺/随机观察...所有这些github.com/technoweenie/attachment_fu/tree/master/lib/… 都有一个with_image 类方法...你需要其中一个吗?
  • 嗯,我尝试添加一个类方法 with_image,它只返回true,没有变化。
  • 另外,with_image 在其他处理器中由process_attachment_with_processing 方法调用,我认为它没有什么特别之处。

标签: ruby-on-rails amazon-s3 ruby-on-rails-2 attachment-fu


【解决方案1】:

好的,回答我自己的问题 - 新的一天可以带来什么,真是太棒了。我突然想到更仔细地查看 attachment_fu 源代码,我注意到了这一点......

    def after_process_attachment
      if @saved_attachment
        if respond_to?(:process_attachment_with_processing) && thumbnailable? && !attachment_options[:thumbnails].blank? && parent_id.nil?
          temp_file = temp_path || create_temp_file
          attachment_options[:thumbnails].each { |suffix, size| create_or_update_thumbnail(temp_file, suffix, *size) }
        end
        save_to_storage
        @temp_paths.clear
        @saved_attachment = nil
        callback :after_attachment_saved
      end
    end

我没有将此实例变量@saved_attachment 设置为true(或truthy),因此save_to_storage 不会继续。它默默地失败了——记录仍然很高兴地保存到数据库中,它只是不存储文件。

总之,答案是,如果我对生成的 mp3 感到满意(即它最初是一个 mp3,或者我成功地将它转换为一个 mp3),那么我需要将 @saved_attachment = true 设置为我的处理器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2014-03-05
    • 2011-10-01
    相关资源
    最近更新 更多