Dealing with user uploaded files between deployments
If you are writing an application that allows your users to upload files, you have to work around the fact that their files will be deleted every time you deploy your application.
Usually if you want your file to available they will be uploaded to the public folder. The solution is to store those files in the “shared” directory.
Here is one example on how to set that up for a Rails app.
Create a directory called “uploads” in your.
When using attachment_fu or similar, use a :prefix=>”/public/uploads/#{tablename}
Add the following tasks to your deploy file
desc "link upload directory"
task :symlink_uploads, :roles => :app do
run "ln -nfs /var/www/apps//containers/rails//shared/system/uploads /public/uploads"
end
end
desc "create upload directory"task :create_uploads_directory, :roles => :app do
run "mkdir #{deploy_to}/shared/system/media"
end
end
Hook them into your deployment
after "deploy","symlink_uploads"
after "deploy:setup","create_uploads_directory"
Any uploads will now be stored outside the versioned directories.
January 23rd, 2009 at 10:20 pm
While this works, of course, I personally like to deal with uploaded files by placing them in a completely separate directory outside of the app structure and set it up on a subdomain with vhosts. Create files.domain.com which points to ~/web/public/userfiles
The caveat with that method (which I don’t really find it to be much of an issue) is when when developing your directory structure for that will probably be different on your local machine, but I just do something like:
RAILS["ENV"] == “production” ? “/path/to/production” : “/path/to/development”