summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2012-03-04 18:00:23 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2012-03-04 18:00:23 +0100
commit69984a8c93ec8910b3cb026c9056572644c059ac (patch)
treebd0e9f59e264e909e73a9676d74256a4ac97da11 /app.py
parent0f4155c33283522fe278b3f47e3405446f2165ff (diff)
Working recoding on demand for single files and cue sheets.
Diffstat (limited to 'app.py')
-rwxr-xr-xapp.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/app.py b/app.py
index e947f80..7d88ebe 100755
--- a/app.py
+++ b/app.py
@@ -1,9 +1,26 @@
#!/usr/bin/env python2
-import db, os, json, mimetypes, datetime
+import db, codec, os, json, mimetypes, datetime
from config import config
class JSONApplication(object):
+ @staticmethod
+ def cache_check(track):
+ ext = os.path.splitext(track.filename)[-1][1:]
+ if ext in config.get('allow_extensions').split():
+ return True
+ try:
+ r = codec.Recoder(track.get_path(), config.get('encoder'), track.file_index, os.path.join(config.get('cache_dir'), str(track.id)))
+ except codec.DecoderNotFoundError:
+ return False
+ return os.path.exists(r.enc_destination)
+
+ @staticmethod
+ def format_track(track):
+ d = track.dict()
+ d['cache'] = JSONApplication.cache_check(track)
+ return d
+
def list(self, environ, start_response, path):
root_id = int(path[1]) if len(path) > 1 and len(path[1]) else 0
session = db.Session()
@@ -15,7 +32,7 @@ class JSONApplication(object):
directories = directory.children
tracks = directory.tracks
contents = json.dumps([x.dict() for x in directories] +
- [x.dict() for x in tracks])
+ [self.format_track(x) for x in tracks])
finally:
session.close()
start_response('200 OK', [('Content-Type', 'application/json'), ('Content-Length', str(len(contents)))])
@@ -88,6 +105,12 @@ class Application(object):
try:
track = db.Track.get_by_id(session, track)
filename = track.get_path()
+ ext = os.path.splitext(filename)[-1][1:]
+ if not ext in config.get('allow_extensions').split():
+ r = codec.Recoder(filename, config.get('encoder'), track.file_index, os.path.join(config.get('cache_dir'), str(track.id)))
+ filename = r.enc_destination
+ if not os.path.exists(filename):
+ r.recode()
except db.NoResultFound:
start_response('404 Not Found', [])
return []