app_5.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. def upload():
  2. r = Response(mimetype="application/json")
  3. r.headers["Content-Type"] = "text/json; charset=utf-8"
  4. if 'upload_phase' in request.values and request.values['upload_phase'] == 'start':
  5. if 'size' not in request.values:
  6. r.response = json.dumps({'error': 'provide file size'})
  7. r.status_code = HTTPStatus.BAD_REQUEST
  8. return r
  9. else:
  10. if UPLOAD_MAX_SIZE < int(request.values['size']):
  11. r.response = json.dumps({'error': 'file too large'})
  12. r.status_code = HTTPStatus.BAD_REQUEST
  13. return r
  14. else:
  15. # create a session
  16. size = int(request.values['size'])
  17. new_session_id = get_cache().incr("upload:session:id", 1)
  18. token = request.headers['Authorization'].split(' ')[1]
  19. data = jwt.decode(token.encode('UTF-8'), 'SECRET', algorithm='HS256')
  20. table = get_db()['uploads']
  21. mydict = {'status': 'start','users':[data['sub']]}
  22. x = table.insert_one(mydict)
  23. upload_id = x.inserted_id
  24. upload_redis_key = "{0}:{1}".format("upload:session", new_session_id)
  25. get_cache().hset(upload_redis_key, "state", "start")
  26. get_cache().hset(upload_redis_key, "size", size)
  27. get_cache().hset(upload_redis_key, "start_offset", 0)
  28. get_cache().hset(upload_redis_key, "upload_id", str(upload_id))
  29. start_offset = 0
  30. end_offset = CHUNK_SIZE
  31. if size <= CHUNK_SIZE:
  32. end_offset = size
  33. get_cache().hset(upload_redis_key, "end_offset", end_offset)
  34. response_body = {
  35. 'upload_session_id': new_session_id,
  36. 'upload_id': str(upload_id),
  37. 'start_offset': start_offset,
  38. 'end_offset': end_offset,
  39. }
  40. r.response = json.dumps(response_body)
  41. r.status_code = HTTPStatus.OK
  42. else:
  43. if 'upload_session_id' not in request.values or 'video_file_chunk' not in request.values:
  44. r.response = json.dumps({'error': 'provide upload session id and video file chunk'})
  45. r.status_code = HTTPStatus.BAD_REQUEST
  46. return r
  47. upload_session_id = request.values['upload_session_id']
  48. upload_redis_key = "{0}:{1}".format("upload:session", upload_session_id)
  49. uploadid = get_cache().hget(upload_redis_key, 'upload_id')
  50. start_offset = int(get_cache().hget(upload_redis_key, 'end_offset'))
  51. end_offset = int(get_cache().hget(upload_redis_key, 'end_offset'))
  52. size = int(get_cache().hget(upload_redis_key, 'size'))
  53. print(end_offset)
  54. if end_offset == size:
  55. response_body = {
  56. 'upload_session_id': upload_session_id,
  57. 'uploadid': uploadid,
  58. 'start_offset': end_offset,
  59. 'end_offset': end_offset,
  60. 'status': 'done'
  61. }
  62. # complete it
  63. r.response = json.dumps(response_body)
  64. token = request.headers['Authorization'].split(' ')[1]
  65. data = jwt.decode(token.encode('UTF-8'), 'SECRET', algorithm='HS256')
  66. get_db()['uploads'].update_one({'_id': ObjectId(uploadid)},
  67. {'$set': {'ownerid': data['sub'], 'status': 'done'}})
  68. # get_db()['users'].update({'name': data['sub']}, {'$push': {'uploads': uploadid}})
  69. elif (start_offset + CHUNK_SIZE) >= size:
  70. end_offset = size
  71. response_body = {
  72. 'upload_session_id': upload_session_id,
  73. 'uploadid': uploadid,
  74. 'start_offset': start_offset,
  75. 'end_offset': end_offset,
  76. }
  77. r.response = json.dumps(response_body)
  78. else:
  79. end_offset = start_offset + CHUNK_SIZE
  80. # start_offset = start_offset
  81. response_body = {
  82. 'upload_session_id': upload_session_id,
  83. 'uploadid':uploadid,
  84. 'start_offset': start_offset,
  85. 'end_offset': end_offset,
  86. }
  87. r.response = json.dumps(response_body)
  88. get_cache().hset(upload_redis_key, "start_offset", start_offset)
  89. get_cache().hset(upload_redis_key, "end_offset", end_offset)
  90. video_file_chunk = request.values['video_file_chunk']
  91. uploadid = get_cache().hget(upload_redis_key, 'upload_id')
  92. print(video_file_chunk)
  93. get_db()['uploads'].update_one({'_id': ObjectId(uploadid)}, {'$push': {'chunks': video_file_chunk}})
  94. return r
  95. if __name__ == "__main__":
  96. # app.run(port=8081)
  97. app.run(host="0.0.0.0")
  98. # ,ssl_context=("/etc/ssl/certs/pythonusersapi/cert.pem","/etc/ssl/certs/pythonusersapi/key.pem"),port=8081)