endpoint: /api/cleanup
- Add new endpoint /api/cleanup, params: file_uuid - Cleanup leftover files on the server
This commit is contained in:
@@ -120,3 +120,15 @@ def noisereduce_task(file_path):
|
|||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False
|
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
|
||||||
@@ -12,6 +12,7 @@ from .tasks import trim_audio_task
|
|||||||
from .tasks import resample_audio_task
|
from .tasks import resample_audio_task
|
||||||
from .tasks import music_separation_task
|
from .tasks import music_separation_task
|
||||||
from .tasks import noisereduce_task
|
from .tasks import noisereduce_task
|
||||||
|
from .tasks import cleanup_task
|
||||||
from freqsplit.input.format_checker import is_supported_format
|
from freqsplit.input.format_checker import is_supported_format
|
||||||
|
|
||||||
UPLOAD_DIR = "/tmp/freqsplit"
|
UPLOAD_DIR = "/tmp/freqsplit"
|
||||||
@@ -181,3 +182,18 @@ def download_audio(request):
|
|||||||
|
|
||||||
# Stream the ZIP file
|
# Stream the ZIP file
|
||||||
return FileResponse(open(zip_file_path, "rb"), as_attachment=True, filename=os.path.basename(zip_file_path))
|
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
@@ -23,6 +23,7 @@ from api.views import resample_audio
|
|||||||
from api.views import separate_music
|
from api.views import separate_music
|
||||||
from api.views import noisereduce
|
from api.views import noisereduce
|
||||||
from api.views import download_audio
|
from api.views import download_audio
|
||||||
|
from api.views import cleanup
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
@@ -32,5 +33,7 @@ urlpatterns = [
|
|||||||
path('api/resample', resample_audio, name='resample_audio'),
|
path('api/resample', resample_audio, name='resample_audio'),
|
||||||
path('api/separate', separate_music, name="separate_music"),
|
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")
|
path('api/download', download_audio, name="download_audio"),
|
||||||
|
path('api/cleanup', cleanup, name="cleanup"),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user