我正在尝试将图像从本地文件系统迁移到DropBox,所以我使用carrierwave dropBox gem将所有图像移动到dropBox.我能够存储从我的应用程序上传的新图像.我试图移动现有的图像.
我正在使用Article.first.avatar?检查图像是否存在的方法,我已经在很多地方使用这种方法来处理我的应用程序中不同大小的图像.
当我使用上述方法来确定图像是否存在时,它总是在DropBox中不存在图像时显示为true.看看我的控制台输出(2),
我的上传者:
class Avatar < CarrierWave::Uploader::Base include CarrierWave::MiniMagick storage: file def store_dir if model "uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}" else "uploads/#{mounted_as}/" end end end
控制台输出(1)
>Article.first.avatar? >false #<AvatarUploader:0x007f9813f1fe70 @file= #<CarrierWave::SanitizedFile:0x007f9813f1e688 @content_type=nil,@file="/Users/work/project/app1/public/uploads/370/avatar/avatar.png",@original_filename=nil>,@model= ##Article model @mounted_as=:avatar,@storage=#<CarrierWave::Storage::File:0x007f9813f1fad8 @uploader=#<AvatarUploader:0x007f9813f1fe70 ...>>,@versions={}>
我更改了上传器如下:
class Avatar < CarrierWave::Uploader::Base include CarrierWave::MiniMagick storage: dropBox def store_dir if model "uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}" else "uploads/#{mounted_as}/" end end end
控制台输出(2)
>Article.first.avatar? >true #<AvatarUploader:0x007f8574143ee8 @file= #<CarrierWave::Storage::DropBox::File:0x007f8574143308 @client= #<DropBoxClient:0x007f8574143420 @root="dropBox",@session= #<DropBoxSession:0x007f8574143498 @access_token=#<OAuthToken:0x007f8574143470 @key="123453333",@secret="22222222222">,@consumer_key="abcdeafs",@consumer_secret="asdfasfj",@locale=nil,@request_token=nil>>,@config= {:app_key=>"asdfasfasf",:app_secret=>"asdfkasfksf",:access_token=>"adfkjasfkhs",:access_token_secret=>"aksdfkhsfksf",:access_type=>"dropBox",:user_id=>"292929292"},@path="uploads/images/370/avatar.png",@uploader=#<AvatarUploader:0x007f8574143ee8 ...>>,@model= #Artcle Model>,@mounted_as=:image,@storage= #<CarrierWave::Storage::DropBox:0x007f8574143c90 @config= {:app_key=>"asdfasfasf",@dropBox_client= #<DropBoxClient:0x007f8574143420 @root="dropBox",@session=
解决方法
我在黑暗中刺穿的是Carrierwave内部,它返回的正常文件类(已清理文件)通过检查文件是否存在来响应空:
在carrierwave dropBox gem中,File类(https://github.com/robin850/carrierwave-dropbox/blob/master/lib/carrierwave/storage/dropbox.rb#L45)没有实现这一点.我猜你是否重新开课并补充说
module CarrierWave module Storage class DropBox < Abstract class File def empty? # use dropBox API to get the file and check the presence here end end end end end
它可能有用吗?