gpt4 book ai didi

ruby-on-rails - 载波:将版本名称移到文件名的末尾,而不是前

转载 作者:行者123 更新时间:2023-12-04 04:37:11 49 4
gpt4 key购买 nike

当前在Carrierwave中,在创建像这样的不同版本时上传了foo.png这样的文件之后:

class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"#{model.class.to_s.underscore}/#{model.id}"
end

version :thumb do
process :resize_to_fit => [500, 500]
end
end

导致文件上传为:
thumb_foo.png
foo.png

由于SEO的原因,我想将“拇指”移动到文件名的末尾。根据此处的 docs,我添加了:
  def full_filename(for_file)
if parent_name = super(for_file)
extension = File.extname(parent_name)
base_name = parent_name.chomp(extension)
[base_name, version_name].compact.join("_") + extension
end
end

def full_original_filename
parent_name = super
extension = File.extname(parent_name)
base_name = parent_name.chomp(extension)
[base_name, version_name].compact.join("_") + extension
end

文档说这应该导致:
foo_thumb.png
foo.png

但是,我最终得到以下结果:
thumb_foo_thumb.png
foo.png

知道我在做什么错吗?

最佳答案

只需在#full_filename块下使用version即可:

class AvatarUploaer < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick

storage :file

version :thumb do
process resize_to_fill: [50, 50]

def full_filename(for_file = model.logo.file)
parts = for_file.split('.')
extension = parts[-1]
name = parts[0...-1].join('.')
"#{name}_#{version_name}.#{extension}"
end
end
end

结果如下:
/Users/user/app/uploads/1x1.gif
/Users/user/app/uploads/1x1_thumb.gif
  • 在Wiki中的更多信息:How to: Migrate from one model to another
  • MVP示例:https://gist.github.com/itsNikolay/2394f84f31db33d4c3dc6634068b0259
  • 关于ruby-on-rails - 载波:将版本名称移到文件名的末尾,而不是前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457132/

    49 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com