diff options
Diffstat (limited to 'mammoth/docx')
| -rw-r--r-- | mammoth/docx/__init__.py | 10 | ||||
| -rw-r--r-- | mammoth/docx/files.py | 14 |
2 files changed, 19 insertions, 5 deletions
diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 3848736..a0576cf 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -19,13 +19,14 @@ from ..zips import open_zip _empty_result = results.success([]) -def read(fileobj): +def read(fileobj, external_file_access=False): zip_file = open_zip(fileobj, "r") part_paths = _find_part_paths(zip_file) read_part_with_body = _part_with_body_reader( getattr(fileobj, "name", None), zip_file, part_paths=part_paths, + external_file_access=external_file_access, ) return results.combine([ @@ -134,7 +135,7 @@ def _read_document(zip_file, read_part_with_body, notes, comments, part_paths): ) -def _part_with_body_reader(document_path, zip_file, part_paths): +def _part_with_body_reader(document_path, zip_file, part_paths, external_file_access): content_types = _try_read_entry_or_default( zip_file, "[Content_Types].xml", @@ -156,7 +157,10 @@ def _part_with_body_reader(document_path, zip_file, part_paths): default=Numbering.EMPTY, ) - files = Files(None if document_path is None else os.path.dirname(document_path)) + files = Files( + None if document_path is None else os.path.dirname(document_path), + external_file_access=external_file_access, + ) def read_part(name, reader, default=_undefined): relationships = _read_relationships(zip_file, _find_relationships_path_for(name)) diff --git a/mammoth/docx/files.py b/mammoth/docx/files.py index 61cf8d5..e26a43f 100644 --- a/mammoth/docx/files.py +++ b/mammoth/docx/files.py @@ -11,10 +11,16 @@ except ImportError: class Files(object): - def __init__(self, base): + def __init__(self, base, external_file_access): self._base = base - + self._external_file_access = external_file_access + def open(self, uri): + if not self._external_file_access: + raise ExternalFileAccessIsDisabledError( + "could not open external image '{0}', external file access is disabled".format(uri) + ) + try: if _is_absolute(uri): return contextlib.closing(urlopen(uri)) @@ -34,3 +40,7 @@ def _is_absolute(url): class InvalidFileReferenceError(ValueError): pass + + +class ExternalFileAccessIsDisabledError(InvalidFileReferenceError): + pass |
