endpoint: /api/cleanup_zip/

- Add endpoint /api/cleanup_zip/ to cleanup leftover zip files leftover by /api/download/
This commit is contained in:
Joel Mathew Thomas
2025-02-26 20:16:54 +05:30
parent ee5999da91
commit e1dac18802
2 changed files with 16 additions and 2 deletions
+14 -1
View File
@@ -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)
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)
+2 -1
View File
@@ -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")
]