code refinement for endpoint /api/download/

This commit is contained in:
Joel Mathew Thomas
2025-02-26 19:55:29 +05:30
parent 6316de3933
commit 1a8e4c5f40
+25 -21
View File
@@ -145,7 +145,7 @@ def noisereduce(request):
# Endpoint to download audio file or zipped directory # Endpoint to download audio file or zipped directory
@api_view(['GET']) @api_view(['GET'])
def download_audio(request): def download_audio(request):
"""Handles downloading audio file or a zipped directory""" """Handles downloading an audio file or a zipped directory."""
file_uuid = request.query_params.get("file_uuid") file_uuid = request.query_params.get("file_uuid")
if not file_uuid: if not file_uuid:
@@ -154,26 +154,30 @@ def download_audio(request):
dir_path = os.path.join(UPLOAD_DIR, file_uuid) dir_path = os.path.join(UPLOAD_DIR, file_uuid)
if not os.path.exists(dir_path) or not os.listdir(dir_path): if not os.path.exists(dir_path) or not os.listdir(dir_path):
return False, "No file found", status.HTTP_500_INTERNAL_SERVER_ERROR return Response({"error": "No file found"}, status=status.HTTP_404_NOT_FOUND)
file_path = os.path.join(dir_path, os.listdir(dir_path)[0])
base_dir = os.path.dirname(file_path) audio_files = [f for f in os.listdir(dir_path) if f.endswith(('.wav', '.mp3', '.flac'))]
sources_folder = "sources" in os.listdir(dir_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',))] # If only one audio file exists (no sources/)
sources_folder = "sources" in files_in_base_dir
if len(audio_files) == 1 and not sources_folder: if len(audio_files) == 1 and not sources_folder:
file_path = os.path.join(dir_path, audio_files[0])
return FileResponse(open(file_path, "rb"), as_attachment=True, filename=os.path.basename(file_path)) 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" # If there are multiple audio files or a sources/ directory, create a ZIP
with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf: zip_file_path = os.path.join(UPLOAD_DIR, f"{file_uuid}.zip")
for root, _, files in os.walk(base_dir):
for file in files: # Ensure ZIP file is always fresh
file_path = os.path.join(root, file) if os.path.exists(zip_file_path):
arcname = os.path.relpath(file_path, base_dir) os.remove(zip_file_path)
zipf.write(file_path, arcname)
# Create ZIP file
return FileResponse(open(zip_file_path, "rb"), as_attachment=True, filename=os.path.basename(zip_file_path)) with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf:
return Response({"error": "Unexpected file structure"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) for root, _, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, dir_path) # Preserve folder structure inside ZIP
zipf.write(file_path, arcname)
# Stream the ZIP file
return FileResponse(open(zip_file_path, "rb"), as_attachment=True, filename=os.path.basename(zip_file_path))