From e1dac1880224afc137a573b68eb7a10b5f030a56 Mon Sep 17 00:00:00 2001 From: Joel Mathew Thomas <90510078+joelmathewthomas@users.noreply.github.com> Date: Wed, 26 Feb 2025 20:16:54 +0530 Subject: [PATCH] endpoint: /api/cleanup_zip/ - Add endpoint /api/cleanup_zip/ to cleanup leftover zip files leftover by /api/download/ --- api/api/views.py | 15 ++++++++++++++- api/backend/urls.py | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/api/api/views.py b/api/api/views.py index 507cc6f..215eb6e 100644 --- a/api/api/views.py +++ b/api/api/views.py @@ -196,4 +196,17 @@ def cleanup(request): if task.get(): return Response({"message": f"Successfully cleaned up files on the server"}, status=status.HTTP_200_OK) else: - return Response({"error": "Failed to cleanup files on the server"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) \ No newline at end of file + return Response({"error": "Failed to cleanup files on the server"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + +@api_view(['POST']) +def cleanup_zip(request): + """Handles cleanup of all zip files leftover by api/download""" + # Delete all ZIP files in UPLOAD_DIR + 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) + 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 diff --git a/api/backend/urls.py b/api/backend/urls.py index 2aec782..727daa7 100644 --- a/api/backend/urls.py +++ b/api/backend/urls.py @@ -24,6 +24,7 @@ from api.views import separate_music from api.views import noisereduce from api.views import download_audio from api.views import cleanup +from api.views import cleanup_zip urlpatterns = [ path('admin/', admin.site.urls), @@ -35,5 +36,5 @@ urlpatterns = [ path('api/noisereduce', noisereduce, name="noisreduce"), path('api/download', download_audio, name="download_audio"), path('api/cleanup', cleanup, name="cleanup"), - + path('api/cleanup_zip', cleanup_zip, name="cleanup_zip") ]