Skip to content

Commit bc2212c

Browse files
Add example for movie streaming range request support
1 parent d688ae1 commit bc2212c

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

375 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'''A simple streaming movie server example'''
2+
import hug
3+
4+
5+
@hug.get(output=hug.output_format.mp4_video)
6+
def watch():
7+
'''Watch an example movie, streamed directly to you from hug'''
8+
return 'movie.mp4'

hug/decorators.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,26 @@ def interface(request, response, api_version=None, **kwargs):
365365
else:
366366
to_return = transform(to_return)
367367

368-
to_return = (function_output(to_return, **function_output_kwargs) if
369-
not hasattr(to_return, 'read') else to_return)
368+
to_return = function_output(to_return, **function_output_kwargs)
370369
if hasattr(to_return, 'read'):
371-
response.stream = to_return
370+
size = None
372371
if hasattr(to_return, 'name') and os.path.isfile(to_return.name):
373-
response.stream_len = os.path.getsize(to_return.name)
372+
size = os.path.getsize(to_return.name)
373+
if request.range and size:
374+
start, end = request.range
375+
if end < 0:
376+
end = size + end
377+
end = min(end, size)
378+
length = end - start + 1
379+
to_return.seek(start)
380+
response.data = to_return.read(length)
381+
response.status = falcon.HTTP_206
382+
response.content_range = (start, end, size)
383+
to_return.close()
384+
else:
385+
response.stream = to_return
386+
if size:
387+
response.stream_len = size
374388
else:
375389
response.data = to_return
376390

0 commit comments

Comments
 (0)