载波和 mongoid 的回调问题

发布于 2024-10-23 00:03:13 字数 581 浏览 1 评论 0原文

我在 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 目录。

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

谢谢!

乔纳森

I am using carrierwave and mongoid on a rails 3 application and am having an issue with an after_save callback. Consider the following

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

My issue is that in my enqueue_for_encoding method, file.url points to the local tmp directory not the s3 directory.

How do I get my enqueue_for_encoding method to be called when file.url points to s3?

Thanks!

Jonathan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无语# 2024-10-30 00:03:13

查看有关回调的 Carrierwave 操作指南页面

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

它对我有用

Check out carrierwave's howto page on Callbacks

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

It worked for me

黑色毁心梦 2024-10-30 00:03:13

好吧,我明白了。需要进行一些黑客攻击。因此,目前 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 时尚未完全存储

Okay, I figured it out. To took a bit of hacking. So currently carrierwave does not expose an after_create hook, all of it persisting and processing happens in the after_save callback. Here is the code I used to work around it:

# 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

UPDATE

Woops -- that didn't work. It almost did -- the url is correct, but it is being fired permanently. Meaning the file is still in process of being loaded, and is not fully stored when enqueue_for_encoding is called

缱倦旧时光 2024-10-30 00:03:13

可以在上传器本身上设置 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

It is possible to set your enqueue_for_encoding callback on the uploader itself. But I prefer to do it this way:

class Video
  # mount the uploader first:
  mount_uploader :file, VideoUploader
  # then add the callback:
  after_save :enqueue_for_encoding, on: :create
end
叫嚣ゝ 2024-10-30 00:03:13

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

# video_uploader.rb

process :encode

def encode
  model.enqueue_for_encoding
end

process 回调在文件保存后调用(我认为)这应该允许您在文件上传到 S3 后挂载。

You could try removing your after_create callback in the model and add the following to your uploader:

# video_uploader.rb

process :encode

def encode
  model.enqueue_for_encoding
end

The process callbacks are called after the file is saved (I think) which should allow you to hook in once your file is up on S3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文