diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-07-17 16:04:32 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-07-17 16:04:32 +0200 |
commit | c9ba238cad3cbfe12e1c0be9e36e1526b759e055 (patch) | |
tree | 0300a3222eb5bb3151f4d453e294efb44b6c52f2 /fbin.py | |
parent | 53d795fabe64c670bd97c0c0ad929b5a9b3fcd3f (diff) |
Added an image section with thumbnails of all uploaded image files.
Diffstat (limited to 'fbin.py')
-rwxr-xr-x | fbin.py | 33 |
1 files changed, 32 insertions, 1 deletions
@@ -2,12 +2,16 @@ import templates import settings, db, os, random, datetime, shutil, mimetypes, cgi, tempfile, hashlib, Cookie +from PIL import Image base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' if not os.path.isdir(settings.file_directory): os.mkdir(settings.file_directory) +if not os.path.isdir(settings.thumb_directory): + os.mkdir(settings.thumb_directory) + class Application(object): def get_user(self, username, password): session = db.Session() @@ -287,19 +291,46 @@ class Application(object): 'files': files, })) + def images(self, environ, start_response, path): + c = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None) + user = self.validate_cookie(c) + if user == None: + start_response('200 OK', [('Content-Type', 'text/html')]) + return ['Not logged in.'] + files = [f for f in self.get_files(user) if f.is_image()] + start_response('200 OK', [('Content-Type', 'text/html')]) + return str(templates.images(searchList = { + 'user': user, + 'files': files, + })) + + def thumb(self, environ, start_response, path): + hash = path[1] + thumbfile = os.path.join(settings.thumb_directory, hash + '.jpg') + if not os.access(thumbfile, os.F_OK): + filename = self.get_file_path(hash) + im = Image.open(filename) + im.thumbnail(settings.thumb_size, Image.ANTIALIAS) + im.save(thumbfile) + + start_response('200 OK', [('Content-Type', 'image/jpeg')]) + return open(thumbfile, 'rb') + f = file u = upload l = login s = static h = help m = my_files + i = images + t = thumb o = logout r = register def __call__(self, environ, start_response): path = environ['PATH_INFO'].split('/')[1:] module = path[0] - if len(module) and module in 'fulshmor': + if len(module) and module in 'fulshmitor': return getattr(self, module)(environ, start_response, path) else: start_response('302 Found', [('Location', '/u')]) |