From a5705fffd0fe9975fd32339af9c4df496cdc7a3b Mon Sep 17 00:00:00 2001 From: Joel Mathew Thomas <90510078+joelmathewthomas@users.noreply.github.com> Date: Thu, 20 Mar 2025 00:12:10 +0530 Subject: [PATCH] remove file_dir on websocket disconnect --- api/api/consumers.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/api/api/consumers.py b/api/api/consumers.py index 6d5f6ca..2b517c7 100644 --- a/api/api/consumers.py +++ b/api/api/consumers.py @@ -1,7 +1,12 @@ # api/consumers.py import json +import os +import shutil +from pathlib import Path from channels.generic.websocket import WebsocketConsumer +UPLOAD_DIR = Path("/tmp/freqsplit") + class MediaConsumer(WebsocketConsumer): def connect(self): self.accept() @@ -26,4 +31,11 @@ class MediaConsumer(WebsocketConsumer): def disconnect(self, close_code): print("Disconnected from Websocket") - print("Stored file UUIDs:", self.file_uuid) \ No newline at end of file + print("Stored file UUIDs:", self.file_uuid) + for file_uuid in self.file_uuid: + dir_path = os.path.join(UPLOAD_DIR, file_uuid); + try: + if os.path.exists(dir_path): + shutil.rmtree(dir_path) + except Exception as e: + print(f"Error: Failed to cleanup {dir_path}: {e}") \ No newline at end of file