From 9e10c2cd8f31409fcc7b68ef554c399abcba82e3 Mon Sep 17 00:00:00 2001 From: TheElementalOfDestruction Date: Wed, 20 Mar 2024 11:45:39 -0700 Subject: [PATCH] Fix #409 --- CHANGELOG.md | 4 +++ README.rst | 4 +-- extract_msg/__init__.py | 4 +-- extract_msg/attachments/attachment_base.py | 2 +- extract_msg/attachments/signed_att.py | 42 ++++++++++++++++++++++ 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 781754a1..c748a619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +**v0.48.3** +* [[TeamMsgExtractor #409](https://github.com/TeamMsgExtractor/msg-extractor/issues/409)] Added missing private method to `SignedAttachment`. +* Fixed some missing typing information. + **v0.48.2** * Fixed bugs with `MessageBase.asEmailMessage()`. Numerous improvements to how it handles the data. diff --git a/README.rst b/README.rst index 2d62238b..bf1c5863 100644 --- a/README.rst +++ b/README.rst @@ -260,8 +260,8 @@ your access to the newest major version of extract-msg. .. |License: GPL v3| image:: https://img.shields.io/badge/License-GPLv3-blue.svg :target: LICENSE.txt -.. |PyPI3| image:: https://img.shields.io/badge/pypi-0.48.2-blue.svg - :target: https://pypi.org/project/extract-msg/0.48.2/ +.. |PyPI3| image:: https://img.shields.io/badge/pypi-0.48.3-blue.svg + :target: https://pypi.org/project/extract-msg/0.48.3/ .. |PyPI2| image:: https://img.shields.io/badge/python-3.8+-brightgreen.svg :target: https://www.python.org/downloads/release/python-3810/ diff --git a/extract_msg/__init__.py b/extract_msg/__init__.py index d8e2ca63..95974ac8 100644 --- a/extract_msg/__init__.py +++ b/extract_msg/__init__.py @@ -27,8 +27,8 @@ # along with this program. If not, see . __author__ = 'Destiny Peterson & Matthew Walker' -__date__ = '2024-03-09' -__version__ = '0.48.2' +__date__ = '2024-03-20' +__version__ = '0.48.3' __all__ = [ # Modules: diff --git a/extract_msg/attachments/attachment_base.py b/extract_msg/attachments/attachment_base.py index f9276757..18909719 100644 --- a/extract_msg/attachments/attachment_base.py +++ b/extract_msg/attachments/attachment_base.py @@ -152,7 +152,7 @@ def _getTypedStream(self, filename: MSG_PATH, _type = None): raise ReferenceError('The MSGFile for this Attachment instance has been garbage collected.') return msg._getTypedStream([self.__dir, msgPathToString(filename)], True, _type) - def _handleFnc(self, _zip, filename, customPath, kwargs) -> pathlib.Path: + def _handleFnc(self, _zip, filename, customPath: pathlib.Path, kwargs) -> pathlib.Path: """ "Handle Filename Conflict" diff --git a/extract_msg/attachments/signed_att.py b/extract_msg/attachments/signed_att.py index bec68978..f7c530c6 100644 --- a/extract_msg/attachments/signed_att.py +++ b/extract_msg/attachments/signed_att.py @@ -62,6 +62,48 @@ def __init__(self, msg, data: bytes, name: str, mimetype: str, node: email.messa if self.__data is None: self.__data = data + def _handleFnc(self, _zip, filename, customPath: pathlib.Path, kwargs) -> pathlib.Path: + """ + "Handle Filename Conflict" + + Internal function for use in determining how to modify the saving path + when a file with the same name already exists. This is mainly because + any save function that uses files will need to do this functionality. + + :returns: A ``pathlib.Path`` object to where the file should be saved. + """ + fullFilename = customPath / filename + + overwriteExisting = kwargs.get('overwriteExisting', False) + + if _zip: + # If we are writing to a zip file and are not overwriting. + if not overwriteExisting: + name, ext = os.path.splitext(filename) + nameList = _zip.namelist() + if str(fullFilename).replace('\\', '/') in nameList: + for i in range(2, 100): + testName = customPath / f'{name} ({i}){ext}' + if str(testName).replace('\\', '/') not in nameList: + return testName + else: + # If we couldn't find one that didn't exist. + raise FileExistsError(f'Could not create the specified file because it already exists ("{fullFilename}").') + else: + if not overwriteExisting and fullFilename.exists(): + # Try to split the filename into a name and extension. + name, ext = os.path.splitext(filename) + # Try to add a number to it so that we can save without overwriting. + for i in range(2, 100): + testName = customPath / f'{name} ({i}){ext}' + if not testName.exists(): + return testName + else: + # If we couldn't find one that didn't exist. + raise FileExistsError(f'Could not create the specified file because it already exists ("{fullFilename}").') + + return fullFilename + def save(self, **kwargs) -> constants.SAVE_TYPE: """ Saves the attachment data.