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:
+10
-3
@@ -1,9 +1,16 @@
|
|||||||
# api/utils.py
|
# api/utils.py
|
||||||
import os
|
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."""
|
"""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)
|
dir_path = os.path.join(base_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
|
return False, "No file found", status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||||
return os.path.join(dir_path, os.listdir(dir_path)[0]) # Assumes only one file exists
|
return True, os.path.join(dir_path, os.listdir(dir_path)[0]), status.HTTP_200_OK # Assumes only one file exists
|
||||||
+10
-20
@@ -27,7 +27,7 @@ def upload_audio(request):
|
|||||||
|
|
||||||
# Check file format before proceeding
|
# Check file format before proceeding
|
||||||
if not is_supported_format(audio_file.name):
|
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
|
# Generate a unique ID for this upload
|
||||||
file_uuid = str(uuid.uuid4())[:8]
|
file_uuid = str(uuid.uuid4())[:8]
|
||||||
@@ -56,17 +56,12 @@ def upload_audio(request):
|
|||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def normalize_audio(request):
|
def normalize_audio(request):
|
||||||
"""Handles audio normalization request"""
|
"""Handles audio normalization request"""
|
||||||
file_uuid = request.data.get("file_uuid")
|
stat, result, status_code = get_audio_file_path(request, UPLOAD_DIR)
|
||||||
|
if stat == False:
|
||||||
if not file_uuid:
|
return Response({"error": result}, status=status_code)
|
||||||
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)
|
|
||||||
|
|
||||||
# Call Celery task synchronously
|
# Call Celery task synchronously
|
||||||
task = normalize_audio_task.apply(args=(file_path,))
|
task = normalize_audio_task.apply(args=(result,))
|
||||||
|
|
||||||
if task.get():
|
if task.get():
|
||||||
return Response({"message": "Audio normalized successfully"}, status=status.HTTP_200_OK)
|
return Response({"message": "Audio normalized successfully"}, status=status.HTTP_200_OK)
|
||||||
@@ -77,19 +72,14 @@ def normalize_audio(request):
|
|||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def trim_audio(request):
|
def trim_audio(request):
|
||||||
"""Handles trimming of leading and trailing silence from an audio clip"""
|
"""Handles trimming of leading and trailing silence from an audio clip"""
|
||||||
file_uuid = request.data.get("file_uuid")
|
stat, result, status_code = get_audio_file_path(request, UPLOAD_DIR)
|
||||||
|
if stat == False:
|
||||||
if not file_uuid:
|
return Response({"error": result}, status=status_code)
|
||||||
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)
|
|
||||||
|
|
||||||
# Call Celery task synchronously
|
# Call Celery task synchronously
|
||||||
task = trim_audio_task.apply(args=(file_path,))
|
task = trim_audio_task.apply(args=(result,))
|
||||||
|
|
||||||
if task.get():
|
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:
|
else:
|
||||||
return Response({"error": "Failed to normalize audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({"error": "Failed to normalize audio"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
Reference in New Issue
Block a user