2012-10-20 18:05:39 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from flask import request, send_file
|
2012-11-11 20:39:26 +00:00
|
|
|
import Image
|
2012-10-20 18:05:39 +00:00
|
|
|
from web import app
|
2012-11-11 20:39:26 +00:00
|
|
|
from db import Track, Folder
|
|
|
|
import config
|
2012-10-20 18:05:39 +00:00
|
|
|
import os.path, uuid
|
|
|
|
|
|
|
|
@app.route('/rest/stream.view')
|
|
|
|
def stream_media():
|
|
|
|
id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
|
|
|
|
if not id:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(10, 'Missing media id')
|
2012-10-20 18:05:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
tid = uuid.UUID(id)
|
|
|
|
except:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(0, 'Invalid media id')
|
2012-10-20 18:05:39 +00:00
|
|
|
|
|
|
|
track = Track.query.get(tid)
|
|
|
|
if not track:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(70, 'Media not found'), 404
|
2012-10-20 18:05:39 +00:00
|
|
|
|
|
|
|
if maxBitRate:
|
|
|
|
try:
|
|
|
|
maxBitRate = int(maxBitRate)
|
|
|
|
except:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(0, 'Invalid bitrate value')
|
2012-10-20 18:05:39 +00:00
|
|
|
|
|
|
|
if track.bitrate > maxBitRate:
|
|
|
|
# TODO transcode
|
|
|
|
pass
|
|
|
|
|
|
|
|
if format != 'mp3':
|
|
|
|
# TODO transcode
|
|
|
|
pass
|
|
|
|
|
|
|
|
if estimateContentLength == 'true':
|
|
|
|
return send_file(track.path), 200, { 'Content-Length': os.path.getsize(track.path) }
|
|
|
|
|
|
|
|
return send_file(track.path)
|
|
|
|
|
2012-11-11 20:39:26 +00:00
|
|
|
@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)
|
|
|
|
|