From 9680c51c6744780d32baa76a83a37c371dc81dc5 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 12 Apr 2020 21:54:38 -0700 Subject: use flask's send_file() conditional instead of our HTTP 206 partial handler --- README | 5 +---- run.py | 48 +----------------------------------------------- 2 files changed, 2 insertions(+), 51 deletions(-) diff --git a/README b/README index ef4e33a..8c8cf86 100644 --- a/README +++ b/README @@ -33,8 +33,5 @@ design decisions 3. XML pubDate and list order is derived from MP3 track attributes; if not present or duplicates exist, tracks are sorted alphanumerically -4. partial content handling (HTTP 206) implemented to satisfy podcasting apps - which require valid responses to Range requests (e.g. Apple podcasts) - -5. no rebuild endpoint exists; cache-affecting routines are run externally via +4. no rebuild endpoint exists; cache-affecting routines are run externally via rebuild.py diff --git a/run.py b/run.py index e33b149..3bd5e92 100755 --- a/run.py +++ b/run.py @@ -71,53 +71,7 @@ def list_books(): f_path = books[a]['files'][f]['path'] - # ship the whole file if we don't receive a Range header - range_header = request.headers.get('Range', None) - if not range_header: - return send_file( - f_path, - mimetype=mimetypes.guess_type(f_path)[0] - ) - - # partial request handling--certain podcast apps (iOS) and browsers - # (Safari) require correct replies to Range requests; if we serve the - # entire file, we're treated like a stream (no seek, duration...) - size = books[a]['files'][f]['size_bytes'] - - # if no lower bound provided, start at beginning - byte1, byte2 = 0, None - m = re.search(r'(\d+)-(\d*)', range_header) - g = m.groups() - if g[0]: - byte1 = int(g[0]) - if g[1]: - byte2 = int(g[1]) - - # if no upper bound provided, serve rest of file - length = size - byte1 - if byte2 is not None: - length = byte2 - byte1 - - # read file at byte1 for length - data = None - with open(f_path, 'rb') as f: - f.seek(byte1) - data = f.read(length) - - # create response with partial data, populate Content-Range - response = Response( - data, - 206, - mimetype=mimetypes.guess_type(f_path)[0], - direct_passthrough=True - ) - response.headers.add( - 'Content-Range', - 'bytes {0}-{1}/{2}'.format(byte1, byte1 + length, size) - ) - response.headers.add('Accept-Ranges', 'bytes') - - return response + return send_file(f_path, conditional=True) # serve up audiobook RSS feed; only audiobook hash provided elif a: -- cgit v1.2.3-54-g00ecf