nowa / carrierwave-upyun

UpYun storage for CarrierWave
161 stars 27 forks source link

Sometimes the old uploaded files will lost #27

Closed huacnlee closed 9 years ago

huacnlee commented 10 years ago

场景:

最早搞 IMAX.im 的时候就会遇到,到现在我依然没查到原因在哪里,现在我的另外一个项目里面再次遇到。

目前我的猜测

class User
  mount_uploader :avatar, AvatarUploader
end
  1. 我在怀疑是否有可能用户在修改数据(非修改 :avatar 字段),遇到 Bug,无意中触发了 Carrierwave 删除旧图片的功能?
  2. 用户实际上是在修改 :avatar,Carrierwave 上传成功,并删除了旧图片。接着程序挂了,或执行步骤异常中断了,新的图片地址没有存入数据库。

第二个情况似乎比较容易发生。

huacnlee commented 10 years ago
after_save :store_avatar!
before_save :write_avatar_identifier
after_commit :remove_avatar! :on => :destroy
before_update :store_previous_model_for_avatar
after_save :remove_previously_stored_avatar

If you want to skip any of these callbacks (eg. you want to keep the existing avatar, even after uploading a new one), you can use ActiveRecord’s skip_callback method.

class User
  mount_uploader :avatar, AvatarUploader
  skip_callback :save, :after, :remove_previously_stored_avatar
end
huacnlee commented 10 years ago

目前的 remove_previously_stored_avatar 是挂在 after_save 上面的,如果有 Transaction 的情况可能会出现误删除了,但数据还没保存的情况。

huacnlee commented 10 years ago

似乎我已经找到问题原因了,已经给 Carrierwave 提交 Pull Request

https://github.com/carrierwaveuploader/carrierwave/pull/1447

hzlu commented 9 years ago

model update和destroy之后,又拍云上的图片没有被回调删除,model里面没有skip_callback,请问怎么处理这个问题