forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanelVideo.py
62 lines (52 loc) · 1.89 KB
/
panelVideo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: hwliang <2020-05-18>
# +-------------------------------------------------------------------
# +-------------------------------------------------------------------
# | 流媒体模块
# +-------------------------------------------------------------------
import os,sys,re
import mimetypes
import public
from BTPanel import Response
def get_buff_size(file_size):
buff_size = 2097152
if file_size > 1073741824:
buff_size = 4194304
return buff_size
def partial_response(path, start, end=None):
if not os.path.exists(path):
return Response("file not fount!",404)
file_size = os.path.getsize(path)
buff_size = get_buff_size(file_size)
if end is None:
end = start + buff_size - 1
end = min(end, file_size - 1)
end = min(end, start + buff_size - 1)
length = end - start + 1
with open(path, 'rb') as fd:
fd.seek(start)
bytes = fd.read(length)
assert len(bytes) == length
response = Response(bytes,206,mimetype=mimetypes.guess_type(path)[0],direct_passthrough=True,)
response.headers.add('Content-Range', 'bytes {0}-{1}/{2}'.format(start, end, file_size,),)
response.headers.add('Accept-Ranges', 'bytes')
return response
def get_range(request):
range = request.headers.get('Range')
m = None
if range:
m = re.match(r'bytes=(?P<start>\d+)-(?P<end>\d+)?', range)
if m:
start = m.group('start')
end = m.group('end')
start = int(start)
if end is not None:
end = int(end)
return start, end
else:
return 0, None