From 6316de39334992a3115da62d72fe475a716a5aeb Mon Sep 17 00:00:00 2001 From: Joel Mathew Thomas <90510078+joelmathewthomas@users.noreply.github.com> Date: Wed, 26 Feb 2025 19:53:02 +0530 Subject: [PATCH] endpoint: /api/download - Add endpoint /api/download to download the files - If single file, download single file - Else download zip file --- api/api/views.py | 40 +++++++++++++++++++++++++++++++++++++++- api/backend/urls.py | 4 +++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/api/api/views.py b/api/api/views.py index 482d59f..06d96a3 100644 --- a/api/api/views.py +++ b/api/api/views.py @@ -1,5 +1,7 @@ import os import uuid +import zipfile +from django.http import FileResponse, HttpResponse from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status @@ -138,4 +140,40 @@ def noisereduce(request): if task.get(): return Response({"message": f"Removed noise from audio successfully"}, status=status.HTTP_200_OK) else: - return Response({"error": "Failed to remove noise from audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) \ No newline at end of file + return Response({"error": "Failed to remove noise from audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + +# Endpoint to download audio file or zipped directory +@api_view(['GET']) +def download_audio(request): + """Handles downloading audio file or a zipped directory""" + file_uuid = request.query_params.get("file_uuid") + + if not file_uuid: + return Response({"error": "file_uuid is required"}, status=status.HTTP_400_BAD_REQUEST) + + dir_path = os.path.join(UPLOAD_DIR, file_uuid) + + if not os.path.exists(dir_path) or not os.listdir(dir_path): + return False, "No file found", status.HTTP_500_INTERNAL_SERVER_ERROR + file_path = os.path.join(dir_path, os.listdir(dir_path)[0]) + + base_dir = os.path.dirname(file_path) + + files_in_base_dir = os.listdir(base_dir) + audio_files = [f for f in files_in_base_dir if f.endswith(('.wav', '.mp3', '.flac',))] + sources_folder = "sources" in files_in_base_dir + + if len(audio_files) == 1 and not sources_folder: + return FileResponse(open(file_path, "rb"), as_attachment=True, filename=os.path.basename(file_path)) + elif len(audio_files) >= 1 and sources_folder: + zip_file_path = f"{base_dir}.zip" + with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf: + for root, _, files in os.walk(base_dir): + for file in files: + file_path = os.path.join(root, file) + arcname = os.path.relpath(file_path, base_dir) + zipf.write(file_path, arcname) + + return FileResponse(open(zip_file_path, "rb"), as_attachment=True, filename=os.path.basename(zip_file_path)) + return Response({"error": "Unexpected file structure"}, 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 a37ad56..63be3f9 100644 --- a/api/backend/urls.py +++ b/api/backend/urls.py @@ -22,6 +22,7 @@ from api.views import trim_audio from api.views import resample_audio from api.views import separate_music from api.views import noisereduce +from api.views import download_audio urlpatterns = [ path('admin/', admin.site.urls), @@ -30,5 +31,6 @@ urlpatterns = [ path('api/trim', trim_audio, name='trim_audio'), path('api/resample', resample_audio, name='resample_audio'), path('api/separate', separate_music, name="separate_music"), - path('api/noisereduce', noisereduce, name="noisreduce") + path('api/noisereduce', noisereduce, name="noisreduce"), + path('api/download', download_audio, name="download_audio") ]