Skip to content
Snippets Groups Projects
Commit 97603dc6 authored by ale's avatar ale
Browse files

use X-SENDFILE by default

parent d2d85887
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ from djrandom.frontend import app, require_auth ...@@ -11,6 +11,7 @@ from djrandom.frontend import app, require_auth
from sqlalchemy import distinct, func from sqlalchemy import distinct, func
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
use_xsendfile = 1
@app.route('/autocomplete') @app.route('/autocomplete')
...@@ -75,13 +76,7 @@ def fileiter(path, pos, end): ...@@ -75,13 +76,7 @@ def fileiter(path, pos, end):
pos += len(chunk) pos += len(chunk)
@app.route('/dl/<sha1>') def download_song_python(mp3):
@require_auth
def download_song(sha1):
mp3 = MP3.query.get(sha1)
if not mp3:
abort(404)
# Parse 'Range' HTTP header. # Parse 'Range' HTTP header.
start = 0 start = 0
end = os.path.getsize(mp3.path) end = os.path.getsize(mp3.path)
...@@ -100,6 +95,7 @@ def download_song(sha1): ...@@ -100,6 +95,7 @@ def download_song(sha1):
else: else:
end = int(hrange) end = int(hrange)
# Create response with content iterator.
response = Response(fileiter(mp3.path, start, end), response = Response(fileiter(mp3.path, start, end),
status=(partial and 206 or 200), status=(partial and 206 or 200),
direct_passthrough=True) direct_passthrough=True)
...@@ -110,3 +106,22 @@ def download_song(sha1): ...@@ -110,3 +106,22 @@ def download_song(sha1):
response.content_type = 'audio/mpeg' response.content_type = 'audio/mpeg'
response.content_length = (end - start) response.content_length = (end - start)
return response 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment