#1714: Change file and folder uuid checking to check for file or folder names already existing
This commit is contained in:
@@ -69,6 +69,10 @@ class FileSystem(SimComponent):
|
||||
folder = self.get_folder_by_id(folder_uuid)
|
||||
|
||||
if folder is not None:
|
||||
# check if file with name already exists
|
||||
if folder.get_file_by_name(file_name):
|
||||
raise Exception(f'File with name "{file_name}" already exists.')
|
||||
|
||||
file = FileSystemFile(name=file_name, size=size, file_type=file_type)
|
||||
folder.add_file(file=file)
|
||||
else:
|
||||
@@ -94,14 +98,13 @@ class FileSystem(SimComponent):
|
||||
:param: folder_name: The name of the folder
|
||||
:type: folder_name: str
|
||||
"""
|
||||
# check if folder with name already exists
|
||||
if self.get_folder_by_name(folder_name):
|
||||
raise Exception(f'Folder with name "{folder_name}" already exists.')
|
||||
|
||||
folder = FileSystemFolder(name=folder_name)
|
||||
|
||||
if folder.uuid in self.folders:
|
||||
# iterate until a folder with a non-matching uuid is added
|
||||
# which is VERY unlikely but it'll be weird if it happens twice
|
||||
return self.create_folder(folder_name=folder_name)
|
||||
else:
|
||||
self.folders[folder.uuid] = folder
|
||||
self.folders[folder.uuid] = folder
|
||||
return folder
|
||||
|
||||
def delete_file(self, file: Optional[FileSystemFile] = None):
|
||||
@@ -155,6 +158,10 @@ class FileSystem(SimComponent):
|
||||
if file is None:
|
||||
raise Exception("File to be moved is None")
|
||||
|
||||
# check if file with name already exists
|
||||
if target_folder.get_file_by_name(file.name):
|
||||
raise Exception(f'Folder with name "{file.name}" already exists.')
|
||||
|
||||
# remove file from src
|
||||
src_folder.remove_file(file)
|
||||
|
||||
@@ -185,6 +192,10 @@ class FileSystem(SimComponent):
|
||||
if file is None:
|
||||
raise Exception("File to be moved is None")
|
||||
|
||||
# check if file with name already exists
|
||||
if target_folder.get_file_by_name(file.name):
|
||||
raise Exception(f'Folder with name "{file.name}" already exists.')
|
||||
|
||||
# add file to target
|
||||
target_folder.add_file(file)
|
||||
|
||||
|
||||
@@ -20,6 +20,10 @@ class FileSystemFolder(FileSystemItem):
|
||||
"""Return a FileSystemFile with the matching id."""
|
||||
return self.files.get(file_id)
|
||||
|
||||
def get_file_by_name(self, file_name: str) -> FileSystemFile:
|
||||
"""Return a FileSystemFile with the matching id."""
|
||||
return next((f for f in list(self.files) if f.name == file_name), None)
|
||||
|
||||
def add_file(self, file: FileSystemFile):
|
||||
"""Adds a file to the folder list."""
|
||||
if file is None or not isinstance(file, FileSystemFile):
|
||||
|
||||
Reference in New Issue
Block a user