Forum: Ruby on Rails File management!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
078ae5ebda3c973cfabf6a95b4cc168f?d=identicon&s=25 Vu Nguyen (yellow)
on 2006-01-30 06:34
Hi, I'm new to ROR. I was wondering how to:

1. Check if the file is being read/downloaded/....
2. Rename files

If you can help or point out any document that I need to read to
accomplish this, please do. Thanks a lot.
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-30 09:37
(Received via mailing list)
vu nguyen wrote:
> Hi, I'm new to ROR. I was wondering how to:
>
> 1. Check if the file is being read/downloaded/....
By (possibly) another process?  Via the webserver?  You'll have to add
that manually, I think.

> 2. Rename files
File.rename('oldfilename', 'newfilename')

>
> If you can help or point out any document that I need to read to
> accomplish this, please do. Thanks a lot.

http://www.ruby-doc.org/core/classes/File.html
078ae5ebda3c973cfabf6a95b4cc168f?d=identicon&s=25 Vu Nguyen (yellow)
on 2006-01-31 06:46
Wow, thanks a lot. The reason I asked was that I want to allow users
download files from my host, yet hide the real url from them to prevent
leaching.

The solution I came up with was: let the user download the file, check
if the files downloaded (not being read), then rename it. Of course the
database will keep track of the files for me.
But today I did some check and found out some thing interesting: even if
I change the file name while the user downloading it, it wouldnt affect
their current download --> Then I dont have to check if the file is
being accessed (which is a hard job to do)

Also I found another interesting solution:
http://www.webclass.ru/eng/Tutorials/Perl/How_to_hide.html
Basically instead of sending the file header to the user's browser, you
print out the file content. I wonder if it's possible to do on Rails
tho, and what happen if the file is pretty big and not an image file....
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-31 10:12
(Received via mailing list)
vu nguyen wrote:
> being accessed (which is a hard job to do)
If the file's popular, there's a race condition in there:

Client X sees the page, notes the filename as 'aaa.zip'.
Client Y sees the page, notes the filename as 'aaa.zip'.
Client X requests the file as 'aaa.zip'.
Server renames the file to 'bbb.zip'.
Client Y requests the file as 'aaa.zip'.
Client Y gets a 404.

Slight snag.
078ae5ebda3c973cfabf6a95b4cc168f?d=identicon&s=25 Vu Nguyen (yellow)
on 2006-01-31 11:51
Alex Young wrote:
> vu nguyen wrote:
>> being accessed (which is a hard job to do)
> If the file's popular, there's a race condition in there:
>
> Client X sees the page, notes the filename as 'aaa.zip'.
> Client Y sees the page, notes the filename as 'aaa.zip'.
> Client X requests the file as 'aaa.zip'.
> Server renames the file to 'bbb.zip'.
> Client Y requests the file as 'aaa.zip'.
> Client Y gets a 404.
>
> Slight snag.


Interesting, but then you have to rename it back after some time? Or you
keep track using database?

Talking about link hiding, I used readfile() when I worked with PHP
(still do). But now I think of a better way: give each file 1 unique id,
keep track in mysql. Everytime the browser/download manager requests the
file, I simply send back the header to that file. That way my users can
use RESUME.
I can still authenticate and prevent leaching, and my users can resume
their broken download files.
078ae5ebda3c973cfabf6a95b4cc168f?d=identicon&s=25 Vu Nguyen (yellow)
on 2006-01-31 11:55
Huhm, this is mailing list so I cant edit what I posted *_*. Anyway,
just to add 1 thing: after sending the header to the browser/Dmanager so
the download can start, I will just rename my file/or the parent
folder.(which does not affect the current downloads in any way).
This topic is locked and can not be replied to.