endpoint: /api/cleanup

- Add new endpoint /api/cleanup, params: file_uuid
- Cleanup leftover files on the server
This commit is contained in:
Joel Mathew Thomas
2025-02-26 20:05:49 +05:30
parent 1a8e4c5f40
commit ee5999da91
3 changed files with 33 additions and 2 deletions
+12
View File
@@ -120,3 +120,15 @@ def noisereduce_task(file_path):
return True
except Exception as e:
return False
@shared_task
def cleanup_task(file_path):
"""Celery task to cleanup files"""
file_path = Path(file_path)
# Cleanup
try:
shutil.rmtree(os.path.dirname(file_path))
return True
except Exception as e:
return False
+16
View File
@@ -12,6 +12,7 @@ from .tasks import trim_audio_task
from .tasks import resample_audio_task
from .tasks import music_separation_task
from .tasks import noisereduce_task
from .tasks import cleanup_task
from freqsplit.input.format_checker import is_supported_format
UPLOAD_DIR = "/tmp/freqsplit"
@@ -181,3 +182,18 @@ def download_audio(request):
# Stream the ZIP file
return FileResponse(open(zip_file_path, "rb"), as_attachment=True, filename=os.path.basename(zip_file_path))
@api_view(['POST'])
def cleanup(request):
"""Handles file cleanup after pipeline processing"""
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 = cleanup_task.apply(args=(result,))
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)
+4 -1
View File
@@ -23,6 +23,7 @@ from api.views import resample_audio
from api.views import separate_music
from api.views import noisereduce
from api.views import download_audio
from api.views import cleanup
urlpatterns = [
path('admin/', admin.site.urls),
@@ -32,5 +33,7 @@ urlpatterns = [
path('api/resample', resample_audio, name='resample_audio'),
path('api/separate', separate_music, name="separate_music"),
path('api/noisereduce', noisereduce, name="noisreduce"),
path('api/download', download_audio, name="download_audio")
path('api/download', download_audio, name="download_audio"),
path('api/cleanup', cleanup, name="cleanup"),
]