2012-10-20 18:05:39 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
from flask import request, send_file, Response
|
2012-12-02 15:42:25 +00:00
|
|
|
import os.path
|
2013-06-07 13:31:30 +00:00
|
|
|
from PIL import Image
|
2013-10-15 11:14:20 +00:00
|
|
|
import subprocess
|
2013-11-03 20:46:19 +00:00
|
|
|
import shlex
|
2013-11-04 02:38:03 +00:00
|
|
|
import mutagen
|
2013-11-26 07:59:08 +00:00
|
|
|
import fnmatch
|
|
|
|
import mimetypes
|
2012-11-23 16:13:25 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
import config, scanner
|
2012-11-23 16:13:25 +00:00
|
|
|
from web import app
|
2013-12-07 17:46:30 +00:00
|
|
|
from db import Track, Folder, User, ClientPrefs, now, session
|
2012-12-02 15:42:25 +00:00
|
|
|
from api import get_entity
|
2012-10-20 18:05:39 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
from flask import g
|
|
|
|
|
|
|
|
def after_this_request(func):
|
|
|
|
if not hasattr(g, 'call_after_request'):
|
|
|
|
g.call_after_request = []
|
|
|
|
g.call_after_request.append(func)
|
|
|
|
return func
|
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
def per_request_callbacks(response):
|
|
|
|
for func in getattr(g, 'call_after_request', ()):
|
|
|
|
response = func(response)
|
|
|
|
return response
|
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
def prepare_transcoding_cmdline(base_cmdline, input_file, input_format, output_format, output_bitrate):
|
|
|
|
if not base_cmdline:
|
|
|
|
return None
|
2013-11-03 20:46:19 +00:00
|
|
|
|
|
|
|
return base_cmdline.replace('%srcpath', '"'+input_file+'"').replace('%srcfmt', input_format).replace('%outfmt', output_format).replace('%outrate', str(output_bitrate))
|
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
|
2012-11-22 13:51:43 +00:00
|
|
|
@app.route('/rest/stream.view', methods = [ 'GET', 'POST' ])
|
2012-10-20 18:05:39 +00:00
|
|
|
def stream_media():
|
2013-12-09 07:18:02 +00:00
|
|
|
|
|
|
|
@after_this_request
|
|
|
|
def add_header(response):
|
|
|
|
if 'X-Sendfile' in response.headers:
|
|
|
|
xsendfile = response.headers['X-Sendfile'] or ''
|
|
|
|
redirect = config.get('base', 'accel-redirect')
|
|
|
|
if redirect and xsendfile:
|
|
|
|
response.headers['X-Accel-Charset'] = 'utf-8'
|
|
|
|
response.headers['X-Accel-Redirect'] = redirect + xsendfile.encode('UTF8')
|
2013-12-29 19:30:12 +00:00
|
|
|
app.logger.debug('X-Accel-Redirect: ' + redirect + xsendfile)
|
2013-12-09 07:18:02 +00:00
|
|
|
return response
|
2014-01-14 05:18:46 +00:00
|
|
|
|
|
|
|
def transcode(process):
|
|
|
|
try:
|
|
|
|
for chunk in iter(process.stdout.readline, ''):
|
|
|
|
yield chunk
|
|
|
|
process.wait()
|
|
|
|
except:
|
|
|
|
app.logger.debug('transcoding timeout, killing process')
|
|
|
|
process.terminate()
|
|
|
|
process.wait()
|
|
|
|
|
2012-12-02 15:42:25 +00:00
|
|
|
status, res = get_entity(request, Track)
|
2013-11-01 18:46:16 +00:00
|
|
|
|
2012-12-02 15:42:25 +00:00
|
|
|
if not status:
|
2013-11-26 07:59:08 +00:00
|
|
|
return res
|
2012-10-20 18:05:39 +00:00
|
|
|
|
2013-12-07 17:46:30 +00:00
|
|
|
maxBitRate, format, timeOffset, size, estimateContentLength, client = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength', 'c' ])
|
2013-10-15 08:32:35 +00:00
|
|
|
if format:
|
|
|
|
format = format.lower()
|
2012-10-20 18:05:39 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
do_transcoding = False
|
|
|
|
src_suffix = res.suffix()
|
2013-11-26 07:59:08 +00:00
|
|
|
dst_suffix = src_suffix
|
2013-10-15 11:14:20 +00:00
|
|
|
dst_bitrate = res.bitrate
|
2013-11-26 07:59:08 +00:00
|
|
|
dst_mimetype = mimetypes.guess_type('a.' + src_suffix)
|
2013-10-15 11:14:20 +00:00
|
|
|
|
2013-12-14 19:51:14 +00:00
|
|
|
if maxBitRate:
|
|
|
|
try:
|
|
|
|
maxBitRate = int(maxBitRate)
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid bitrate value')
|
2012-10-20 18:05:39 +00:00
|
|
|
|
2013-12-14 19:51:14 +00:00
|
|
|
if dst_bitrate > maxBitRate and maxBitRate != 0:
|
2013-10-15 11:14:20 +00:00
|
|
|
do_transcoding = True
|
2013-12-14 19:51:14 +00:00
|
|
|
dst_bitrate = maxBitRate
|
|
|
|
|
|
|
|
if format and format != 'raw' and format != src_suffix:
|
|
|
|
do_transcoding = True
|
|
|
|
dst_suffix = format
|
|
|
|
dst_mimetype = mimetypes.guess_type(dst_suffix)
|
2013-10-15 11:14:20 +00:00
|
|
|
|
2013-12-07 17:46:30 +00:00
|
|
|
if client:
|
|
|
|
prefs = ClientPrefs.query.get((request.user.id, client))
|
|
|
|
if not prefs:
|
|
|
|
prefs = ClientPrefs(user_id = request.user.id, client_name = client)
|
|
|
|
session.add(prefs)
|
2012-10-20 18:05:39 +00:00
|
|
|
|
2013-12-07 17:46:30 +00:00
|
|
|
if prefs.format:
|
|
|
|
dst_suffix = prefs.format
|
|
|
|
if prefs.bitrate and prefs.bitrate < dst_bitrate:
|
|
|
|
dst_bitrate = prefs.bitrate
|
2013-10-15 11:14:20 +00:00
|
|
|
|
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
if not format and src_suffix == 'flac':
|
|
|
|
dst_suffix = 'ogg'
|
|
|
|
dst_bitrate = 320
|
2013-11-26 07:59:08 +00:00
|
|
|
dst_mimetype = 'audio/ogg'
|
2013-11-03 20:46:19 +00:00
|
|
|
do_transcoding = True
|
|
|
|
|
2013-11-04 02:38:03 +00:00
|
|
|
duration = mutagen.File(res.path).info.length
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Serving file: ' + res.path + '\n\tDuration of file: ' + str(duration))
|
2013-11-04 02:38:03 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
if do_transcoding:
|
|
|
|
transcoder = config.get('transcoding', 'transcoder_{}_{}'.format(src_suffix, dst_suffix))
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
decoder = config.get('transcoding', 'decoder_' + src_suffix) or config.get('transcoding', 'decoder')
|
|
|
|
encoder = config.get('transcoding', 'encoder_' + dst_suffix) or config.get('transcoding', 'encoder')
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
if not transcoder and (not decoder or not encoder):
|
|
|
|
transcoder = config.get('transcoding', 'transcoder')
|
|
|
|
if not transcoder:
|
|
|
|
return request.error_formatter(0, 'No way to transcode from {} to {}'.format(src_suffix, dst_suffix))
|
2013-10-14 16:36:45 +00:00
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
transcoder, decoder, encoder = map(lambda x: prepare_transcoding_cmdline(x, res.path, src_suffix, dst_suffix, dst_bitrate), [ transcoder, decoder, encoder ])
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2013-11-04 19:00:15 +00:00
|
|
|
decoder = map(lambda s: s.decode('UTF8'), shlex.split(decoder.encode('utf8')))
|
|
|
|
encoder = map(lambda s: s.decode('UTF8'), shlex.split(encoder.encode('utf8')))
|
|
|
|
transcoder = map(lambda s: s.decode('UTF8'), shlex.split(transcoder.encode('utf8')))
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug(str( decoder ) + '\n' + str( encoder ) + '\n' + str(transcoder))
|
2013-11-04 19:00:15 +00:00
|
|
|
|
|
|
|
if '|' in transcoder:
|
2013-11-03 20:46:19 +00:00
|
|
|
pipe_index = transcoder.index('|')
|
|
|
|
decoder = transcoder[:pipe_index]
|
|
|
|
encoder = transcoder[pipe_index+1:]
|
|
|
|
transcoder = None
|
|
|
|
|
2013-12-29 19:30:12 +00:00
|
|
|
app.logger.debug('decoder' + str(decoder) + '\nencoder' + str(encoder))
|
2013-11-03 20:46:19 +00:00
|
|
|
|
2013-10-20 15:27:20 +00:00
|
|
|
try:
|
|
|
|
if transcoder:
|
2013-11-05 16:47:08 +00:00
|
|
|
app.logger.warn('transcoder: '+str(transcoder))
|
2013-11-04 19:00:15 +00:00
|
|
|
proc = subprocess.Popen(transcoder, stdout = subprocess.PIPE, shell=False)
|
2013-10-20 15:27:20 +00:00
|
|
|
else:
|
2013-11-03 20:46:19 +00:00
|
|
|
dec_proc = subprocess.Popen(decoder, stdout = subprocess.PIPE, shell=False)
|
|
|
|
proc = subprocess.Popen(encoder, stdin = dec_proc.stdout, stdout = subprocess.PIPE, shell=False)
|
|
|
|
|
2014-01-14 05:18:46 +00:00
|
|
|
response = Response(transcode(proc), 200, {'Content-Type': dst_mimetype, 'X-Content-Duration': str(duration)})
|
2013-10-20 15:27:20 +00:00
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Error while running the transcoding process')
|
2013-10-15 11:14:20 +00:00
|
|
|
|
|
|
|
else:
|
2013-11-03 20:46:19 +00:00
|
|
|
app.logger.warn('no transcode')
|
2013-11-26 07:59:08 +00:00
|
|
|
response = send_file(res.path)
|
|
|
|
response.headers['Content-Type'] = dst_mimetype
|
|
|
|
response.headers['Accept-Ranges'] = 'bytes'
|
2013-11-04 02:38:03 +00:00
|
|
|
response.headers['X-Content-Duration'] = str(duration)
|
2013-10-15 11:14:20 +00:00
|
|
|
|
2013-06-07 18:35:21 +00:00
|
|
|
res.play_count = res.play_count + 1
|
|
|
|
res.last_play = now()
|
2013-06-13 16:17:33 +00:00
|
|
|
request.user.last_play = res
|
|
|
|
request.user.last_play_date = now()
|
2013-06-07 18:35:21 +00:00
|
|
|
session.commit()
|
|
|
|
|
2013-10-15 11:14:20 +00:00
|
|
|
return response
|
2012-10-20 18:05:39 +00:00
|
|
|
|
2013-06-12 19:29:42 +00:00
|
|
|
@app.route('/rest/download.view', methods = [ 'GET', 'POST' ])
|
|
|
|
def download_media():
|
|
|
|
status, res = get_entity(request, Track)
|
|
|
|
if not status:
|
|
|
|
return res
|
|
|
|
|
|
|
|
return send_file(res.path)
|
|
|
|
|
2012-11-22 13:51:43 +00:00
|
|
|
@app.route('/rest/getCoverArt.view', methods = [ 'GET', 'POST' ])
|
2012-11-11 20:39:26 +00:00
|
|
|
def cover_art():
|
2013-11-26 07:59:08 +00:00
|
|
|
@after_this_request
|
|
|
|
def add_header(response):
|
|
|
|
if 'X-Sendfile' in response.headers:
|
|
|
|
redirect = response.headers['X-Sendfile'] or ''
|
|
|
|
xsendfile = config.get('base', 'accel-redirect')
|
|
|
|
if redirect and xsendfile:
|
|
|
|
response.headers['X-Accel-Redirect'] = xsendfile + redirect
|
|
|
|
app.logger.debug('X-Accel-Redirect: ' + xsendfile + redirect)
|
|
|
|
return response
|
|
|
|
|
2012-12-02 15:42:25 +00:00
|
|
|
status, res = get_entity(request, Folder)
|
2013-11-26 07:59:08 +00:00
|
|
|
|
2012-12-02 15:42:25 +00:00
|
|
|
if not status:
|
|
|
|
return res
|
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Cover Art Check: ' + res.path + '/*.jp*g')
|
|
|
|
|
|
|
|
coverfile = os.listdir(res.path)
|
|
|
|
coverfile = fnmatch.filter(coverfile, '*.jp*g')
|
|
|
|
app.logger.debug('Found Images: ' + str(coverfile))
|
|
|
|
|
|
|
|
if not coverfile:
|
|
|
|
app.logger.debug('No Art Found!')
|
|
|
|
res.has_cover_art = False
|
|
|
|
session.commit()
|
2012-11-11 20:39:26 +00:00
|
|
|
return request.error_formatter(70, 'Cover art not found')
|
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
coverfile = coverfile[0]
|
|
|
|
|
2012-11-11 20:39:26 +00:00
|
|
|
size = request.args.get('size')
|
|
|
|
if size:
|
|
|
|
try:
|
|
|
|
size = int(size)
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid size value')
|
|
|
|
else:
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Serving cover art: ' + res.path + coverfile)
|
|
|
|
return send_file(os.path.join(res.path, coverfile))
|
2012-11-11 20:39:26 +00:00
|
|
|
|
2013-11-26 07:59:08 +00:00
|
|
|
im = Image.open(os.path.join(res.path, coverfile))
|
2012-11-11 20:39:26 +00:00
|
|
|
if size > im.size[0] and size > im.size[1]:
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Serving cover art: ' + res.path + coverfile)
|
|
|
|
return send_file(os.path.join(res.path, coverfile))
|
2012-11-11 20:39:26 +00:00
|
|
|
|
2013-07-15 12:37:51 +00:00
|
|
|
size_path = os.path.join(config.get('base', 'cache_dir'), str(size))
|
2012-12-02 15:42:25 +00:00
|
|
|
path = os.path.join(size_path, str(res.id))
|
2012-11-11 20:39:26 +00:00
|
|
|
if os.path.exists(path):
|
2013-11-26 07:59:08 +00:00
|
|
|
app.logger.debug('Serving cover art: ' + path)
|
2012-11-11 20:39:26 +00:00
|
|
|
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')
|
2013-11-26 07:59:08 +00:00
|
|
|
|
|
|
|
app.logger.debug('Serving cover art: ' + path)
|
2012-11-11 20:39:26 +00:00
|
|
|
return send_file(path)
|
|
|
|
|