5 Replies Latest reply on Oct 18, 2013 11:25 AM by Steve Sommers

    Delete file on session end

    BreakawayPaul Level 2

      I am recreating a web application that serves up search results in a CSV file.  I'm able to create the csv fil from a database query, then offer a download link to grab it from.

       

      Question 1: In order to make the CSV files unique, I am appending them with the session id.  Is this a bad idea?  Should I use an encrypted session id instead?

       

      Question 2: In order to prevent the server from filling up with csv files, I want to delete the file once it's been downloaded.  I'd like to do this on session end, but the session variable I used to name the file will be gone when the session ends, so this might not be the best idea.  Any suggestions?

       

      Thanks!

        • 1. Re: Delete file on session end
          Steve Sommers Level 4

          Question 1: I would use CreateUUID() for the file name uniqueness and store this in the session scope. I don't like using the actual session id as I don't like the idea of using something out of my control for this sort of thing. Also, your logic would have to account for the session setting options in the CF administration.

           

          Question 2: There is an onSessionEnd event in the latest CF versions, but I have not used it. I would store an array in the application scope with expiration information. When the item expires, deleted it from the array. Then have a recurring scheduled event delete all the files not in the current array, maybe once an hour or so.

          1 person found this helpful
          • 2. Re: Delete file on session end
            BreakawayPaul Level 2

            Thanks Steve.

             

            You're correct, I don't have control over the CF Admin, so the UUID is probably the best idea.  As for the deleting, perhaps I could just write something to delete any files older than an hour whenever the page is loaded.

            • 3. Re: Delete file on session end
              Steve Sommers Level 4

              That'll work too. That was actually my original thought but then I assumed you wanted something more complex.

              • 4. Re: Delete file on session end
                BreakawayPaul Level 2

                Complex is tough where I work because I'm not allowed to create things like chron jobs.  Unless it happens in the CF application itself, it ain't happening.

                • 5. Re: Delete file on session end
                  Steve Sommers Level 4

                  Then maybe something like this in your onRequestStart or onSessionStart of your application.cfc:

                   

                  <cfset variables.doCleanup = false />
                  <cflock name="myCHRON" timeout="15" throwontimeout="no" type="exclusive">

                       <cfif NOT structKeyExists(application,"myNextCleanupRun") or application.myNextCleanupRun LTE now()>

                            <cfset variables.doCleanup = true />

                            <cfset application.myNextCleanupRun = dateAdd('h',1,now()) />

                       </cfif>
                  </cflock>

                  <cfif variables.doCleanup>...

                  </cfif>