Reputation: 5670
I'm creating an application for our office that allows authenticated users in the office to upload files for clients, creates a list of links to download the files and emails the client with the list of links.
When a user in the office logs in they're assigned a UUID in their session scope. That UUID becomes the directory name that the uploaded files are stored in.
Occasionally I'll need to clear out old files and delete the directories, but when that happens there's a chance a client may try to re-download a file from an old link.
In ColdFusion 9 how would I catch this and send them to an error page? I'm also hoping to use similar code to redirect a user once the download starts so they're not just sitting on a blank page during the download process.
Here's my force download page that takes a folder name variable and a filename variable to serve up the files.
<cfset folder = #URL.folder#>
<cfset FileDownload = #URL.file#>
<cfset exten = ListLast(FileDownload, ".")>
<cfswitch expression="#exten#">
<cfcase value="zip"><cfset content_type = "application/zip, application/x-zip, application/x-zip-compressed, application/octet-stream, application/x-compress, application/x-compressed, multipart/x-zip"></cfcase>
<cfcase value="ai"><cfset content_type = "application/illustrator"></cfcase>
<cfcase value="eps"><cfset content_type = "application/illustrator, application/octect-stream"></cfcase>
<cfcase value="pdf"><cfset content_type = "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf"></cfcase>
<cfcase value="psd"><cfset content_type = "image/photoshop, image/x-photoshop, image/psd, application/photoshop"></cfcase>
<cfcase value="jpg"><cfset content_type = "image/jpeg"></cfcase>
<cfcase value="png"><cfset content_type = "image/png"></cfcase>
<cfcase value="tif"><cfset content_type = "image/tiff"></cfcase>
<cfdefaultcase><cfset content_type = "image/jpeg"></cfdefaultcase>
</cfswitch>
<cfset fileToGetSizeOf = expandPath("./#folder#/#FileDownload#") />
<cfoutput><cfheader name="content-disposition" value="attachment;filename=#FileDownload#"><cfheader name="content-length" value="#getFileInfo(fileToGetSizeOf ).size#" />
<cfcontent type="#content_type#" file="#ExpandPath("./#folder#")#/#FileDownload#" deletefile="#delete_file#"></cfoutput>
Upvotes: 1
Views: 371
Reputation: 206
You can use FileExists(absolute_path) function to check for the existence of the downloaded file.
Upvotes: 0
Reputation: 6956
There are three goals you want to achieve here:
Serve the files securely. Currently you are not protected from exploiting the system (or other user) files, for example like this (I've set passed values instead of URL keys):
<cfset folder = "../../../some/other/path/" />
<cfset FileDownload = "some_other_file.pdf" />
Show 404 page if file or directory does not exist any more.
Show some nice page once download starts. Maybe I am missing something, but redirecting user after download started (=headers are sent) is pretty tricky.
Any way, there's pretty simple and reliable solution to all of these.
You should keep the list of the files in database, so each record may include:
Note: 3-4 may be done as single column ('path/filename.ext') since paths are temporary any way.
So, when your application creates a list of links to download the files and emails the client with the list of links.
, it also records each file to the database. Each of these links now may look like this:
<a href="http://yourwebsite.tld/download.cfm?file=#ID#">#DESCRIPTION#</a>
When user clicks the link script performs following:
start
key in URL: increment the downloads counter, serve the file using cfcontent and content type.download.cfm?file=#ID#&start=yes
, plus you have meta http-equiv="refresh"
with same URL.Notes:
errorCode
, so you can have one place of redirect.Upvotes: 3
Reputation: 111
You can use FileExists
to check for the existence and make the decision based on that:
<cfif NOT FileExists(fileToGetSizeOf)>
<!--- send a 404 --->
<cfelse>
<!--- send the file --->
</cfif>
Upvotes: 0
Reputation: 11
If your site has a 404 page, you are already directing them to an error page. Or at least your web server is. If you don't have a site wide 404 page set up, make it a cfm page to give you some extra functionality (logging, alerting, etc)
Upvotes: 0