Sunday, September 24, 2006

Ruby file upload

OK - An update. I got a suggestion from a friend (after my upload post) to try file_column. I tried it and it almost worked perfectly. Much nicer for my uses.

The one problem - the files are stored in directory per image_id - one image per directory. This was overkill for my needs, I wanted to store the images in a directory per project_id. To do this, I had to make some manual changes to file_column.rb and my environment.rb files. I'll do my best to post what I did here...

On lines 359 and 361 in file_column.rb, the original code would delete an existing directory and move over the temporary directory to replace it (after a successful download). I modified it to simply move the file over, make the directory if it needs to.

FileUtils.mkpath(@dir) unless File.exists?(@dir) File.join(local_dir, @filename), File.join(@dir, @filename)

On line 395 the code deletes the directory, you must change the delete_files method to be

FileUtils.rm_rf File.join(@dir, @filename)

So only the particular file is deleted.

In my environment.rb file, I overrode the ___ method so the directory name would be based on the user's project.

require 'file_column'
require 'file_column_helper'
require 'rails_file_column'

module FileColumn
class PermanentUploadedFile

def relative_path_prefix
project = Project.find(@instance.project_id)


No comments:

Post a Comment