summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
Diffstat (limited to 'app.py')
-rwxr-xr-xapp.py75
1 files changed, 57 insertions, 18 deletions
diff --git a/app.py b/app.py
index 3b887d8..f02dcc6 100755
--- a/app.py
+++ b/app.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python2
-import os, mimetypes, json, cgi, recode, time, urllib, events
+import os, mimetypes, json, cgi, recode, time, urllib, events, db, itertools
from config import config
from directory import Directory, File
@@ -16,12 +16,11 @@ class Application(object):
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())
+ return (str(x) for x in Directory(full_path).listdir())
else:
- return File(rel_path).send(environ, start_response)
+ return File(full_path).send(environ, start_response)
def static(self, environ, start_response, path):
filename = os.path.join('static', *path[1:])
@@ -37,7 +36,17 @@ class Application(object):
def cache(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
path = os.path.join(*path[1:])
- track = args.getvalue('track') if 'track' in args else None
+ path = os.path.join(config.get('music_root'), path)
+ track = int(args.getvalue('track')) if 'track' in args else None
+
+ try:
+ session = db.Session()
+ t = db.Track.find(session, path, track)
+ except:
+ start_response('404 Not Fonud', [])
+ return []
+ finally:
+ session.close()
cache_path = File(path, track = track).get_cache_path()
if not os.path.exists(cache_path) or '..' in path:
@@ -51,19 +60,40 @@ class Application(object):
def json_list(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
- directory = args.getvalue('directory') if 'directory' in args else '/'
- d = Directory(directory)
-
- contents = d.listdir()
+ directory = args.getvalue('directory') if 'directory' in args else ''
+
+ if directory[-1:] == '/':
+ directory = directory[:-1]
+ directory = os.path.join(config.get('music_root'), directory)
+ if directory[-1:] == '/':
+ directory = directory[:-1]
+ contents = []
+ try:
+ session = db.Session()
+ directory = db.Directory.get(session, directory)
+ directories = (Directory(d.path) for d in directory.children)
+ tracks = (File(t.get_path(), track = t.file_index, metadata = t.get_metadata()) for t in directory.tracks)
+ contents = [x.json() for x in itertools.chain(directories, tracks)]
+ finally:
+ session.close()
start_response('200 OK', [('Content-Type', 'text/plain')])
- s = json.dumps([x.json() for x in contents])
- return s
+ return json.dumps(contents)
def json_recode(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
- path = args.getvalue('path') if 'path' in args else None
- track = args.getvalue('track') if 'track' in args else None
+ path = args.getvalue('path') if 'path' in args else ''
+ path = os.path.join(config.get('music_root'), path)
+ track = int(args.getvalue('track')) if 'track' in args else None
+
+ try:
+ session = db.Session()
+ t = db.Track.find(session, path, track)
+ except:
+ start_response('404 Not Fonud', [])
+ return []
+ finally:
+ session.close()
f = File(path, track = track)
# see json_play()
@@ -77,9 +107,18 @@ class Application(object):
def json_play(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
-
- path = args.getvalue('path')
- track = args.getvalue('track') if 'track' in args else None
+ rel_path = args.getvalue('path')
+ path = os.path.join(config.get('music_root'), rel_path)
+ track = int(args.getvalue('track')) if 'track' in args else None
+
+ try:
+ session = db.Session()
+ t = db.Track.find(session, path, track)
+ except:
+ start_response('404 Not Fonud', [])
+ return []
+ finally:
+ session.close()
f = File(path, track = track)
# TODO: replace this with some sane logic
@@ -90,9 +129,9 @@ class Application(object):
if not os.path.exists(cache_path):
f.start_recode(decoder, encoder, environ['sessionid'])
else:
- events.event_pub.play(environ['sessionid'], '/cache/{0}{1}'.format(path, ('?track=' + track if track else '')))
+ events.event_pub.play(environ['sessionid'], '/cache/{0}{1}'.format(rel_path, ('?track=' + str(track) if track else '')))
else:
- events.event_pub.play(environ['sessionid'], '/files/{0}'.format(path))
+ events.event_pub.play(environ['sessionid'], '/files/{0}'.format(rel_path))
start_response('200 OK', [('Content-Type', 'text/plain')])
return []