Commit 3a492620 by 周田

fix:删除没用的文件

parent ebd02fbc
...@@ -84,33 +84,41 @@ class AllProtocolVersionViewSet(GenericViewSet, ListModelMixin): ...@@ -84,33 +84,41 @@ class AllProtocolVersionViewSet(GenericViewSet, ListModelMixin):
@api_view(['POST']) @api_view(['POST'])
@parser_classes([MultiPartParser]) @parser_classes([MultiPartParser])
def raw_file_upload(request): def raw_file_upload(request):
# 将上传来的文件保存下来
file_obj = request.FILES.get('file') file_obj = request.FILES.get('file')
protocol_name = request.data.get('protocol_name') protocol_name = request.data.get('protocol_name')
version = request.data.get('version') version = request.data.get('version')
assert protocol_name is not None and version is not None, Response(status=status.HTTP_400_BAD_REQUEST) assert protocol_name is not None and version is not None, Response(status=status.HTTP_400_BAD_REQUEST)
try: # 构建文件夹路径和文件路径
folder_path = os.path.join(settings.BASE_DIR, 'protocol_raw_files', protocol_name, version) folder_path = os.path.join(settings.BASE_DIR, 'protocol_raw_files', protocol_name, version)
if not os.path.exists(folder_path): if not os.path.exists(folder_path):
os.makedirs(folder_path) os.makedirs(folder_path)
file_path = os.path.join(folder_path, file_obj.name) file_path = os.path.join(folder_path, file_obj.name)
with open(file_path, 'wb') as f:
for chunk in file_obj.chunks():
f.write(chunk)
except Exception as e:
print(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# 更新协议版本信息,将文件路径保存下来 # 更新协议版本信息,将文件路径保存下来
protocol_versions = AllProtocolVersion.objects.filter(protocol_name=protocol_name).first() protocol_versions = AllProtocolVersion.objects.filter(protocol_name=protocol_name).first()
version_path_list = json.loads(protocol_versions.version_paths) version_path_list = json.loads(protocol_versions.version_paths)
for version_path in version_path_list: for version_path in version_path_list:
if version_path['version'] == version: if version_path['version'] == version:
if version_path.get('path', None) is not None: if version_path.get('path', None) is not None:
os.remove(version_path['path']) try:
os.remove(version_path['path'])
except Exception as e:
print(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
version_path['path'] = file_path version_path['path'] = file_path
break break
# 将上传来的文件保存下来
try:
with open(file_path, 'wb') as f:
for chunk in file_obj.chunks():
f.write(chunk)
except Exception as e:
print(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# 将路径保存下来
protocol_versions.version_paths = json.dumps(version_path_list) protocol_versions.version_paths = json.dumps(version_path_list)
protocol_versions.save() protocol_versions.save()
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment