Wednesday, December 22, 2010

Filename rename in File Column


File column is very useful plugin to upload multi sizes of images. Currently in file column plugin we are not able to provide file name.
I found number of forums which are facing this issue but not able to find the answer.
I found the solution which required to customize the file column plugin.
Add option filename in file column method.
1
2
3
4
T= Time.now
file_column :uploaded_data, :magick => {
:versions => { "thumb" => "150x150", "medium" => "640x480>" }
},:filename => "#{T.strftime("%Y%m%d%H%M")}"
Initialized filename option in FileColumn.rb file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def self.init_options(defaults, model, attr)
options = defaults.dup
options[:store_dir] ||= File.join(options[:root_path], model, attr)
options[:filename]||=nil
unless options[:store_dir].is_a?(Symbol)
options[:tmp_base_dir] ||= File.join(options[:store_dir], "tmp")
end
options[:base_url] ||= options[:web_root] + File.join(model, attr)
 
[:store_dir, :tmp_base_dir].each do |dir_sym|
if options[dir_sym].is_a?(String) and !File.exists?(options[dir_sym])
FileUtils.mkpath(options[dir_sym])
end
end
 
options
end
Assign filename to temp image path. Update code @class TempUploadedFile store_upload method line number 219
1
@filename = options[:filename] || FileColumn::sanitize_filename(file.original_filename)
Now test your code and get the uploaded image new file name.

1 comment:

  1. Your code works very well but Rails cache the filename because of "config.cache_classes = true" . So the filename is the same for my images. How to disable cache for the filename ?

    ReplyDelete