From c9ba238cad3cbfe12e1c0be9e36e1526b759e055 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 17 Jul 2011 16:04:32 +0200 Subject: Added an image section with thumbnails of all uploaded image files. --- fbin.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'fbin.py') diff --git a/fbin.py b/fbin.py index 1212586..2689f47 100755 --- a/fbin.py +++ b/fbin.py @@ -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')]) -- cgit v1.2.3