使用回形针保存文件系统和S3上的附件

时间:2012-03-13 17:39:41

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

我正在使用wav-mp3的变体作为我的回形针处理器,我需要能够在本地和远程保存附件。这是处理器:

module Paperclip
    class Ffmpeg < Processor

    attr_accessor :file, :params, :format

    def initialize file, options = {}, attachment = nil
      super
      @file           = file
      @params         = options[:params]
      @current_format = File.extname(@file.path)
      @basename       = File.basename(@file.path, @current_format)
      @format         = options[:format]
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
      begin
        parameters = []
        parameters << @params
        parameters << ":source"
        parameters << ":dest"
        parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
        cmd = "-y -i #{File.expand_path(src.path)} #{File.expand_path(dst.path)} -acodec mp3"
        success = Paperclip.run("ffmpeg", cmd)
        # This was an idea but I don't know how to access the Tempfile's name from the controller
        FileUtils.copy_file(File.expand_path(dst.path), "#{Rails.root}/tmp/#{File.basename(src.path)}" )
      rescue PaperclipCommandLineError => e
        raise PaperclipError, "There was an error converting #{@basename} to mp3"
      end
      dst
    end
  end
end

你可以看到我正在使用FileUtils.copy_file在本地保存它,但我不知道如何将文件名传回控制器。

这是我的模特:

class AudioFile < ActiveRecord::Base
  establish_connection "audio_file"

  has_attached_file :wav,
                    :styles => {
                       :mp3 => {
                         #:params => "-encode --scale 1 --vbr-new -V7",
                         #:params => "-i
                         :format => "mp3" }
                    },
                    :default_style => :normal,
                    :processors => [:ffmpeg],
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root}/config/s3.yml",
                    :path => "/call_recordings/:filename"

  after_post_process :get_duration

  def get_duration
    # This what I'm ultimately trying to do:
    #self.duration = `mp3info -p "%S" #{File.expand_path(dst.path)}`
  end
end

最终我试图将mp3的持续时间放入模型中,但我不确定如何在控制器中使用该文件的本地副本执行此操作,然后删除该文件。

对任何建议开放:)

0 个答案:

没有答案