Rails、CarrierWave 和 Fog - 销毁或覆盖时忽略丢失的文件

发布于 2024-12-03 22:24:56 字数 215 浏览 0 评论 0原文

当缺少附件/图像时,Rails w CarrierWave 使用 Fog 删除/销毁记录的正确方法是什么?

在缺少将图像导入到 RackSpace 后,我正在尝试清理一些记录。有一些缺失的图像和拇指。当我尝试删除记录时,出现错误

Fog::Storage::Rackspace::NotFound

是否有 CarrierWave 或 Fog 设置使其更能容忍此类情况?

What is the correct way in Rails w CarrierWave using Fog to delete/destroy records when there are missing attachments/images?

I am trying to clean up a few records after a missing import of images to RackSpace. There are a few missing images and thumbs. When I try to delete a record I get a error

Fog::Storage::Rackspace::NotFound

Is there a CarrierWave or Fog setting to make it more tolerant to these kinds of scenarios?

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

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

发布评论

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

评论(1

苍暮颜 2024-12-10 22:24:56

我刚刚遇到这个问题,发现原始问题在这里提交: https://github.com/jnicklas /rierwave/issues/481 和描述修复的 wiki 页面:https://github .com/jnicklas/rierwave/wiki/How-To%3A-Silently-ignore-missing-files-on-destroy-or-overwrite

但是我对解决方案并不满意,我没有想要将这两种方法添加到我所有使用上传器的模型中。我倾向于编写 1 个基本上传器和子类,以满足特定需求的任何更改。所以我深入研究了这些方法:remove_#{column_name}!和remove_previously_stored_#{column_name}并在这里找到主题:https: //github.com/jnicklas/rierwave/blob/master/lib/rierwave/mount.rb#L204https://github.com/jnicklas/rierwave/blob/master /lib/rierwave/mount.rb#L204

这两个方法都只是调用remove!在上传器上。所以解决这个问题最简单的方法就是覆盖删除!上传器中的方法。那么你只需要在 1 个地方重写一个方法。我的覆盖如下所示:

class CloudfilesUploader < CarrierWave::Uploader::Base
  # Override to silently ignore trying to remove missing previous file
  def remove!
    begin
      super
    rescue Fog::Storage::Rackspace::NotFound
    end
  end
end

这应该可以解决您尝试重新上传图像并覆盖不存在的图像或尝试删除不存在的图像时的问题。

〜汤姆

I just ran into this issue and found the original issue filed here: https://github.com/jnicklas/carrierwave/issues/481 and the wiki page describing the fix here: https://github.com/jnicklas/carrierwave/wiki/How-To%3A-Silently-ignore-missing-files-on-destroy-or-overwrite

However I wasn't happy with the solution, I didn't want to have to add those 2 methods into all of my models that use an uploader. I tend to write 1 base uploader and subclass that for any changes to specific needs. So I dug into those methods: remove_#{column_name}! and remove_previously_stored_#{column_name} and found theme here: https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/mount.rb#L204 and https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/mount.rb#L204

Both of these methods just call remove! on the uploader. So the easiest way to fix the issue is to override the remove! method in the uploader. Then you only need to override one method and in 1 place. My overrride looks like the following:

class CloudfilesUploader < CarrierWave::Uploader::Base
  # Override to silently ignore trying to remove missing previous file
  def remove!
    begin
      super
    rescue Fog::Storage::Rackspace::NotFound
    end
  end
end

That should solve your problems when trying to re-upload an image and overwrite an image that doesn't exist or when you just try and delete an image that doesn't exist.

~ Tom

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