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. --- .gitignore | 1 + db.py | 12 +++++++++++- fbin.py | 33 ++++++++++++++++++++++++++++++++- static/style.css | 4 ++++ templates/__init__.py | 1 + templates/base.tmpl | 1 + templates/images.tmpl | 14 ++++++++++++++ 7 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 templates/images.tmpl diff --git a/.gitignore b/.gitignore index 71b5cc6..268ebe8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ *.pyc settings.py /files +/thumbs /templates/*.py !/templates/__init__.py diff --git a/db.py b/db.py index 9c49155..3e16cd9 100644 --- a/db.py +++ b/db.py @@ -4,7 +4,7 @@ from sqlalchemy.orm import sessionmaker, relation, backref from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.exc import IntegrityError from sqlalchemy.sql import and_ -import settings, os +import settings, os, mimetypes engine = create_engine(settings.db_path) @@ -56,5 +56,15 @@ class File(Base): hash = self.hash, filename = self.filename, ext = os.path.splitext(self.filename)[1], size = self.pretty_size(os.path.getsize(self.get_path())), date = self.date.strftime('%Y-%m-%d %H:%M:%S UTC')) + def get_mime_type(self): + return mimetypes.guess_type(self.filename, strict = False)[0] or 'application/octet-stream' + + def is_image(self): + return self.get_mime_type().startswith('image') + + def image_html(self): + return u'{filename}'.format( + hash = self.hash, filename = self.filename, ext = os.path.splitext(self.filename)[1]) + Base.metadata.create_all() Session = sessionmaker(bind = engine, autoflush = True, autocommit = False) 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')]) diff --git a/static/style.css b/static/style.css index 79730df..609a2aa 100644 --- a/static/style.css +++ b/static/style.css @@ -11,6 +11,10 @@ div#page-menu ul { margin-left: 0; } div#page-menu a { color: #ccc; text-decoration: none; margin: 0 .5em 0 .5em; } ul { list-style-type: none; } +ul.image-list li { display: inline-block; } +ul.image-list li img { opacity: .8; } +ul.image-list li img:hover { opacity: 1; } +ul.image-list li sup { margin-left: auto; margin-right: auto; } li { margin-left: 1em; } p { margin-bottom: 1em; } code { font-family: monospace; } diff --git a/templates/__init__.py b/templates/__init__.py index b8f3f17..3f11c65 100644 --- a/templates/__init__.py +++ b/templates/__init__.py @@ -3,4 +3,5 @@ from uploaded import uploaded from help import help from login import login from my import my +from images import images from register import register diff --git a/templates/base.tmpl b/templates/base.tmpl index aca2011..307aaec 100644 --- a/templates/base.tmpl +++ b/templates/base.tmpl @@ -18,6 +18,7 @@ #if $user
  • logout
  • myfiles
  • +
  • images
  • #else
  • login
  • register
  • diff --git a/templates/images.tmpl b/templates/images.tmpl new file mode 100644 index 0000000..34c8e78 --- /dev/null +++ b/templates/images.tmpl @@ -0,0 +1,14 @@ +#def title: images +#def header: images +#extends templates.base +#def content +

    Your uploaded images:

    + +#end def -- cgit v1.2.3