1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-09 19:52:16 +00:00

Implemented getCoverArt.view

This commit is contained in:
Alban 2012-11-11 21:39:26 +01:00
parent 584f1cf90c
commit 6198660765
8 changed files with 58 additions and 10 deletions

View File

@ -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,

View File

@ -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

View File

@ -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)

View File

@ -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):

6
db.py
View File

@ -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

View File

@ -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)

View File

@ -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)

2
web.py
View File

@ -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()