bugfix: api/cleanup_zip
- Fix error when there are no zip files to delete
This commit is contained in:
+8
-2
@@ -201,12 +201,18 @@ def cleanup(request):
|
|||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def cleanup_zip(request):
|
def cleanup_zip(request):
|
||||||
"""Handles cleanup of all zip files leftover by api/download"""
|
"""Handles cleanup of all zip files leftover by api/download"""
|
||||||
# Delete all ZIP files in UPLOAD_DIR
|
deleted_files = []
|
||||||
|
|
||||||
for file in os.listdir(UPLOAD_DIR):
|
for file in os.listdir(UPLOAD_DIR):
|
||||||
if file.endswith(".zip"):
|
if file.endswith(".zip"):
|
||||||
file_path = os.path.join(UPLOAD_DIR, file)
|
file_path = os.path.join(UPLOAD_DIR, file)
|
||||||
try:
|
try:
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
return Response({"message": "Cleaned up zipfiles on the server"}, status=status.HTTP_200_OK)
|
deleted_files.append(file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"message": f"Error deleting {file_path}: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({"message": f"Error deleting {file_path}: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
if deleted_files:
|
||||||
|
return Response({"message": f"Deleted ZIP files: {deleted_files}"}, status=status.HTTP_200_OK)
|
||||||
|
else:
|
||||||
|
return Response({"message": "No ZIP files found to clean up."}, status=status.HTTP_200_OK)
|
||||||
|
|||||||
Reference in New Issue
Block a user