Have you considered storing the path to the files in the database?
e.g. have a table for uploads, and have uploads belongs_to users, and
users has_many uploads. in the create action for your upload
controller, save the files in a user's directory somewhere, and then
store the path to the database.
--
Utah Ruby Users Group
ur...@googlegroups.com
http://groups.google.com/group/urug/
- All meeting times and places can be found here.
** Please suffix your subject with "[JOB]" if your message is about job opportunities.
On Dec 19, 1:57 pm, AJ ONeal <Alvin.ON...@gmail.com> wrote:
> It seems that Rails is not the way to go for uploading or downloading
> files.
> (especially as the sizes grow and the regex to find the multipart tags
> doesn't get any faster or less memory consuming).
>
> I'm creating a file storage area for users.
>
> - Only an authenticated user can upload a file
> - A user can only download a file he owns (or has permissions to)
You've stipulated that you want to use Rails for authn and authz but not use any webserver modules. These criteria are at odds.
The most robust way that I am familiar with to perform file upload/downloads (and potentially streaming) is to take advantage of the webserver that typically sits in front of Rails.
For downloading files, let the request go through your application, but instead of sending the file directly with Rails, let the webserver (or reverse proxy) do it. Sendfile and X-Accell-Redirect are your friends here. They're usually on the list of included modules too.
For uploading, both Apache and Nginx fully buffers the request before it sends it to the Rails backend in one big gulp. Rails still has to do all the mime-parsing to extract the files (but it's not held up while the file is being uploaded, a huge win especially for clients with slow internet connections).For more file upload awesomeness I'd look at the Nginx File Upload Module http://brainspl.at/articles/2008/07/20/nginx-upload-module and http://www.motionstandingstill.com/nginx-upload-awesomeness/2008-08-13/ . It parses the POST, writes the files to disk and then when it dispatches to your rails backend it gives you the paths to the files instead of the files themselves. Then you'd run your authorization etc. and just move files around.
How large are the files you are planning to upload?
I store all files in /var/webapps/MyRailsApp/private/Apache does not have access to this directory.My controller determines whether or not the user is allowed to get a file.My understanding (and perhaps my ignorance) is that Apache can't serve the file.For downloading files, let the request go through your application, but instead of sending the file directly with Rails, let the webserver (or reverse proxy) do it. Sendfile and X-Accell-Redirect are your friends here. They're usually on the list of included modules too.Am I to understand that if I use X-Sendfile I can tell Apache to send a file which is not in a public space?If that's the case, this post I found looks simple enough to follow.
Am I to understand that if I use X-Sendfile I can tell Apache to send a file which is not in a public space?If that's the case, this post I found looks simple enough to follow.That is definitely the case.
The plugin you linked to looks like it does the right thing, or you could just use the stuff built into Rails