Gem carrierwave:清空 public/uploads/tmp 后,再上传文件不会在存储到这里了??

kee · 2014年02月16日 · 最后由 kee 回复于 2014年02月18日 · 3714 次阅读

By default, CarrierWave copies an uploaded file twice, first copying the file into the cache, then copying the file into the store.

清空了 public/uploads/tmp,再上传图片,为什么这里不出现了 (first copying the file into the cache)??代码没动过

应该 carrierwave 首先缓存在 public/uploads/tmp,然后会转存在 public/uploads/[class_name_in_dash] 中。(前提是你用缺省配置)

By default, CarrierWave copies an uploaded file twice, first copying the file into the cache, then copying the file into the store.

@ericguo 这里说的就是啊,我没动过 code,没设置过

def move_to_cache
  true
end

我就是把 public/uploads/tmp 清空了

#2 楼 @kee 它是不是自动清除了缓存?

@Martin91 应该不是,几天前的我才刚清掉,没理由刚上传的 tmp 里就没有吧?

#4 楼 @kee 读一下代码,看它是怎么操作缓存的咯

def cache!(new_file) 
107         new_file = CarrierWave::SanitizedFile.new(new_file)                                                                
108   
109         unless new_file.empty?
110           raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form                                
111   
112           with_callbacks(:cache, new_file) do
113             self.cache_id = CarrierWave.generate_cache_id unless cache_id                                                  
114   
115             @filename = new_file.filename   
116             self.original_filename = new_file.filename                                                                     
117   
118             if move_to_cache
119               debugger   #调试进到这里,但cache_path = true,这里是否应该是路径str?  
120               @file = new_file.move_to(cache_path, permissions)
121             else           

#6 楼 @kee 看代码,cache_path 应该是一个路径才对。 https://github.com/carrierwaveuploader/carrierwave/blob/357f71e0797764a1760ce7a12c40ef19780ce6ce/lib/carrierwave/uploader/cache.rb#L158

def cache_path
  File.expand_path(File.join(cache_dir, cache_name), root)
end

#7 楼 @Martin91 cache_dir 是可配置项。你可以指定把上传文件时临时文件放在哪里。因为上传的时候,不可能直接往 server 放,要做一个缓冲区。

By default, CarrierWave copies an uploaded file twice, first copying the file into the cache, then copying the file into the store. For large files, this can be prohibitively time consuming.

这里写 copy twice,我上传时注意了下 public/photos/tmp 目录,显示生成了 cache 然后传到 server 接着 cache 删除了(是 move 吧?)

我在 uploader 里添加

def move_to_cache
    true
  end
  def move_to_store
    true
  end

cache 就保留下来了,但是 wiki 里是这样

When the move_to_cache and/or move_to_store methods return true, files will be moved (instead of copied) to the cache and store respectively.

@ruby_sky 如果默认是 copy 那 cache 不应改清掉,设置 move_to = true 反而表面上让我感觉是 copy? 请简单描述下 CarrierWave 上传流程(文件 -》cache -》server?) cc @Martin91

https://github.com/carrierwaveuploader/carrierwave 有关 Large files 段(上边引用的),我是否理解错了?? 改为 move_to = true,会否增加上传耗时?

def move_to_cache
    true
  end
  def move_to_store
    true
  end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号