From 97603dc6df19259c501d03332b53a50b659a7c29 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Sun, 2 Oct 2011 19:40:23 +0100 Subject: [PATCH] use X-SENDFILE by default --- server/djrandom/frontend/views.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/server/djrandom/frontend/views.py b/server/djrandom/frontend/views.py index ca83889..8c18737 100644 --- a/server/djrandom/frontend/views.py +++ b/server/djrandom/frontend/views.py @@ -11,6 +11,7 @@ from djrandom.frontend import app, require_auth from sqlalchemy import distinct, func log = logging.getLogger(__name__) +use_xsendfile = 1 @app.route('/autocomplete') @@ -75,13 +76,7 @@ def fileiter(path, pos, end): pos += len(chunk) -@app.route('/dl/<sha1>') -@require_auth -def download_song(sha1): - mp3 = MP3.query.get(sha1) - if not mp3: - abort(404) - +def download_song_python(mp3): # Parse 'Range' HTTP header. start = 0 end = os.path.getsize(mp3.path) @@ -100,6 +95,7 @@ def download_song(sha1): else: end = int(hrange) + # Create response with content iterator. response = Response(fileiter(mp3.path, start, end), status=(partial and 206 or 200), direct_passthrough=True) @@ -110,3 +106,22 @@ def download_song(sha1): response.content_type = 'audio/mpeg' response.content_length = (end - start) return response + + +def download_song_xsendfile(mp3): + return Response('', status=200, + content_type = 'audio/mpeg', + headers={'X-SENDFILE': mp3.path}) + + +@app.route('/dl/<sha1>') +@require_auth +def download_song(sha1): + mp3 = MP3.query.get(sha1) + if not mp3: + abort(404) + if use_xsendfile: + return download_song_xsendfile(mp3) + else: + return download_song_python(mp3) + -- GitLab