6

我在 rails 3 应用程序上使用carrierwave 和 mongoid,并且遇到了 after_save 回调的问题。考虑以下

class Video
  include Mongoid::Document

  field :name  

  mount_uploader :file, VideoUploader

  after_create :enqueue_for_encoding

  protected

  def enqueue_for_encoding
     // point your encoding service to where it expects the permanent file to reside
     // in my case on s3 
  end

end

我的问题是,在我的enqueue_for_encoding方法中,file.url 指向本地 tmp 目录而不是 s3 目录。

enqueue_for_encoding当 file.url 指向 s3 时,如何调用我的方法?

谢谢!

乔纳森

4

4 回答 4

2

查看carrierwave关于回调的howto页面

https://github.com/jnicklas/carrierwave/wiki/How-to%3A-use-callbacks

它对我有用

于 2012-01-04T11:50:50.753 回答
1

好吧,我想通了。采取了一些黑客攻击。因此,目前carrierwave 没有公开 after_create 挂钩,所有这些都在 after_save 回调中持续存在和处理。这是我用来解决它的代码:

# Video.rb

  mount_uploader :file, VideoUploader

  # overwrite the file setting to flag the model that we are creating rather than saving
  def file=(obj)
    @new_file = true
    super(obj)
  end

  # chain the store_file! method to enqueue_for_encoding after storing the file AND
  # if the file is new
  alias_method :orig_store_file!, :store_file!
  def store_file!
    orig_store_file!
    if @new_file #means dirty
      @new_file = false
      enqueue_for_encoding
    end
    true
  end

更新

糟糕——那没用。它几乎做到了——网址是正确的,但它被永久解雇了。这意味着文件仍在加载过程中,并且在调用 enqueue_for_encoding 时未完全存储

于 2011-03-21T14:40:47.160 回答
1

可以enqueue_for_encoding在上传器本身上设置回调。但我更喜欢这样做:

class Video
  # mount the uploader first:
  mount_uploader :file, VideoUploader
  # then add the callback:
  after_save :enqueue_for_encoding, on: :create
end
于 2017-06-03T05:23:46.263 回答
0

您可以尝试删除after_create模型中的回调并将以下内容添加到您的上传器:

# video_uploader.rb

process :encode

def encode
  model.enqueue_for_encoding
end

保存文件后process调用回调(我认为),这应该允许您在文件在 S3 上启动后挂接。

于 2011-03-19T23:06:56.973 回答