3

I need to set up a page that will let the user upload a file, modify the file with a script, and then serve the file back to the user.

I have the uploading and modifying parts down, but I don't know where to put the file. They are going to be in the area of 1 or 2mb, and I have very little space on my webhosting plan, so I want to get rid of the files as soon as possible. There's no reason for the files to exist any longer than after the users are given the option to download by their browser upon being redirected.

Is the only way to this with a cron job that checks the creation time of the files and deletes them if they're a certain age?

I'm working with python and PHP.

edit:

First the file is uploaded. Then the location of the file is sent back to the user. The javascript on the page redirects to the path of the file. The browser opens save file dialog, and they choose to save the file or cancel. If they cancel, I want to delete the file immediately. If they choose to save the file, I want to delete the file once their download has completed.

mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • Both Python and PHP can accomplish this. Which one would you like an answer for? – Theron Luhn Apr 20 '12 at 07:26
  • @TheronLuhn, Both, if you wouldn't mind. – mowwwalker Apr 20 '12 at 07:26
  • @TheronLuhn, Also, it depends. If the code is going in the script that handles the file, then preference to Python, but if it's a script that's being run independently, then PHP. Either way, I would prefer both :D – mowwwalker Apr 20 '12 at 07:27
  • Sorry about not answering what you were actually asking. I'm not sure if what you want is possible. If it is, it definitely won't be easy to implement. Once you pass the user off to the download link, it's in the hands of the HTTP server (probably Apache), which is a whole different ball game. – Theron Luhn Apr 20 '12 at 07:53
  • @TheronLuhn, Alright, thanks for answering. Sorry you wrote all the PHP and Python only to delete your answer. I do appreciate it :D – mowwwalker Apr 20 '12 at 08:05

3 Answers3

1

In general, you have to set up a policy. The policy is for any uploaded file, the life time is 5 mins.

Kniganapolke's solution is fine and probably the cheapest and easiest to implement. If you don't like keeping the information of where the actual file is located (in your server, which you must not expose to the outside world), you need to record this in your database.

But there is an alternate solution, which is better in the long terms.

Make a queue. Yes. Cron job is the way to go because you have to consider when a user closes FireFox. Do we really want to write some javascript to check whether a user is closing Firefox? I don't know how to do it as I am not a javascript guy. Probably not really easy. But we don't want to deal with shell script or low-level system commands. Do it with Python? That means don't use cron job.

The policy is, throws as many things as needed into the queue. Add upload to the queue, throw return (display the link), then throw delete policy (check if current time is longer than 5 mins), if not, throw back the same policy into the queue.

You want a task queue such as Celery (just to name one of my favorites)

These distributed task queue would be more ideal for web services such as upload a picture, modify, and then throws back the urls. You don't want your service to be blocking (what if the conversion takes 1 minute)? Your server man (say apache or nginx) will probably timeout internally if you didn't set it up properly. You also don't want to make timeout too long, otherwise it would bring disaster.

With a task queue, you can now extend your web service. This will make your future project much better.

Some readings

http://bitkickers.blogspot.com/2010/07/djangocelery-quickstart-or-how-i.html

job queue implementation for python

Community
  • 1
  • 1
CppLearner
  • 16,273
  • 32
  • 108
  • 163
0

The first that comes to mind is setup a timeout in JavaScript and send an AJAX request to your server script to delete the file.
I'd also handle the case when user leaves or closes the page (in IE it's onBeforeUnload event).

Edit: this requires you to change the way files are downloaded. See for example starting file download with JavaScript.

Community
  • 1
  • 1
Kniganapolke
  • 5,073
  • 5
  • 22
  • 19
0

I don't know why I didn't think of it, but I was on an IRC for Python, discussing a completely unrelated issue, and someone asked why I didn't just serve the file. Exactly!

I never needed to save the file, just to send it back to the user with the correct header. Problem solved!

mowwwalker
  • 16,634
  • 25
  • 104
  • 157