Carreirwave - 如何直接从保存的文件名创建版本名称

Rob*_*Rob 9 ruby ruby-on-rails rmagick carrierwave ruby-on-rails-4

我正在为我的应用上传的文件创建一个拇指图像.图像名称中包含时间戳代码.当我运行recreate_versions拇指时,生成的图像也包含时间戳,但它使用当前时间戳,使得拇指图像名称与原始文件名不同.

所以我认为一个修复方法是为拇指图像设置一个自定义文件名.基本上有'thumb_'+'原始文件名'.

   version :thumb do
     process :resize_to_limit => [110, nil]

     def full_filename(for_file = model.image_value.file)
     'thumb_' + File.basename(model.image_value.path).to_s
     end
   end

  def filename(for_file = model.image_value.file)
   "#{model.id}" + "-v#{timestamp}" + "-" + "#{model.name}" + ".png" if original_filename.present?
  end

  def timestamp
    var = :"@#{mounted_as}_timestamp"
    model.instance_variable_get(var) or model.instance_variable_set(var, Time.now.to_i)
  end
Run Code Online (Sandbox Code Playgroud)

这似乎是一个简单的修复,但由于某些原因,当我运行recreate_versions拇指图像时,生成的当前时间戳记在其中,而不是原始文件名称中的时间戳.根据我的理解,它应该获取存储在DB中的值作为原始文件名并将其添加到'thumb_'的末尾.但不知何故,它改变了名称中的时间戳.

stored filename                      =       1-v1474175808-model-name.png
Item.find(1).image_value_url(:thumb) = thumb_1-v1474175808-model-name.png #this works correctly and looks for the correct filename
thumb filename saved                 = thumb_1-v1472111618-model-name.png #thumb saved is different. For some reason has a different timestamp in name
Run Code Online (Sandbox Code Playgroud)

我想也许def full_filename不会被运行,但是如果我将其更改为其他内容,则保存的拇指文件名将更改为其中的内容def full_filename.

不知道这里发生了什么.希望有人可以提供帮助.如果看起来它应该工作,请告诉我,至少这将澄清它可能是我不看的东西.

Rob*_*Rob 1

最终不得不使用生成的 url 并slice!在 url 上使用仅保留图像名称。

   version :thumb do
     process :resize_to_limit => [110, nil]

    def full_filename(for_file = model.image_value.file)
      raw_file_name = model.image_value.slice!(0..65)
     'thumb_' + raw_file_name
     end

  end
Run Code Online (Sandbox Code Playgroud)