From ad0a2a7b038a03e27a35564598eb32843b9aced1 Mon Sep 17 00:00:00 2001 From: j Date: Sun, 22 Feb 2015 16:38:06 +0530 Subject: [PATCH] add range request support --- oml/item/handlers.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/oml/item/handlers.py b/oml/item/handlers.py index aa410e5..1339abc 100644 --- a/oml/item/handlers.py +++ b/oml/item/handlers.py @@ -45,12 +45,28 @@ class EpubHandler(OMLHandler): def serve_static(handler, path, mimetype, include_body=True, disposition=None): #fixme use static file handler / serve ranges handler.set_header('Content-Type', mimetype) - handler.set_header('Content-Length', str(os.stat(path).st_size)) + size = os.stat(path).st_size + handler.set_header('Accept-Ranges', 'bytes') if disposition: handler.set_header('Content-Disposition', "attachment; filename*=UTF-8''%s" % quote(disposition.encode('utf-8'))) if include_body: - with open(path, 'rb') as fd: - handler.write(fd.read()) + if 'Range' in handler.request.headers: + handler.set_status(206) + r = handler.request.headers.get('Range').split('=')[-1].split('-') + start = int(r[0]) + end = int(r[1]) if r[1] else size + length = end - start + handler.set_header('Content-Length', str(length)) + handler.set_header('Content-Range', 'bytes %s-%s/%s' % (start, end-1, size)) + with open(path, 'rb') as fd: + fd.seek(start) + handler.write(fd.read(length)) + else: + handler.set_header('Content-Length', str(size)) + with open(path, 'rb') as fd: + handler.write(fd.read()) + else: + handler.set_header('Content-Length', str(size)) return class FileHandler(OMLHandler):