From ddcbf740eaf50619e0109d0d9e4713bb624b17ab Mon Sep 17 00:00:00 2001 From: Joel Mathew Thomas <90510078+joelmathewthomas@users.noreply.github.com> Date: Wed, 26 Feb 2025 21:23:32 +0530 Subject: [PATCH] bugfix: api/cleanup_zip - Fix error when there are no zip files to delete --- api/api/views.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/api/api/views.py b/api/api/views.py index 215eb6e..4f3cc4f 100644 --- a/api/api/views.py +++ b/api/api/views.py @@ -201,12 +201,18 @@ def cleanup(request): @api_view(['POST']) def cleanup_zip(request): """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): if file.endswith(".zip"): file_path = os.path.join(UPLOAD_DIR, file) try: 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: - return Response({"message": f"Error deleting {file_path}: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) \ No newline at end of file + 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)