|
6 | 6 | from .config import Settings |
7 | 7 | from python_osw_validation import OSWValidation |
8 | 8 | from .models.queue_message_content import ValidationResult |
| 9 | +import uuid |
9 | 10 |
|
10 | 11 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
11 | 12 | # Path used for download file generation. |
@@ -49,31 +50,44 @@ def is_osw_valid(self) -> ValidationResult: |
49 | 50 |
|
50 | 51 | return result |
51 | 52 |
|
| 53 | + # Downloads the single file into a unique directory |
52 | 54 | def download_single_file(self, file_upload_path=None) -> str: |
53 | 55 | is_exists = os.path.exists(DOWNLOAD_FILE_PATH) |
| 56 | + unique_id = self.get_unique_id() |
54 | 57 | if not is_exists: |
55 | 58 | os.makedirs(DOWNLOAD_FILE_PATH) |
56 | | - |
| 59 | + unique_directory = os.path.join(DOWNLOAD_FILE_PATH,unique_id) |
| 60 | + if not os.path.exists(unique_directory): |
| 61 | + os.makedirs(unique_directory) |
| 62 | + |
57 | 63 | file = self.storage_client.get_file_from_url(self.container_name, file_upload_path) |
58 | 64 | try: |
59 | 65 | if file.file_path: |
60 | 66 | file_path = os.path.basename(file.file_path) |
61 | | - with open(f'{DOWNLOAD_FILE_PATH}/{file_path}', 'wb') as blob: |
| 67 | + local_download_path = os.path.join(unique_directory,file_path) |
| 68 | + with open(local_download_path, 'wb') as blob: |
62 | 69 | blob.write(file.get_stream()) |
63 | | - logger.info(f' File downloaded to location: {DOWNLOAD_FILE_PATH}/{file_path}') |
64 | | - return f'{DOWNLOAD_FILE_PATH}/{file_path}' |
| 70 | + logger.info(f' File downloaded to location: {local_download_path}') |
| 71 | + return local_download_path |
65 | 72 | else: |
66 | 73 | logger.info(' File not found!') |
67 | 74 | except Exception as e: |
68 | 75 | traceback.print_exc() |
69 | 76 | logger.error(e) |
70 | 77 |
|
| 78 | + # Generates a unique string for directory |
| 79 | + def get_unique_id(self) -> str: |
| 80 | + unique_id = uuid.uuid1().hex[0:24] |
| 81 | + return unique_id |
| 82 | + |
| 83 | + |
| 84 | + |
71 | 85 | @staticmethod |
72 | 86 | def clean_up(path): |
73 | 87 | if os.path.isfile(path): |
74 | 88 | logger.info(f' Removing File: {path}') |
75 | 89 | os.remove(path) |
76 | 90 | else: |
77 | | - folder = os.path.join(DOWNLOAD_FILE_PATH, path) |
78 | | - logger.info(f' Removing Folder: {folder}') |
79 | | - shutil.rmtree(folder, ignore_errors=False) |
| 91 | + # folder = os.path.join(DOWNLOAD_FILE_PATH, path) |
| 92 | + logger.info(f' Removing Folder: {path}') |
| 93 | + shutil.rmtree(path, ignore_errors=False) |
0 commit comments