From 0e936a1e55d0168b8d4af5e396bc8a22c7d260e2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 29 Jul 2011 17:09:03 +0200 Subject: Added support for configurable virtual root. --- db.py | 11 ++++++----- fbin.py | 42 +++++++++++++++++++++++++++++++----------- templates/base.tmpl | 18 +++++++++--------- templates/help.tmpl | 2 +- templates/login.tmpl | 2 +- templates/register.tmpl | 2 +- templates/upload.tmpl | 2 +- 7 files changed, 50 insertions(+), 29 deletions(-) diff --git a/db.py b/db.py index 3e16cd9..8652540 100644 --- a/db.py +++ b/db.py @@ -52,9 +52,10 @@ class File(Base): return os.path.join(settings.file_directory, self.hash + os.path.splitext(self.filename)[1]) def html(self): - return u'{filename} 1 2 ({size}) on {date}'.format( - 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')) + return u'{filename} ' \ + '1 2 ({size}) on {date}'.format( + root = settings.virtual_root, hash = self.hash, filename = self.filename, ext = os.path.splitext(self.filename)[1], + size = self.pretty_size(self.get_size()), 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' @@ -63,8 +64,8 @@ class File(Base): 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]) + return u'{filename}'.format( + root = settings.virtual_root, 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 b7450f1..bae6434 100755 --- a/fbin.py +++ b/fbin.py @@ -154,7 +154,7 @@ class Application(object): form = cgi.FieldStorage(fp = environ['wsgi.input'], environ = environ) if environ['REQUEST_METHOD'] != 'POST' or not 'file' in form or not 'filename' in form: start_response('200 OK', [('Content-Type', 'text/html')]) - return str(templates.upload(searchList = {'user': user})) + return str(templates.upload(searchList = {'root': settings.virtual_root, 'user': user})) filename = form.getvalue('filename') @@ -185,6 +185,7 @@ class Application(object): else: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.uploaded(searchList = { + 'root': settings.virtual_root, 'user': user, 'hash': hash, 'filename': filename, @@ -200,6 +201,7 @@ class Application(object): if environ['REQUEST_METHOD'] != 'POST' or not 'username' in form or not 'password' in form: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.login(searchList = { + 'root': settings.virtual_root, 'user': user, 'error': None, })) @@ -212,6 +214,7 @@ class Application(object): if user == None: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.login(searchList = { + 'root': settings.virtual_root, 'user': user, 'error': 'Login failed', })) @@ -225,7 +228,10 @@ class Application(object): c['uid']['expires'] = expires c['identifier']['expires'] = expires - start_response('302 Found', [('Location', '/u'), ('Set-Cookie', c['uid'].OutputString()), ('Set-Cookie', c['identifier'].OutputString())]) + start_response('302 Found', [ + ('Location', settings.virtual_root + 'u'), + ('Set-Cookie', c['uid'].OutputString()), + ('Set-Cookie', c['identifier'].OutputString())]) return [] def register(self, environ, start_response, path): @@ -235,6 +241,7 @@ class Application(object): if environ['REQUEST_METHOD'] != 'POST' or not 'username' in form or not 'password' in form or not 'password2' in form: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.register(searchList = { + 'root': settings.virtual_root, 'user': user, 'error': None, })) @@ -245,6 +252,7 @@ class Application(object): if password != password2: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.register(searchList = { + 'root': settings.virtual_root, 'user': user, 'error': 'Passwords doesn\'t match', })) @@ -253,11 +261,12 @@ class Application(object): if not user: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.register(searchList = { + 'root': settings.virtual_root, 'user': None, 'error': 'Username already taken.', })) - start_response('302 Found', [('Location', '/l')]) + start_response('302 Found', [('Location', settings.virtual_root + 'l')]) return [] def logout(self, environ, start_response, path): @@ -267,7 +276,10 @@ class Application(object): c['uid']['expires'] = expires c['identifier'] = '' c['identifier']['expires'] = expires - start_response('302 Found', [('Set-Cookie', c['uid'].OutputString()), ('Set-Cookie', c['identifier'].OutputString()), ('Location', '/')]) + start_response('302 Found', [ + ('Set-Cookie', c['uid'].OutputString()), + ('Set-Cookie', c['identifier'].OutputString()), + ('Location', settings.virtual_root)]) return [] def static(self, environ, start_response, path): @@ -285,6 +297,7 @@ class Application(object): user = self.validate_cookie(c) start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.help(searchList = { + 'root': settings.virtual_root, 'user': user, 'scheme': environ['wsgi.url_scheme'], 'host': environ['HTTP_HOST'], @@ -299,6 +312,7 @@ class Application(object): files = self.get_files(user) start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.my(searchList = { + 'root': settings.virtual_root, 'user': user, 'files': files, })) @@ -312,6 +326,7 @@ class Application(object): 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 = { + 'root': settings.virtual_root, 'user': user, 'files': files, })) @@ -346,16 +361,21 @@ class Application(object): def __call__(self, environ, start_response): path = environ['PATH_INFO'].split('/')[1:] - module = path[0] + module = path[0] if len(path) else '' if len(module) and module in 'fulshmitor': return getattr(self, module)(environ, start_response, path) else: - start_response('302 Found', [('Location', '/u')]) + start_response('302 Found', [('Location', settings.virtual_root + 'u')]) return [] if __name__ == '__main__': - from wsgiref.simple_server import make_server, WSGIServer - # enable IPv6 - WSGIServer.address_family |= 10 - http = make_server('', 8000, Application()) - http.serve_forever() + import sys + if len(sys.argv) == 3: + from flup.server.fcgi import WSGIServer + WSGIServer(Application(), bindAddress = (sys.argv[1], int(sys.argv[2]))).run() + else: + from wsgiref.simple_server import make_server, WSGIServer + # enable IPv6 + WSGIServer.address_family |= 10 + http = make_server('', 8000, Application()) + http.serve_forever() diff --git a/templates/base.tmpl b/templates/base.tmpl index 307aaec..6c1cb4f 100644 --- a/templates/base.tmpl +++ b/templates/base.tmpl @@ -4,26 +4,26 @@ $title - + #block head #end block
diff --git a/templates/help.tmpl b/templates/help.tmpl index bdf95d4..bb59424 100644 --- a/templates/help.tmpl +++ b/templates/help.tmpl @@ -2,7 +2,7 @@ #def header: help #extends templates.base #def content -

Usage: POST to $scheme://$host/u with filedata given to "file" and original filename to "filename". +

Usage: POST to $scheme://${host}${root}u with filedata given to "file" and original filename to "filename". Login is sent by cookies with either: