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

Implemented stream.view

This commit is contained in:
Alban 2012-10-20 20:05:39 +02:00
parent a6301111c2
commit 79c219a7d5
3 changed files with 68 additions and 8 deletions

View File

@ -16,12 +16,8 @@ def set_formatter():
"""Return a function to create the response."""
(f, callback) = map(request.args.get, ['f', 'callback'])
if f == 'jsonp':
if not callback:
# TODO
# MiniSub has a bug, trying to retrieve jsonp without
# callback in case of getCoverArt.view
# it's not a problem because the getCoverArt should
# return a byte stream
# Some clients (MiniSub, Perisonic) set f to jsonp without callback for streamed data
if not callback and request.endpoint not in [ 'stream_media' ]:
return ResponseHelper.responsize_json({
'error': {
'code': 0,
@ -66,8 +62,10 @@ def set_content_type(response):
if not request.path.startswith('/rest/'):
return response
f = request.args.get('f')
response.headers['content-type'] = 'application/json' if f in [ 'jsonp', 'json' ] else 'text/xml'
if response.mimetype.startswith('text'):
f = request.args.get('f')
response.headers['content-type'] = 'application/json' if f in [ 'jsonp', 'json' ] else 'text/xml'
return response
@app.errorhandler(404)

61
api/media.py Executable file
View File

@ -0,0 +1,61 @@
# coding: utf-8
from flask import request, send_file
from web import app
from db import Track
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:
return request.formatter({
'error': {
'code': 10,
'message': 'Missing media id'
}
}, error = True)
try:
tid = uuid.UUID(id)
except:
return request.formatter({
'error': {
'code': 0,
'Message': 'Invalid media id'
}
}, error = True)
track = Track.query.get(tid)
if not track:
return request.formatter({
'error': {
'code': 70,
'message': 'Media not found'
}
}, error = True), 404
if maxBitRate:
try:
maxBitRate = int(maxBitRate)
except:
return request.formatter({
'error': {
'code': 0,
'message': 'Invalid bitrate value'
}
}, error = True)
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)

1
web.py
View File

@ -183,4 +183,5 @@ import api.system
import api.browse
import api.user
import api.albums_songs
import api.media