From 6dfc49fc8e0a0b59a4bb1b75fb8089b58019402b Mon Sep 17 00:00:00 2001 From: Naresh Kumar D Date: Wed, 10 Apr 2024 13:23:54 +0530 Subject: [PATCH 1/2] Update validation.py - Fixes the threading issue that happens when two files with same name are validated at the same time --- src/validation.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/validation.py b/src/validation.py index 0b716a3..67e9e26 100644 --- a/src/validation.py +++ b/src/validation.py @@ -6,6 +6,7 @@ from .config import Settings from python_osw_validation import OSWValidation from .models.queue_message_content import ValidationResult +import uuid ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) # Path used for download file generation. @@ -49,25 +50,38 @@ def is_osw_valid(self) -> ValidationResult: return result + # Downloads the single file into a unique directory def download_single_file(self, file_upload_path=None) -> str: is_exists = os.path.exists(DOWNLOAD_FILE_PATH) + unique_id = self.get_unique_id() if not is_exists: os.makedirs(DOWNLOAD_FILE_PATH) - + unique_directory = os.path.join(DOWNLOAD_FILE_PATH,unique_id) + if not os.path.exists(unique_directory): + os.makedirs(unique_directory) + file = self.storage_client.get_file_from_url(self.container_name, file_upload_path) try: if file.file_path: file_path = os.path.basename(file.file_path) - with open(f'{DOWNLOAD_FILE_PATH}/{file_path}', 'wb') as blob: + local_download_path = os.path.join(unique_directory,file_path) + with open(local_download_path, 'wb') as blob: blob.write(file.get_stream()) - logger.info(f' File downloaded to location: {DOWNLOAD_FILE_PATH}/{file_path}') - return f'{DOWNLOAD_FILE_PATH}/{file_path}' + logger.info(f' File downloaded to location: {local_download_path}') + return local_download_path else: logger.info(' File not found!') except Exception as e: traceback.print_exc() logger.error(e) + # Generates a unique string for directory + def get_unique_id(self) -> str: + unique_id = uuid.uuid1().hex[0:24] + return unique_id + + + @staticmethod def clean_up(path): if os.path.isfile(path): From 39b478bf9e6d7f33915272848e8cbaeb7ca5552f Mon Sep 17 00:00:00 2001 From: Naresh Kumar D Date: Wed, 10 Apr 2024 14:26:01 +0530 Subject: [PATCH 2/2] Update validation.py --- src/validation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/validation.py b/src/validation.py index 67e9e26..9ef6a5a 100644 --- a/src/validation.py +++ b/src/validation.py @@ -88,6 +88,6 @@ def clean_up(path): logger.info(f' Removing File: {path}') os.remove(path) else: - folder = os.path.join(DOWNLOAD_FILE_PATH, path) - logger.info(f' Removing Folder: {folder}') - shutil.rmtree(folder, ignore_errors=False) + # folder = os.path.join(DOWNLOAD_FILE_PATH, path) + logger.info(f' Removing Folder: {path}') + shutil.rmtree(path, ignore_errors=False)