views.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from account.decorators import login_required
  2. from file.models import File
  3. from django.http import FileResponse
  4. from django.utils.http import urlquote
  5. from folder.models import Folder
  6. from .judgement_function import judge_filepath
  7. from account.models import get_user
  8. from utils.debug import debug_view
  9. from utils.http import make_json_response
  10. from utils.permission import can_delete
  11. from utils.crypto import secure_transport
  12. from utils.crypto import get_file_encrypt_cipher, get_padding
  13. import base64
  14. # Create your views here.
  15. @secure_transport
  16. @debug_view(template_name='upload_file.html')
  17. @login_required
  18. def upload_file(request):
  19. data = request.POST
  20. user = get_user(request)
  21. key = data.get('key', '')
  22. if key:
  23. file_b64 = data.get('file_b64')
  24. if not file_b64:
  25. return make_json_response(code=400, error='文件不存在')
  26. file_name = data.get('file_name')
  27. else:
  28. try:
  29. file_obj = request.FILES.get('file')
  30. except:
  31. return make_json_response(code=400, error='文件不存在')
  32. file_name = file_obj.name
  33. file_type = judge_filepath(file_name.split('.')[-1].lower()) if '.' in file_name else ''
  34. father_folder_id = data.get('father_folder_id')
  35. try:
  36. folder = Folder.objects.get(folder_id=father_folder_id)
  37. except:
  38. return make_json_response(code=402, error='文件夹不存在')
  39. if not folder.check_permission(user=user):
  40. return make_json_response(code=404, error='没有上传文件的权限')
  41. file = File.objects.create(file_name=file_name,
  42. father_folder=folder,
  43. file_type=file_type,
  44. owner=user,
  45. group=folder.group,
  46. key=key)
  47. try:
  48. file_path = file.get_path()
  49. with open(file_path, 'wb+') as f:
  50. if key:
  51. print(key)
  52. file.file_type, content_b64 = file_b64.split(',')
  53. file.save()
  54. file_bytes = base64.b64decode(content_b64)
  55. file_bytes += get_padding(file_bytes)
  56. enc_file_bytes = get_file_encrypt_cipher(key).encrypt(file_bytes)
  57. f.write(enc_file_bytes)
  58. else:
  59. for chunk in file_obj.chunks():
  60. f.write(chunk)
  61. except Exception as e:
  62. print(e)
  63. file.delete()
  64. return make_json_response(code=500, error='文件保存失败')
  65. return make_json_response()
  66. @secure_transport
  67. # @debug_view('file_id')
  68. @login_required
  69. def download_file(request):
  70. user = get_user(request)
  71. file_id = request.POST.get('file_id')
  72. try:
  73. file = File.objects.get(file_id=file_id)
  74. except:
  75. return make_json_response(code=402, error='文件不存在')
  76. if not file.father_folder.check_permission(user=user):
  77. return make_json_response(code=404, error='没有下载文件的权限')
  78. try:
  79. file_path = file.get_path()
  80. f = open(file_path, 'rb')
  81. except:
  82. return make_json_response(code=500, error='文件读取失败')
  83. if file.key:
  84. try:
  85. enc_file_bytes = f.read()
  86. file_bytes = get_file_encrypt_cipher(file.key).decrypt(enc_file_bytes)
  87. content_b64 = base64.b64encode(file_bytes).decode()
  88. file_b64 = f'{file.file_type},{content_b64}'
  89. f.close()
  90. except Exception as e:
  91. print(e)
  92. return make_json_response(code=500, error='文件读取失败')
  93. return make_json_response(file_b64=file_b64, **file.to_json())
  94. else:
  95. file_bytes = f.read()
  96. content_b64 = base64.b64encode(file_bytes).decode()
  97. file_b64 = f'{file.file_type},{content_b64}'
  98. f.close()
  99. return make_json_response(file_b64=file_b64, **file.to_json())
  100. # file_name = file.file_name
  101. # response = FileResponse(f)
  102. # response['Content-Type'] = 'application/octet-stream'
  103. # response['Content-Disposition'] = 'attachment;filename={}'.format(urlquote(file_name))
  104. # return response
  105. @secure_transport
  106. # @debug_view('file_id')
  107. @login_required
  108. def delete_file(request):
  109. data = request.POST
  110. user = get_user(request)
  111. file_id = data.get('file_id')
  112. try:
  113. file = File.objects.get(file_id=file_id)
  114. except:
  115. return make_json_response(code=402, error='文件不存在')
  116. if not can_delete(user=user, f=file):
  117. return make_json_response(code=404, error='没有删除文件的权限')
  118. file.delete()
  119. return make_json_response()