HlsManage.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/5/30 17:53
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: HlsManage.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.views.generic.base import View
  15. from django.utils.decorators import method_decorator
  16. from django.views.decorators.csrf import csrf_exempt
  17. from Service.ModelService import ModelService
  18. from Model.models import Device_Meal
  19. from Object.AWS.S3ClassObject import S3ClassObject
  20. from Object.AWS.CloudfrontSignUrl import CloudfrontSignUrl
  21. from Object.encryHlsVodUrl import getSignUrl
  22. from Object.m3u8generate import PlaylistGenerator
  23. from Object.TokenObject import TokenObject
  24. from Object.ResponseObject import ResponseObject
  25. from django.http import HttpResponse
  26. '''
  27. 设备端
  28. http://13.56.215.252:82/HlsManager?operation=signPutObj&key=555666.mp4&uid=L59KVYDAEPHR1T6M111A&channel=0
  29. http://13.56.215.252:82/HlsManager?operation=signPostObj&key=ppp.mp4&uid=L59KVYDAEPHR1T6M111A&channel=0
  30. 移动端
  31. http://13.56.215.252:82/HlsManager?operation=getHlsVod&uid=L59KVYDAEPHR1T6M111A&channel=0&token=test&key=234234234.ts
  32. http://13.56.215.252:82/HlsManager?operation=getAllTs&uid=L59KVYDAEPHR1T6M111A&channel=0&token=test
  33. http://13.56.215.252:82/HlsManager?operation=getVodUrl&uid=L59KVYDAEPHR1T6M111A&channel=0&token=test&key=121212.mp4
  34. '''
  35. class HlsManage(View):
  36. @method_decorator(csrf_exempt)
  37. def dispatch(self, *args, **kwargs):
  38. return super(HlsManage, self).dispatch(*args, **kwargs)
  39. def get(self, request, *args, **kwargs):
  40. request.encoding = 'utf-8'
  41. return self.validation(request_dict=request.GET)
  42. def post(self, request, *args, **kwargs):
  43. request.encoding = 'utf-8'
  44. return self.validation(request_dict=request.POST)
  45. def validation(self, request_dict, *args, **kwargs):
  46. response = ResponseObject()
  47. operation = request_dict.get('operation', None)
  48. if operation == 'signPutObj':
  49. return self.sign_put_obj(request_dict,response)
  50. elif operation == 'signPostObj':
  51. return self.sign_post_obj(request_dict,response)
  52. elif operation == 'getHlsVod':
  53. return self.get_hls_vod(request_dict,response)
  54. elif operation == 'getAllTs':
  55. return self.get_all_ts(request_dict,response)
  56. elif operation == 'getVodUrl':
  57. return self.get_vod_url(request_dict,response)
  58. else:
  59. return response.json(444, 'operation')
  60. def sign_put_obj(self, request_dict,response):
  61. key = request_dict.get('key', None)
  62. uid = request_dict.get('uid', None)
  63. channel = request_dict.get('channel', None)
  64. if key is not None and uid is not None and channel is not None:
  65. device_meal_queryset = Device_Meal.objects.filter(uid=uid, channel=channel, status=1)
  66. if device_meal_queryset.exists():
  67. path = uid + '_' + channel + '/' + key
  68. meal_id = device_meal_queryset[0].rank_id
  69. bucket_meal={
  70. 1:'ansjer.meal.1',# 7
  71. 2:'ansjer.meal.2',# 30
  72. # 3:'ansjer.meal.3',
  73. }
  74. s3 = S3ClassObject()
  75. sign_url = s3.sign_put_object(key=path, bucket_meal=bucket_meal[meal_id])
  76. if sign_url is not False:
  77. return (0, {'url': sign_url})
  78. else:
  79. return response.json(48)
  80. else:
  81. return response.json(444)
  82. else:
  83. return response.json(444,'key, uid, channel')
  84. def sign_post_obj(self, request_dict,response):
  85. key = request_dict.get('key', None)
  86. uid = request_dict.get('uid', None)
  87. channel = request_dict.get('channel', None)
  88. if key is not None and uid is not None and channel is not None:
  89. device_meal_queryset = Device_Meal.objects.filter(uid=uid, channel=channel, status=1)
  90. if device_meal_queryset.exists():
  91. path = uid + '_' + channel + '/' + key
  92. meal_id = device_meal_queryset[0].rank_id
  93. bucket_meal = {
  94. 1: 'ansjer.meal.1', # 7
  95. 2: 'ansjer.meal.2', # 30
  96. # 3:'ansjer.meal.3',
  97. }
  98. s3 = S3ClassObject()
  99. data = s3.sign_post_object(key=path, bucket_meal=bucket_meal[meal_id])
  100. if data is not False:
  101. return response.json(0, {'url': data})
  102. else:
  103. return response.json(48)
  104. else:
  105. return response.json(444)
  106. else:
  107. return response.json(444,'key, uid, channel')
  108. def get_all_ts(self, request_dict,response):
  109. token = request_dict.get('token', None)
  110. if token is not None:
  111. tko = TokenObject(token)
  112. response.lang = tko.lang
  113. if tko.code == 0:
  114. userID = tko.userID
  115. if userID is not None:
  116. uid = request_dict.get('uid', None)
  117. channel = request_dict.get('channel', None)
  118. if uid is not None and channel is not None:
  119. own_permission = ModelService.check_perm(userID=userID, permID=30)
  120. own_device = ModelService.check_own_device(userID=userID, UID=uid)
  121. if own_permission is True or own_device is True:
  122. device_meal_queryset = Device_Meal.objects.filter(uid=uid, channel=channel, status=1)
  123. if device_meal_queryset.exists():
  124. meal_id = device_meal_queryset[0].rank_id
  125. bucket_meal = {
  126. 1: 'ansjer.meal.1', # 7
  127. 2: 'ansjer.meal.2', # 30
  128. }
  129. s3 = S3ClassObject()
  130. data = s3.get_prefix_obj(prefix=uid + '_' + channel + '/', bucket=bucket_meal[meal_id])
  131. # return response.json(0, {'files': data,'bk':bucket_meal[meal_id]})
  132. return response.json(0, {'files': data})
  133. else:
  134. return response.json(444)
  135. else:
  136. return response.json(404)
  137. else:
  138. return response.json(444,'uid,channel')
  139. else:
  140. return response.json(309)
  141. else:
  142. return response.json(tko.code)
  143. else:
  144. return response.json(309)
  145. def get_hls_vod(self, request_dict,response):
  146. token = request_dict.get('token', None)
  147. if token is not None:
  148. tko = TokenObject(token)
  149. response.lang = tko.lang
  150. if tko.code == 0:
  151. userID = tko.userID
  152. if userID is not None:
  153. key = request_dict.get('key', None)
  154. uid = request_dict.get('uid', None)
  155. channel = request_dict.get('channel', None)
  156. own_device = ModelService.check_own_device(userID=userID, UID=uid)
  157. if own_device is True:
  158. device_meal_queryset = Device_Meal.objects.filter(uid=uid, channel=channel, status=1)
  159. if device_meal_queryset.exists():
  160. meal_id = device_meal_queryset[0].rank_id
  161. cf_domain = {
  162. 1: 'http://d3om3d8c0l14oc.cloudfront.net/',
  163. 2: 'http://d4neaolnu8h83.cloudfront.net/',
  164. }
  165. url = cf_domain[meal_id] + uid + '_' + channel + '/' + key
  166. sign_url = CloudfrontSignUrl(url=url)
  167. playlist_entries = [
  168. {
  169. 'name': sign_url,
  170. 'duration': '60',
  171. }
  172. ]
  173. playlist = PlaylistGenerator(playlist_entries).generate()
  174. response = HttpResponse(content_type='application/force-download', content=playlist)
  175. response['Content-Disposition'] = 'attachment; filename=' + key + '.m3u8'
  176. # response['X-Sendfile'] = playlist
  177. # return response.json(404)
  178. return response
  179. # return HttpResponse(playlist)
  180. else:
  181. return response.json(0)
  182. else:
  183. return response.json(15)
  184. else:
  185. return response.json(309)
  186. else:
  187. return response.json(tko.code)
  188. else:
  189. return response.json(309)
  190. # mp4
  191. def get_vod_url(self, request_dict,response):
  192. token = request_dict.get('token', None)
  193. if token is not None:
  194. tko = TokenObject(token)
  195. response.lang = tko.lang
  196. if tko.code == 0:
  197. userID = tko.userID
  198. key = request_dict.get('key', None)
  199. uid = request_dict.get('uid', None)
  200. channel = request_dict.get('channel', None)
  201. if userID is not None:
  202. own_device = ModelService.check_own_device(userID=userID, UID=uid)
  203. if own_device is True:
  204. device_meal_queryset = Device_Meal.objects.filter(uid=uid, channel=channel, status=1)
  205. if device_meal_queryset.exists():
  206. meal_id = device_meal_queryset[0].rank_id
  207. bucket_meal = {
  208. 1: 'ansjer.meal.1', # 7
  209. 2: 'ansjer.meal.2', # 30
  210. }
  211. bk = bucket_meal[meal_id]
  212. path = '/'+bk+'/'+uid+'_'+channel+'/'+key
  213. signUrl = getSignUrl(path=path)
  214. return response.json(0,{'signUrl':signUrl})
  215. else:
  216. return response.json(0)
  217. else:
  218. return response.json(15)
  219. else:
  220. return response.json(309)
  221. else:
  222. return response.json(tko.code)
  223. else:
  224. return response.json(309)