本文介绍了在使用Carrierwave保存之前修改文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传器,在其中实现了#filename方法以获取自定义且唯一的文件名,但是在保存文件之前,该方法似乎被忽略了(我正在使用fog gem上传到机架空间)

I've an uploader where I've implement the #filename method to get a custom and unique filename but it looks like this method is ignored before saving the file (I'm uploading to rackspace with the fog gem)

这里是上传器:

class MyImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  ...

  def filename
    if original_filename.present?
      "#{secure_token}.#{file.path.split('.').last.downcase}"
    else
      super
    end
  end

  ...

  private

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end
end

这是控制台中的一项测试(我正在测试一个大文件名的问题,我认为这是使用自定义#filename方法解决的):

And here is a test in the console (I'm testing an issue with large filenames that I thought was solved with the custom #filename method):

1.9.3-p392 :002 > f = File.open('/Users/myuser/Desktop/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.png')
 => #<File:/Users/myuser/Desktop/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.png>
1.9.3-p392 :003 > my_model_instance.image = f
Errno::ENAMETOOLONG: File name too long - /Users/myuser/app/tmp/uploads/20130509-1723-16769-8458/thumb_lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat.png
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1371:in `initialize'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1371:in `open'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1371:in `block in copy_file'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1370:in `open'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1370:in `copy_file'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:477:in `copy_file'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:396:in `block in cp'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1515:in `block in fu_each_src_dest'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1531:in `fu_each_src_dest0'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1513:in `fu_each_src_dest'
  from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:395:in `cp'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:205:in `copy_to'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:121:in `block in cache!'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:112:in `cache!'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:231:in `block in cache_versions!'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:216:in `each'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:216:in `cache_versions!'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18:in `block in with_callbacks'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18:in `each'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18:in `with_callbacks'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:112:in `cache!'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/mount.rb:315:in `cache'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/mount.rb:173:in `image='
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/bundler/gems/carrierwave-mongoid-28a9b718d42b/lib/carrierwave/mongoid.rb:39:in `image='

所以我认为我需要先修改文件名保存该文件以使自定义文件名完全起作用,因为实际上只有该文件在上载到Rackspace之后才被重命名(仅当文件名不是很大时)

So I think that I need to modify the filename before being saved in order to get the custom filename fully working because actually the file only is rename after being uploaded to Rackspace (only if the filename it's not so large)

推荐答案

在缓存步骤(本地发生)而不是保存步骤(在雾中发生)中,上传正在爆炸。阅读,它看起来像carrierwave有一个您可以重写的方法,可以做到这一点:full_original_filename

The upload is exploding during the caching step (which happens locally) rather than the saving step (which happens in fog). Reading the source, it looks like carrierwave has a method you can override to do just this: full_original_filename

如果您要解决的问题是缩短缓存的文件名,则可以使用以下方法。如果以后要更改该文件名,则需要使用。

If the problem you're trying to solve is to make the cached filename shorter, the following may work. If you want to change that filename later, you'll need to use the official solution for that in addition.

MAX_FILENAME_LENGTH = 64 # arbitrary limit, your milage may vary

def full_original_filename
  filename = super

  return filename if !filename.present?

  extension = File.extname(filename)
  basename = File.basename(filename, extension)[0...MAX_FILENAME_LENGTH]
  basename + extension
end

这篇关于在使用Carrierwave保存之前修改文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:47