Commit 3a492620 by 周田

fix:删除没用的文件

parent ebd02fbc
......@@ -84,23 +84,16 @@ class AllProtocolVersionViewSet(GenericViewSet, ListModelMixin):
@api_view(['POST'])
@parser_classes([MultiPartParser])
def raw_file_upload(request):
# 将上传来的文件保存下来
file_obj = request.FILES.get('file')
protocol_name = request.data.get('protocol_name')
version = request.data.get('version')
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)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
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()
......@@ -108,9 +101,24 @@ def raw_file_upload(request):
for version_path in version_path_list:
if version_path['version'] == version:
if version_path.get('path', None) is not None:
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
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.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