remove redundant code, improve get_audio_file_path()

- Check for file_uuid in request within function
- Check for existence of file within function
- Return appropriate status codes
This commit is contained in:
Joel Mathew Thomas
2025-02-26 02:39:27 +05:30
parent f71d30732f
commit 8356394b8a
2 changed files with 20 additions and 23 deletions
+10 -3
View File
@@ -1,9 +1,16 @@
# api/utils.py
import os
from rest_framework import status
def get_audio_file_path(file_uuid, base_dir):
def get_audio_file_path(request, base_dir):
"""Returns the full path to the audio file inside the given UUID folder."""
file_uuid = request.data.get("file_uuid")
if not file_uuid:
return False, "Missing file_uuid", status.HTTP_400_BAD_REQUEST
dir_path = os.path.join(base_dir, file_uuid)
if not os.path.exists(dir_path) or not os.listdir(dir_path):
return False
return os.path.join(dir_path, os.listdir(dir_path)[0]) # Assumes only one file exists
return False, "No file found", status.HTTP_500_INTERNAL_SERVER_ERROR
return True, os.path.join(dir_path, os.listdir(dir_path)[0]), status.HTTP_200_OK # Assumes only one file exists
+10 -20
View File
@@ -27,7 +27,7 @@ def upload_audio(request):
# Check file format before proceeding
if not is_supported_format(audio_file.name):
return Response({"error": "Unsupported file format"}, status=status.HTTP_400_BAD_REQUEST)
return Response({"error": "Unsupported file format"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# Generate a unique ID for this upload
file_uuid = str(uuid.uuid4())[:8]
@@ -56,17 +56,12 @@ def upload_audio(request):
@api_view(['POST'])
def normalize_audio(request):
"""Handles audio normalization request"""
file_uuid = request.data.get("file_uuid")
if not file_uuid:
return Response({"error": "Missing file_uuid"}, status=status.HTTP_400_BAD_REQUEST)
file_path = get_audio_file_path(file_uuid, UPLOAD_DIR)
if file_path == False:
return Response({"error": "No files found"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
stat, result, status_code = get_audio_file_path(request, UPLOAD_DIR)
if stat == False:
return Response({"error": result}, status=status_code)
# Call Celery task synchronously
task = normalize_audio_task.apply(args=(file_path,))
task = normalize_audio_task.apply(args=(result,))
if task.get():
return Response({"message": "Audio normalized successfully"}, status=status.HTTP_200_OK)
@@ -77,19 +72,14 @@ def normalize_audio(request):
@api_view(['POST'])
def trim_audio(request):
"""Handles trimming of leading and trailing silence from an audio clip"""
file_uuid = request.data.get("file_uuid")
if not file_uuid:
return Response({"error": "Missing file_uuid"}, status=status.HTTP_400_BAD_REQUEST)
file_path = get_audio_file_path(file_uuid, UPLOAD_DIR)
if file_path == False:
return Response({"error": "No files found"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
stat, result, status_code = get_audio_file_path(request, UPLOAD_DIR)
if stat == False:
return Response({"error": result}, status=status_code)
# Call Celery task synchronously
task = trim_audio_task.apply(args=(file_path,))
task = trim_audio_task.apply(args=(result,))
if task.get():
return Response({"message": "Audio trimmed successfulyl"}, status=status.HTTP_200_OK)
return Response({"message": "Audio trimmed successfully"}, status=status.HTTP_200_OK)
else:
return Response({"error": "Failed to normalize audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)