diff options
author | Jordan <me@jordan.im> | 2020-04-12 21:54:38 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2020-04-12 21:54:38 -0700 |
commit | 9680c51c6744780d32baa76a83a37c371dc81dc5 (patch) | |
tree | e991a75f500df8bcad04f2872494416e36a3d630 | |
parent | a1aff012d039832ea2a6c1fc23d1e7a5840f0784 (diff) | |
download | roka-9680c51c6744780d32baa76a83a37c371dc81dc5.tar.gz roka-9680c51c6744780d32baa76a83a37c371dc81dc5.zip |
use flask's send_file() conditional instead of our HTTP 206 partial handler
-rw-r--r-- | README | 5 | ||||
-rwxr-xr-x | run.py | 48 |
2 files changed, 2 insertions, 51 deletions
@@ -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 @@ -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: |