2015-04-20 25 views
8

我想將圖像從本地文件系統遷移到保管箱,所以我使用carrierwave dropbox gem將所有圖像移動到保管箱。我能夠存儲從我的應用程序上傳的新圖像。我正試圖移動現有的圖像。Carrierwave - 保存爲零,如果圖像沒有上傳

我正在使用Article.first.avatar?方法來檢查圖像是否存在,我已經在很多地方用這種方法來處理我的應用程序中不同大小的圖像。

當我使用上面的方法來確定圖像是否存在時,它總是表示圖像不存在於保存箱中時爲真。看看我的控制檯輸出(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", 
    :app_secret=>"asdfkasfksf", 
    :access_token=>"adfkjasfkhs", 
    :access_token_secret=>"aksdfkhsfksf", 
    :access_type=>"dropbox", 
    :user_id=>"292929292"}, 
    @dropbox_client= 
    #<DropboxClient:0x007f8574143420 
    @root="dropbox", 
    @session= 
  1. 爲什麼當圖像不存在時顯示「true」。
  2. 我怎樣才能得到「假」圖像不存在於保管箱。
+0

你能描述一個更具體的問題嗎?如何不上傳圖片? – RockStar

+0

@RockStar更新了我的問題。 – Mano

回答

0

我在黑暗中刺是裏面Carrierwave,它返回正常的文件類(消毒文件)響應通過檢查文件是否存在空:

https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/sanitized_file.rb#L144

裏面的carrierwave Dropbox的寶石,文件類(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 

它可能會工作?

+0

我在gem中添加了上述方法,並通過返回「false」進行測試,但它總是返回「true」。空虛嗎? 返回false 結束 – Mano

相關問題