app_4.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. def uploadshare(uploadId, shareUserId):
  2. r = Response(mimetype="application/json")
  3. r.headers["Content-Type"] = "text/json; charset=utf-8"
  4. token = request.headers['Authorization'].split(' ')[1]
  5. data = jwt.decode(token.encode('UTF-8'), 'SECRET', algorithm='HS256')
  6. upload = get_db().uploads.find_one({'_id': ObjectId(uploadId), 'status': 'done', 'ownerid': data['sub']})
  7. if upload is None:
  8. r.response = json.dumps({'error': 'upload id not found'})
  9. r.status_code = HTTPStatus.NOT_FOUND
  10. return r
  11. u = get_db().users.find_one({"name": shareUserId})
  12. if u is None:
  13. r.response = json.dumps({'error': 'user not found'})
  14. r.status_code = HTTPStatus.NOT_FOUND
  15. return r
  16. if shareUserId in upload['users']:
  17. r.response = json.dumps({'status': 'already shared'})
  18. r.status_code = HTTPStatus.OK
  19. return r
  20. get_db().uploads.update_one({'_id': ObjectId(uploadId)}, {'$push': {'users': shareUserId}})
  21. r.response = json.dumps({'status': 'share success'})
  22. r.status_code = HTTPStatus.OK
  23. return r