# Where to place uploaded files for later download?

**URL:** <https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852>\
**Category:** Rails\
**Created:** [January 19, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852 "2006-01-19T19:16:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/1 "2006-01-19T19:16:00Z")

</div>

Hi there,

I am working in a RoR application which allows certain users  
to upload files. Currently I store these files  
in a directory called “files”. This directory is located in  
the RoR application directory:

rails/rails\_application/files

So users don’t have access to them.

Now I want users to be able to download some of these files  
(they will search based on some parameters). With this  
setup I cannot link from my views to that files since they  
are out of the public directory.

I could move the files directory to the public directory but  
then everybody will have access to those files.

Any idea about how to implement this?

Thank you in advance,

David

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/2 "2006-01-19T19:22:03Z")

</div>

what about a symlink? YOu need some way of getting those files into  
the public directory so its a matter of how you want to do it. Bottom  
line is they will need to be there at some point.

Maybe on a per request basis copy the original file to the public  
directory with some random generated name to make it obscure, and then  
delete it.

adam

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/3 "2006-01-19T19:31:07Z")

</div>

David –

send\_file [1] is probably what you’re looking for.

cheers  
Gerret

[1]  
[http://api.rubyonrails.com/classes/ActionController/Streaming.html#M000044](http://api.rubyonrails.com/classes/ActionController/Streaming.html#M000044)

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/4 "2006-01-19T19:52:14Z")

</div>

You want to use an FCGI authorizer, but it is a fairly advanced  
technique. The authorizer will ping your app and let you decide  
whether to authorize access by returning HTTP 200 to grant access.

The big win is that the files are sent using the web server instead of  
routing them through Ruby. lighttpd and apache use the OS implemented  
sendfile() which is many times more CPU friendly that sending the file  
with Rails. Think of what will happen if 500 users ask for a 100MB  
file and they are all using dialup.

Here’s the lighttpd setup, apache supports it too but not webrick.

fastcgi.server = (  
“/private” =\>  
( “localhost-d” =\>  
( “min-procs” =\> 1,  
“max-procs” =\> 1,  
“socket” =\> “log/fcgi.socket”,  
“bin-path” =\> “public/dispatch.fcgi”,  
“bin-environment” =\> ( “RAILS\_ENV” =\> “development” ),  
“docroot” =\> “private”,  
“mode” =\> “authorizer”  
)  
),  
“.fcgi” =\>  
( “localhost-f” =\>  
(  
“min-procs” =\> 1,  
“max-procs” =\> 1,  
“socket” =\> “log/fcgi.socket”,  
“bin-path” =\> “public/dispatch.fcgi”,  
“bin-environment” =\> ( “RAILS\_ENV” =\> “development” ),  
“mode” =\> “responder”  
)  
)  
)

–  
Jon S.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/5 "2006-01-19T20:01:18Z")

</div>

if you use lighttpd, there is an anti-hotlinking mod

[http://lighttpd.net/documentation/trigger\_b4\_dl.html](http://lighttpd.net/documentation/trigger_b4_dl.html)

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/6 "2006-01-19T20:07:21Z")

</div>

I use lighttpd to create an asset server like [files.myuser.example.com](http://files.myuser.example.com)

# Asset Host

$HTTP[“host”] =~ “^files.([\w\d.]+).example.(com|org|net)” {  
server.document-root = “/path/to/storage\_dir/”  
url.rewrite-once = (“/files/(.\*)” =\> “/%1/$1”)  
}

This takes a request like:  
[http://files.myuser.example.com/files/images/mypic.gif](http://files.myuser.example.com/files/images/mypic.gif)  
and routes it to:  
/path/to/storage\_dir/myuser/images/mypic.gif

You could combine this with FCGI authorizer like Jon S. mentioned.

- Ryan H.

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/7 "2006-01-19T23:05:37Z")

</div>

Hi there,

Thanks to everybody for the replies. They are very helpful.

I will evaluate all the different solutions but I think send\_file  
is what I was looking for.

Thanks again,

David

On Thu, Jan 19, 2006 at 06:51:20PM +0100, Gerret A. wrote:

> > what about a symlink? YOu need some way of getting those files into  
> > On 1/19/06, David Rio D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Thank you in advance,  
> > > removed\_email\_address@domain.invalid  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

* * *

David Rio D.  
Software Engineer  
Console, Inc.  
Tel: 619.237.5552  
Fax: 619.237.5269  
[http://www.console.net](http://www.console.net)

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/8 "2006-01-19T23:47:55Z")

</div>

On 1/19/06, David Rio D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi there,
> 
> Thanks to everybody for the replies. They are very helpful.
> 
> I will evaluate all the different solutions but I think send\_file  
> is what I was looking for.

Note that ruby send\_file ties up your FCGI process until the file is  
sent. This will take a long time for a video download or a dialup  
user. You may end up needing a lot of simultaneous FCGI processes.

Authorizer ties up the web server for the duration, but not the FCGI  
process. The web server is is multithreaded so it can handle being  
tied up.

–  
Jon S.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 19, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/9 "2006-01-19T23:50:58Z")

</div>

On 1/19/06, David Rio D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi there,
> 
> Thanks to everybody for the replies. They are very helpful.
> 
> I will evaluate all the different solutions but I think send\_file  
> is what I was looking for.

Note that ruby send\_file ties up your FCGI process until the file is  
sent. This may take a long time for a video download or a dialup user.  
You may end up needing a lot of simultaneous FCGI processes.

Authorizer ties up the web server for the duration, but not the FCGI  
process.

–  
Jon S.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 20, 2006, 12:57am UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/10 "2006-01-20T00:57:24Z")

</div>

On 1/19/06, Santiago E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Here’s the lighttpd setup, apache supports it too but not webrick.
> 
> Do you have any documentation or website where I can find info about  
> setting this up under apache?

[http://www.fastcgi.com/mod\_fastcgi/docs/mod\_fastcgi.html](http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html)

> Thanks,  
> Santiago
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Jon S.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@davox](https://www.ruby-forum.com/u/davox)\
**Post date:** [January 20, 2006, 12:51am UTC](https://www.ruby-forum.com/t/where-to-place-uploaded-files-for-later-download/52852/11 "2006-01-20T00:51:22Z")

</div>

On 1/19/06, Jon S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s the lighttpd setup, apache supports it too but not webrick.

Do you have any documentation or website where I can find info about  
setting this up under apache?

Thanks,  
Santiago
