我一直在搜索如何不保存原始文件在亚马逊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
然而,这将产生错误:没有这样的文件或目录- 上传/ 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
也许与此相关的SO问题的图像:http://stackoverflow.com/questions/8579425/how-can-i- make-carrierwave-not-save-the-original-file-after-versions-are-process – Arkan
@Arkan嘿我尝试了建议的解决方案,并且我刚刚为# –
Dasun