关于ruby:不同版本的不同文件扩展名

Different file extensions for different versions

我正在使用载波上传图像。我需要主映像版本
保留其原始格式,但要转换其他版本
转换为gif。

此刻我正在做这样的事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 def filename
   change_ext_to_gif(super)
 end

 def change_ext_to_gif(ext)
   ext.chomp(File.extname(ext)) +".gif"
 end

 version :preview do
   process :resize_to_fill => [60, 60]
   process :convert =>"gif"
 end

 version :full do
   process :resize_to_limit => [320, 320]
   process :convert =>"gif"
 end

 version :mobile do
   process :resize_to_limit => [72, 96]
   process :convert =>"gif"
 end

当然,这也会更改我原始文件的扩展名。是
有什么办法解决这个问题?我想我需要重写一些方法
在版本的块中。但是我无法弄清楚它们(我
尝试覆盖文件名和网址,这有帮助,但阻止了版本
文件被删除)。


您可以像这样使用每个版本的文件名:

1
2
3
4
5
6
7
 version :mobile do
   process :resize_to_limit => [72, 96]
   process :convert =>"gif"
   def full_filename(for_file = model.logo.file)
    "fiename here"
   end
 end

因此,只需保留您想要的原始文件名,然后根据版本进行更改即可。 Wiki上还有其他示例:

https://github.com/jnicklas/carrierwave/wiki/操作方法:-Move-version-name-to-end-of-filename,-instead-of-front