views:

34

answers:

3

I want to serve images in a web-app using sessions such that the links to the images expire once the session has expired.

If I show the actual links to the filesystem store of the images, say http://www.mywebapp.com/images/foo1.jpg this clearly makes stopping future requests for the image (one the user has signed out of the session) difficult to stop. Which is why I was considering placing the images in a sqlite db, and serving them from there.

It seems that using the db for image storage is considered bad practice (though apparently the GAE blob store seems to provide this functionality), so i was trying to figure out what the alternatives would be.

1) Perhaps I do somesort of url-re-writing like so:

http://www.mywebapp.com/images/[session_id]/foo1.jpg Thinking of using nginx, but it seems (on a first look) that this will require some hackin to accomplish?

2) Copy the files to a physical directory on the filesystem and delete when the session expires. this seems quite messy though?

Are there any standard methods of accomplishing this dynamic image url thing?

I'm using web.py - if that helps.

Many thanks!

+2  A: 

lighty's mod_secdownload has worked well for me to solve this issue. You can read more about it at http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload

The lighttpd wiki also has a generic article about your problem: http://redmine.lighttpd.net/wiki/1/HowToFightDeepLinking

Habbie
ah, nice - this seems really the sort of thing I would need - i guess I need to figure out how to do this in nginx now!
flyingcrab
A: 

A combination of your two ideas (copy to a dir, expire when session expires) could be generalized to creating a new dir (could be as simple as a symlink) every 15 minutes. When generating the new symlink, also remove the one that's an hour old by now. Always link to the newest name in your code.

Habbie
+1  A: 

Why so complicated?

Serve the image under the name which the user supplied (i.e. http://www.mywebapp.com/images/foo1.jpg)

Save the images in a directory using a UUID as name.

Create a map of file names to UUIDs in the session.

In the handler for /images/ look up the real file name in the map. Return 404 if no such entry exists. Otherwise serve the image.

When the session is closed, delete all files from the map.

In a cron job, delete all images that are older than one day.

This way, several users can upload the same image (same name), images get deleted as soon as possible or by the cron job (if the server crashes or something like that).

Aaron Digulla
woah. that seems to be a far more elegant solution - quick question re. the map for the handler though - guessing this would be something like http://wiki.nginx.org/HttpMapModule ? and i would need to generate the map in python?
flyingcrab
Just use a plain `dict`.
Aaron Digulla