summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
Diffstat (limited to 'app.py')
-rwxr-xr-xapp.py59
1 files changed, 43 insertions, 16 deletions
diff --git a/app.py b/app.py
index 9120ea4..f288646 100755
--- a/app.py
+++ b/app.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python2
-import os, mimetypes, json, cgi, recode, time, urllib, events, threading
+import os, mimetypes, json, cgi, recode, time, urllib, events
from config import config
from directory import Directory, File
@@ -11,26 +11,22 @@ class Application(object):
def files(self, environ, start_response, path):
full_path = os.path.join(config.get('music_root'), *path[1:])
+
+ if not os.path.exists(full_path) or '..' in path:
+ start_response('404 Not Found', [])
+ return []
+
rel_path = os.path.join(*path[1:] or '.')
if os.path.isdir(full_path):
start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
return (str(x) for x in Directory(rel_path).listdir())
else:
- args = cgi.FieldStorage(environ = environ)
-
- decoder = args.getvalue('decoder') if 'decoder' in args else None
- encoder = args.getvalue('encoder') if 'encoder' in args else None
-
- if decoder and encoder:
- cache_file = File(rel_path).get_cache_file()
- return File(cache_file, True).send(environ, start_response)
- else:
- return File(rel_path).send(environ, start_response)
+ return File(rel_path).send(environ, start_response)
def static(self, environ, start_response, path):
filename = os.path.join('static', *path[1:])
- if not os.access(filename, os.F_OK) or '..' in path:
+ if not os.path.exists(filename) or '..' in path:
start_response('404 Not Found', [])
return []
@@ -38,6 +34,16 @@ class Application(object):
start_response('200 OK', [('Content-Type', mime)])
return open(filename, 'rb')
+ def cache(self, environ, start_response, path):
+ path = os.path.join(*path[1:])
+ cache_path = File(path).get_cache_path()
+
+ if not os.path.exists(cache_path) or '..' in path:
+ start_response('404 Not Found', [])
+ return []
+
+ f = File(cache_path, True)
+ return f.send(environ, start_response)
# JSON handlers
@@ -52,15 +58,34 @@ class Application(object):
s = json.dumps([x.json() for x in contents])
return s
- def json_cache(self, environ, start_response, path):
+ def json_recode(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
path = args.getvalue('path') if 'path' in args else None
decoder = args.getvalue('decoder') if 'decoder' in args else None
encoder = args.getvalue('encoder') if 'encoder' in args else None
f = File(path)
- t = threading.Thread(target = f.recode, args = (decoder, encoder, environ['sessionid']))
- t.start()
+ f.start_recode(decoder, encoder, environ['sessionid'])
+
+ start_response('200 OK', [('Content-Type', 'text/plain')])
+ return []
+
+ def json_play(self, environ, start_response, path):
+ args = cgi.FieldStorage(environ = environ)
+
+ path = args.getvalue('path')
+
+ f = File(path)
+ # TODO: replace this with some sane logic
+ if not os.path.splitext(path)[1] in ('.mp3', '.ogg'):
+ cache_path = f.get_cache_path()
+ decoder, encoder = ('ffmpeg',)*2
+ if not os.path.exists(cache_path):
+ f.start_recode(decoder, encoder, environ['sessionid'])
+ else:
+ events.event_pub.play(environ['sessionid'], '/cache/{0}'.format(path))
+ else:
+ events.event_pub.play(environ['sessionid'], '/files/{0}'.format(path))
start_response('200 OK', [('Content-Type', 'text/plain')])
return []
@@ -68,8 +93,10 @@ class Application(object):
handlers = {
'files': files,
'static': static,
+ 'cache': cache,
'list': json_list,
- 'cache': json_cache,
+ 'recode': json_recode,
+ 'play': json_play,
'events': events.EventSubscriber,
}