Ruby on rails 轨道&x2B;Carrierwave:如何更改图像版本(thumb、small等)的默认文件命名?
我正在将我的项目从带有附件的Rails 2升级到带有carrierwave的Rails 3。我有一个现有的数据库和文件目录,其中包含使用附件上传的图像。问题是,在附件中,上传的图像将图像版本(thumb、small、medium等)附加到文件名的后面(Ruby on rails 轨道&x2B;Carrierwave:如何更改图像版本(thumb、small等)的默认文件命名?,ruby-on-rails,Ruby On Rails,我正在将我的项目从带有附件的Rails 2升级到带有carrierwave的Rails 3。我有一个现有的数据库和文件目录,其中包含使用附件上传的图像。问题是,在附件中,上传的图像将图像版本(thumb、small、medium等)附加到文件名的后面(my\u image\u small.jpg),而在carrierwave中,默认情况是相反的(small\u my\u image.jpg)。在carrierwave的什么地方我可以改变这个 这是我的文件上传器: # encoding: utf-
my\u image\u small.jpg
),而在carrierwave中,默认情况是相反的(small\u my\u image.jpg
)。在carrierwave的什么地方我可以改变这个
这是我的文件上传器:
# encoding: utf-8
class FileUploader < CarrierWave::Uploader::Base
#include UploaderFu
# Include RMagick or MiniMagick support:
SMALL_WIDTH = 101
MEDIUM_WIDTH = 223
LARGE_WIDTH = 345
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
# 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}"
"#{Rails.root}/public/file_uploads/#{("%08d" % model.id).scan(/..../).join('/')}"
#{}"#{Rails.root}/public/file_uploads/"
end
version :small do
process :resize_to_fit => [SMALL_WIDTH, 10000]
end
version :medium do
process :resize_to_fit => [MEDIUM_WIDTH, 10000]
end
version :large do
process :resize_to_fit => [LARGE_WIDTH, 10000]
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.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 :thumb do
# process :scale => [50, 50]
# 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
end
编码:utf-8
类FileUploader要覆盖特定版本的文件名约定,您可以在版本块中定义一个
完整文件名
方法:
version :small do
def full_filename(for_file)
[super(for_file), version_name].compact.join('_')
end
process :resize_to_fit => [SMALL_WIDTH, 10000]
end
此方法只是对CarrierWave源代码中现有的full_filename
方法的重写(请参见versions.rb
)
但是,您需要在每个版本上覆盖此选项。相反,您可能希望通过修补Versions
模块来更改CarrierWave的默认行为。您可以将其添加为初始值设定项:
module CarrierWave
module Uploader
module Versions
def full_filename(for_file)
[super(for_file), version_name].compact.join('_')
end
end
end
end
更新:
要更正文件扩展名的问题,可以使用Rake的pathmap
函数解析文件名:
require 'rake'
def full_filename(for_file)
filename = for_file.pathmap("%n")
extension = for_file.pathmap("%x")
[filename, version_name].compact.join('_') + extension
end
为了防止有人想知道我是如何理解的,我查看了gem代码,发现文件名是由
def full_filename
设置的,所以在我的文件上传程序中,我重写了该方法,以便它覆盖该行为
def full_filename (for_file = model.filename.file)
"#{for_file[/(.+)\./][$1]}_#{version_name}.#{for_file[/\.(.+)/][$1]}"
end
嘿,扎克,我实际上没有看到你的答案,我试过了,但是像那样切换version\u name和super(for\u file)的问题是文件名变成了
my\u image.jpg\u small
看看我的答案,有点黑,但我不得不使用正则表达式将文件名分为.jpg之前和.jpg之后的所有内容,并将版本名称粘贴在两者之间。我没有想到这一点(我写这篇文章时没有测试它-对不起)。我已经用另一个不需要你使用正则表达式的选项更新了答案。它工作得很好,谢谢!我认为这比我的正则表达式要可靠得多。我使用的是Carrierwave gem,当我下载文件时,如果文件名包含空格,它就会转换成下划线。那么如何在下载文件时从文件名中删除下划线呢?我使用的是Carrierwave gem,当我下载文件时,如果该文件名包含空格,则会将其转换为下划线。那么如何在下载文件时从文件名中删除下划线呢?我使用的是Carrierwave gem,当我下载文件时,如果该文件名包含空格,则会将其转换为下划线。那么,如何在下载文件时从文件名中删除下划线呢?