summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-07-29 17:09:03 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-07-29 17:09:03 +0200
commit0e936a1e55d0168b8d4af5e396bc8a22c7d260e2 (patch)
tree1215aa6e389fbcaac2320b29fd53d2db23c28a30
parent5aa1666cbe5482af7b79d0ee635423500b2fdc88 (diff)
Added support for configurable virtual root.
-rw-r--r--db.py11
-rwxr-xr-xfbin.py42
-rw-r--r--templates/base.tmpl18
-rw-r--r--templates/help.tmpl2
-rw-r--r--templates/login.tmpl2
-rw-r--r--templates/register.tmpl2
-rw-r--r--templates/upload.tmpl2
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'<a href="/f/{hash}/{filename}">{filename}</a> <sup><a href="/f/{hash}">1</a> <a href="/f/{hash}{ext}">2</a></sup> ({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'<a href="{root}f/{hash}/{filename}">{filename}</a> ' \
+ '<sup><a href="{root}f/{hash}">1</a> <a href="{root}f/{hash}{ext}">2</a></sup> ({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'<a href="/f/{hash}{ext}" title="{filename}"><img src="/t/{hash}" alt="{filename}" /></a>'.format(
- hash = self.hash, filename = self.filename, ext = os.path.splitext(self.filename)[1])
+ return u'<a href="{root}f/{hash}{ext}" title="{filename}"><img src="{root}t/{hash}" alt="{filename}" /></a>'.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 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>$title</title>
- <link rel="StyleSheet" href="/s/style.css" type="text/css" />
+ <link rel="StyleSheet" href="${root}s/style.css" type="text/css" />
#block head
#end block
</head>
<body>
<div id="page">
<div id="page-header">
- <h1><a href="/">$header</a></h1>
+ <h1><a href="$root">$header</a></h1>
<div id="page-menu">
<ul>
- <li><a href="/u">upload</a></li>
+ <li><a href="${root}u">upload</a></li>
#if $user
- <li><a href="/o">logout</a></li>
- <li><a href="/m">myfiles</a></li>
- <li><a href="/i">images</a></li>
+ <li><a href="${root}o">logout</a></li>
+ <li><a href="${root}m">myfiles</a></li>
+ <li><a href="${root}i">images</a></li>
#else
- <li><a href="/l">login</a></li>
- <li><a href="/r">register</a></li>
+ <li><a href="${root}l">login</a></li>
+ <li><a href="${root}r">register</a></li>
#end if
- <li><a href="/h">help</a></li>
+ <li><a href="${root}h">help</a></li>
</ul>
</div>
</div>
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
- <p>Usage: POST to <a href="$scheme://$host/u">$scheme://$host/u</a> with filedata given to "file" and original filename to "filename".
+ <p>Usage: POST to <a href="$scheme://${host}${root}u">$scheme://${host}${root}u</a> with filedata given to "file" and original filename to "filename".
Login is sent by cookies with either:
<ul>
<li>user id in "uid" and an identifier which is sha1(uid+sha1(password))</li>
diff --git a/templates/login.tmpl b/templates/login.tmpl
index 306b0aa..d8aaded 100644
--- a/templates/login.tmpl
+++ b/templates/login.tmpl
@@ -4,7 +4,7 @@
#def content
#set error = $error or ''
<div class="error">$error</div>
- <form method="post" action="/l">
+ <form method="post" action="${root}l">
<p>username</p>
<p><input type="text" id="username" name="username" /></p>
<p>password</p>
diff --git a/templates/register.tmpl b/templates/register.tmpl
index 2a9d1a9..cb371a7 100644
--- a/templates/register.tmpl
+++ b/templates/register.tmpl
@@ -4,7 +4,7 @@
#def content
#set error = $error or ''
<div class="error">$error</div>
- <form method="post" action="/r">
+ <form method="post" action="${root}r">
<p>username</p>
<p><input type="text" id="username" name="username" /></p>
<p>password</p>
diff --git a/templates/upload.tmpl b/templates/upload.tmpl
index 49b0212..ab0bddb 100644
--- a/templates/upload.tmpl
+++ b/templates/upload.tmpl
@@ -15,7 +15,7 @@
</script>
#end def
#def content
- <form method="post" action="/u" enctype="multipart/form-data">
+ <form method="post" action="${root}u" enctype="multipart/form-data">
<input type="hidden" id="filename" name="filename" />
<p><input type="file" id="file" name="file" onchange="file_changed()" /></p>
<p><input type="submit" value="Upload" /></p>