From 56a4126eb8a856ebf8a0e9c0a054d948656a2f7d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 2 May 2013 21:22:47 +0200 Subject: Added api calls for file and image listings. --- fbin.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/fbin.py b/fbin.py index 2bda843..9c8d31d 100755 --- a/fbin.py +++ b/fbin.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 import templates -import settings, db, os, random, datetime, mimetypes, cgi, tempfile, hashlib, Cookie, urllib, subprocess +import settings, db, os, random, datetime, mimetypes, cgi, tempfile, hashlib, Cookie, urllib, subprocess, json from PIL import Image base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' @@ -527,6 +527,32 @@ class Application(object): 'filename': file.filename, })) + def api(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', 'application/json')]) + return [json.dumps({'status': False, 'message': 'Not logged in'})] + form = cgi.FieldStorage(environ = environ) + method = form.getvalue('method') + data = {'status': False, 'method': method, 'message': None} + if method in ('list', 'images'): + files = self.get_files(user) + data['files'] = [ + { + 'name': f.filename, + 'hash': f.hash, + 'date': int(f.date.strftime('%s')), + 'size': f.get_size(), + } + for f in files if method == 'list' or (method == 'images' and f.is_image()) + ] + data['status'] = True + else: + data['message'] = 'Unknown method "%s"' + start_response('200 OK', [('Content-Type', 'application/json')]) + return [json.dumps(data)] + f = file u = upload l = login @@ -539,11 +565,12 @@ class Application(object): r = register c = changepass d = delete + a = api def __call__(self, environ, start_response): path = environ['PATH_INFO'].split('/')[1:] module = path[0] if len(path) else '' - if len(module) and module in 'fulshmitorcd': + if len(module) and module in 'fulshmitorcda': return getattr(self, module)(environ, start_response, path) else: start_response('302 Found', [('Location', settings.virtual_root + 'u')]) -- cgit v1.2.3