diff --git a/api/__init__.py b/api/__init__.py index a16b28b..3ed78ef 100755 --- a/api/__init__.py +++ b/api/__init__.py @@ -17,7 +17,7 @@ def set_formatter(): (f, callback) = map(request.args.get, ['f', 'callback']) if f == 'jsonp': # Some clients (MiniSub, Perisonic) set f to jsonp without callback for streamed data - if not callback and request.endpoint not in [ 'stream_media' ]: + if not callback and request.endpoint not in [ 'stream_media', 'cover_art' ]: return ResponseHelper.responsize_json({ 'error': { 'code': 0, diff --git a/api/browse.py b/api/browse.py index 3b6af3c..bf38b61 100755 --- a/api/browse.py +++ b/api/browse.py @@ -36,7 +36,7 @@ def list_indexes(): folder = Folder.query.get(mfid) - if not folder and not folder.root: + if not folder or (type(folder) is not list and not folder.root): return request.error_formatter(70, 'Folder not found') last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan diff --git a/api/media.py b/api/media.py index 1e3144a..9ad288d 100755 --- a/api/media.py +++ b/api/media.py @@ -1,8 +1,10 @@ # coding: utf-8 from flask import request, send_file +import Image from web import app -from db import Track +from db import Track, Folder +import config import os.path, uuid @app.route('/rest/stream.view') @@ -39,3 +41,40 @@ def stream_media(): return send_file(track.path) +@app.route('/rest/getCoverArt.view') +def cover_art(): + id = request.args.get('id') + if not id: + return request.error_formatter(10, 'Missing cover art id') + try: + fid = uuid.UUID(id) + except: + return request.error_formatter(0, 'Invalid cover art id') + folder = Folder.query.get(fid) + if not folder or not folder.has_cover_art or not os.path.isfile(os.path.join(folder.path, 'cover.jpg')): + return request.error_formatter(70, 'Cover art not found') + + size = request.args.get('size') + if size: + try: + size = int(size) + except: + return request.error_formatter(0, 'Invalid size value') + else: + return send_file(os.path.join(folder.path, 'cover.jpg')) + + im = Image.open(os.path.join(folder.path, 'cover.jpg')) + if size > im.size[0] and size > im.size[1]: + return send_file(os.path.join(folder.path, 'cover.jpg')) + + size_path = os.path.join(config.get('CACHE_DIR'), str(size)) + path = os.path.join(size_path, id) + if os.path.exists(path): + return send_file(path) + if not os.path.exists(size_path): + os.makedirs(size_path) + + im.thumbnail([size, size], Image.ANTIALIAS) + im.save(path, 'JPEG') + return send_file(path) + diff --git a/config.py b/config.py index 243715f..7fd9e70 100755 --- a/config.py +++ b/config.py @@ -2,12 +2,6 @@ import os -def try_load_config(path): - if os.path.exists(path): - app.config.from_pyfile(path) - return True - return False - def check(): path = os.path.join(os.path.expanduser('~'), '.supysonic') if os.path.exists(path): diff --git a/db.py b/db.py index d7e6448..605a297 100755 --- a/db.py +++ b/db.py @@ -62,6 +62,7 @@ class Folder(Base): root = Column(Boolean, default = False) name = Column(String) path = Column(String, unique = True) + has_cover_art = Column(Boolean, default = False) last_scan = Column(DateTime, default = datetime.datetime.min) parent_id = Column(UUID, ForeignKey('folder.id'), nullable = True) @@ -76,6 +77,8 @@ class Folder(Base): if not self.root: info['parent'] = str(self.parent_id) info['artist'] = self.parent.name + if self.has_cover_art: + info['coverArt'] = str(self.id) return info @@ -139,8 +142,9 @@ class Track(Base): info['year'] = self.year if self.genre: info['genre'] = self.genre + if self.folder.has_cover_art: + info['coverArt'] = str(self.folder_id) - # coverArt # transcodedContentType # transcodedSuffix # userRating diff --git a/main.py b/main.py index 84f261d..ac5d285 100755 --- a/main.py +++ b/main.py @@ -2,12 +2,16 @@ from web import app import db, config +import os.path if __name__ == '__main__': if not config.check(): print >>sys.stderr, "Couldn't find configuration file" sys.exit(1) + if not os.path.exists(config.get('CACHE_DIR')): + os.makedirs(config.get('CACHE_DIR')) + db.init_db() app.run(debug = True) diff --git a/scanner.py b/scanner.py index 186b405..84b3888 100755 --- a/scanner.py +++ b/scanner.py @@ -45,6 +45,11 @@ class Scanner: self.__cleanup_folder(folder) + def check_cover_art(self, folder): + folder.has_cover_art = os.path.isfile(os.path.join(folder.path, 'cover.jpg')) + for f in folder.children: + self.check_cover_art(f) + def __scan_file(self, path, folder): tag = eyeD3.Tag() tag.link(path) diff --git a/web.py b/web.py index 1f4088b..7776a98 100755 --- a/web.py +++ b/web.py @@ -162,6 +162,7 @@ def scan_folder(id = None): for folder in db.Folder.query.filter(db.Folder.root == True).all(): s.scan(folder) s.prune(folder) + s.check_cover_art(folder) else: try: idid = uuid.UUID(id) @@ -176,6 +177,7 @@ def scan_folder(id = None): s.scan(folder) s.prune(folder) + s.check_cover_art(folder) added, deleted = s.stats() db.session.commit()