@@ -62,6 +62,48 @@ def __init__(self, msg, data: bytes, name: str, mimetype: str, node: email.messa
6262 if self .__data is None :
6363 self .__data = data
6464
65+ def _handleFnc (self , _zip , filename , customPath : pathlib .Path , kwargs ) -> pathlib .Path :
66+ """
67+ "Handle Filename Conflict"
68+
69+ Internal function for use in determining how to modify the saving path
70+ when a file with the same name already exists. This is mainly because
71+ any save function that uses files will need to do this functionality.
72+
73+ :returns: A ``pathlib.Path`` object to where the file should be saved.
74+ """
75+ fullFilename = customPath / filename
76+
77+ overwriteExisting = kwargs .get ('overwriteExisting' , False )
78+
79+ if _zip :
80+ # If we are writing to a zip file and are not overwriting.
81+ if not overwriteExisting :
82+ name , ext = os .path .splitext (filename )
83+ nameList = _zip .namelist ()
84+ if str (fullFilename ).replace ('\\ ' , '/' ) in nameList :
85+ for i in range (2 , 100 ):
86+ testName = customPath / f'{ name } ({ i } ){ ext } '
87+ if str (testName ).replace ('\\ ' , '/' ) not in nameList :
88+ return testName
89+ else :
90+ # If we couldn't find one that didn't exist.
91+ raise FileExistsError (f'Could not create the specified file because it already exists ("{ fullFilename } ").' )
92+ else :
93+ if not overwriteExisting and fullFilename .exists ():
94+ # Try to split the filename into a name and extension.
95+ name , ext = os .path .splitext (filename )
96+ # Try to add a number to it so that we can save without overwriting.
97+ for i in range (2 , 100 ):
98+ testName = customPath / f'{ name } ({ i } ){ ext } '
99+ if not testName .exists ():
100+ return testName
101+ else :
102+ # If we couldn't find one that didn't exist.
103+ raise FileExistsError (f'Could not create the specified file because it already exists ("{ fullFilename } ").' )
104+
105+ return fullFilename
106+
65107 def save (self , ** kwargs ) -> constants .SAVE_TYPE :
66108 """
67109 Saves the attachment data.
0 commit comments