ruby-on-rails – Carrierwave – 如果图像未上传,则保存为零
我正在尝试将图像从本地文件系统迁移到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内部,它返回的正常文件类(已清理文件)通过检查文件是否存在来响应空:
https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/sanitized_file.rb#L144 在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 它可能有用吗? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |