Skip to content

Commit 462163b

Browse files
authored
Merge pull request #17 from TaskarCenterAtUW/feature-unique-download
Update validation.py
2 parents 57595f3 + 39b478b commit 462163b

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/validation.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .config import Settings
77
from python_osw_validation import OSWValidation
88
from .models.queue_message_content import ValidationResult
9+
import uuid
910

1011
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
1112
# Path used for download file generation.
@@ -49,31 +50,44 @@ def is_osw_valid(self) -> ValidationResult:
4950

5051
return result
5152

53+
# Downloads the single file into a unique directory
5254
def download_single_file(self, file_upload_path=None) -> str:
5355
is_exists = os.path.exists(DOWNLOAD_FILE_PATH)
56+
unique_id = self.get_unique_id()
5457
if not is_exists:
5558
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+
5763
file = self.storage_client.get_file_from_url(self.container_name, file_upload_path)
5864
try:
5965
if file.file_path:
6066
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:
6269
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
6572
else:
6673
logger.info(' File not found!')
6774
except Exception as e:
6875
traceback.print_exc()
6976
logger.error(e)
7077

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+
7185
@staticmethod
7286
def clean_up(path):
7387
if os.path.isfile(path):
7488
logger.info(f' Removing File: {path}')
7589
os.remove(path)
7690
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

Comments
 (0)