From c6e36d2e15675c9e8c5029c0d1aa5a0107af310e Mon Sep 17 00:00:00 2001 From: Ashvin KS Date: Mon, 16 Mar 2026 21:18:41 +0530 Subject: [PATCH 1/3] fix(backend): always cleanup uploaded temp files --- backend/Generator/main.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/backend/Generator/main.py b/backend/Generator/main.py index 04aed79f..a0cf945e 100644 --- a/backend/Generator/main.py +++ b/backend/Generator/main.py @@ -372,16 +372,18 @@ def process_file(self, file): file.save(file_path) content = "" - if file.filename.endswith('.txt'): - with open(file_path, 'r') as f: - content = f.read() - elif file.filename.endswith('.pdf'): - content = self.extract_text_from_pdf(file_path) - elif file.filename.endswith('.docx'): - content = self.extract_text_from_docx(file_path) - - os.remove(file_path) - return content + try: + if file.filename.endswith('.txt'): + with open(file_path, 'r') as f: + content = f.read() + elif file.filename.endswith('.pdf'): + content = self.extract_text_from_pdf(file_path) + elif file.filename.endswith('.docx'): + content = self.extract_text_from_docx(file_path) + return content + finally: + if os.path.exists(file_path): + os.remove(file_path) class QuestionGenerator: """A transformer-based NLP system for generating reading comprehension-style questions from From 69da1d7c4cb9766211cc07ea50749025228510fd Mon Sep 17 00:00:00 2001 From: Ashvin KS Date: Wed, 18 Mar 2026 18:27:32 +0530 Subject: [PATCH 2/3] fix(backend): harden upload temp-file handling --- backend/Generator/main.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/Generator/main.py b/backend/Generator/main.py index a0cf945e..49b115ca 100644 --- a/backend/Generator/main.py +++ b/backend/Generator/main.py @@ -20,6 +20,7 @@ from typing import Any, List, Mapping, Tuple import re import os +import uuid import fitz import mammoth @@ -368,22 +369,28 @@ def extract_text_from_docx(self, file_path): return result.value def process_file(self, file): - file_path = os.path.join(self.upload_folder, file.filename) - file.save(file_path) + original_filename = os.path.basename(file.filename or "") + _, ext = os.path.splitext(original_filename) + lower_ext = ext.lower() + safe_filename = f"{uuid.uuid4().hex}{lower_ext}" + file_path = os.path.join(self.upload_folder, safe_filename) content = "" try: - if file.filename.endswith('.txt'): + file.save(file_path) + if lower_ext == '.txt': with open(file_path, 'r') as f: content = f.read() - elif file.filename.endswith('.pdf'): + elif lower_ext == '.pdf': content = self.extract_text_from_pdf(file_path) - elif file.filename.endswith('.docx'): + elif lower_ext == '.docx': content = self.extract_text_from_docx(file_path) return content finally: - if os.path.exists(file_path): + try: os.remove(file_path) + except FileNotFoundError: + pass class QuestionGenerator: """A transformer-based NLP system for generating reading comprehension-style questions from From 7490a65c4de5791cba743c5e27d7044ab3a935fc Mon Sep 17 00:00:00 2001 From: Ashvin KS Date: Wed, 18 Mar 2026 18:34:08 +0530 Subject: [PATCH 3/3] fix(backend): remove user-derived extension from temp path --- backend/Generator/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/Generator/main.py b/backend/Generator/main.py index 49b115ca..a5564675 100644 --- a/backend/Generator/main.py +++ b/backend/Generator/main.py @@ -372,7 +372,7 @@ def process_file(self, file): original_filename = os.path.basename(file.filename or "") _, ext = os.path.splitext(original_filename) lower_ext = ext.lower() - safe_filename = f"{uuid.uuid4().hex}{lower_ext}" + safe_filename = uuid.uuid4().hex file_path = os.path.join(self.upload_folder, safe_filename) content = ""