1

目前,当我使用神社上传器存储任何图像时,我收到了网址:-

/uploads/store/90bcb5a78ed5de16a6c62eea1fb80ed1.png

但我希望 url 显示如下:-

/uploads/store/original_image_name.png

有什么解决办法

以供参考:-

初始化器/shrine.rb

require "shrine/storage/file_system"
  Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
    store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store")
  }

模型/image_uploader.rb

class ImageUploader < Shrine
  plugin :derivatives
  plugin :url_options, store: { host: "www.cloudfair.com" }
  Attacher.derivatives_processor do |original|
    processor = ImageProcessing::MiniMagick.source(original)
    {
      large:  processor.resize_to_limit!(1280, 800),
      medium: processor.resize_to_limit!(600, 600),
      small:  processor.resize_to_limit!(400, 400),
    }
  end
end

上传图片:-

photo = Photo.new
photo.image = params[:image_file]
photo.image_derivative!
photo.save()

访问图像 URL:-

photo.image(:small).url
4

1 回答 1

1

你可以在你的上传文件中使用这样的东西

def filename
  if original_filename
    "#{model.original_filename}.#{file.extension}"
  end
end
于 2020-06-02T09:00:24.413 回答