我整天都在搜索如何不将原始文件保存在 Amazon S3 的服务器上。每次,我的代码都会生成相同图像的 3 个版本。no_version.png、thumb.png 和 original.png。
我查看了 StackOverflow 并尝试使用此代码:
after :store, :unlink_original
def unlink_original(file)
File.delete if version_name.blank?
end
但是,它仍然产生 3 个版本。
我还尝试了使用路径的变体:
after :store, :unlink_original
def unlink_original(file)
File.delete path if version_name.blank?
end
但是,这会产生错误:没有这样的文件或目录 - uploads/L4l8n.jpg
这是我的 ImageUploader。任何帮助是极大的赞赏。
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# include CarrierWaveDirect::Uploader
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
after :store, :unlink_original
def unlink_original(file)
File.delete if version_name.blank?
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
# def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
# end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
asset_path("fallback/" + [version_name, "default_link.png"].compact.join('_'))
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :original, :if => :is_file_upload?
# process :resize_to_limit => [200, 200], :if => :is_not_file_upload?
# Default version always saves thumbs
version :thumb do
process :resize_to_limit => [200, 200]
process :convert => 'png'
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
protected
def is_file_upload? picture
model.remote_image_url.blank?
end
# def is_not_file_upload? picture
# not model.remote_image_url.blank?
# end
end