Dynamic Download Link
Are you ever wondered how some site generate dynamic links that works for a specific person and for specified time duration. We are going to create such app now!! The Basic idea is to associate a key with the file, the key will be validated for the account and time of expiration. In this case to keep it simple authentication is not implemented instead only validation of expiration time alone is done.
1. DataBase and Related Operations To store the information, a DB table with 3 column is created. 1. File path, 2. Hash code & 3. Download time. Three related operations are 1. adding entry to the table, 2. Retrieving file path given the hash code, 3. Clean up involves removing entries who's download time is less than the specified parameter.
2. Servlet
Two parameters are filePath and link. filePath corresponds to generating link and adding entry to the db. Link corresponds to retrieving the filePath from the db and creating a output stream for the file. During both the process clean up is done.
When link parameter is not null means, its a download request. Perform a clean up and retrieve filePath for the provided link. If filePath is NULL at line no. 56 means, link is removed in previous clean ups or its a fake one. Send error message as response. If filePath is not NULL generate output stream for the file.
For link creation request, generate hash code with file name and download time (Include account name also if required). Perform a clean up and insert the record to the table. Display a message to the user with download link, file name and other essential details. Clean up is performed to avoid accumulation of unnecessary records in DB.
The war file with source code is available here for download. This implemented have directory listing also.