Apache HTTP server - How to determine on the server if a download finished and of which file?

We have an Apache HTTP server up and running, which serves dynamically created zip archives to the users which are possibly several 100s of megabytes in size. As we create a new file with every click on the "download" button (even if the content did not change...), we will most likely run into disc capacity problems.

I have a rather dumb script running that deletes all files that have been created 90+minutes ago. Obviously not that nice of a solution.

I was wondering how to determine from the command line of the server when a download finished, successfully or not. In that case I can delete that file as it will not be served twice. Thinking about it, it might even be sufficient to check if the server is using a file or not as it is created right before it is served to the client.

Thanks!


Solution 1:

Since deleting a file that a process has an open file handle on leaves the file there until the handle is closed you could just delete them immediately, and when Apache closes the file it'll be removed from disk.

Solution 2:

TRS-80's got the right idea, and I'd definitely recommend going down that route. If you're dead-set on waiting until the transfer's finished, then consider using lsof to determine when nobody's got the file open. So, something like:

for file in /directory/full/of/zips/*.zip; do
  if [ -z "$(lsof $file)" ]; then
    # Nobody's reading it, delete
    rm $file
  fi
done

Solution 3:

The first way I see is to parse the result of server-status to know if the download has started. When the download has started you can delete the file as TRS-80 said. But I would not recommend that because if you client got disconnect for any reason he will not be possible to restart the download. So I would parse logs file to know when the file has been downloaded. The entry in the log file will not be added untill the connexion with the client is closed. In the log file you will have the number of octet served to the client so that you can compare with the file size to be sure that he has download the whole file.