Remove pre-pymupdf exceptions and detect pymupdf ones

This commit is contained in:
deeplow 2023-12-19 15:16:25 +00:00
parent e0b092692d
commit 80db7bb02e
No known key found for this signature in database
GPG key ID: 577982871529A52A
2 changed files with 13 additions and 26 deletions

View file

@ -174,7 +174,10 @@ class DocumentToPixels(DangerzoneConverter):
# Convert input document to PDF
conversion = conversions[mime_type]
if conversion["type"] is None:
doc = fitz.open("/tmp/input_file", filetype=mime_type)
try:
doc = fitz.open("/tmp/input_file", filetype=mime_type)
except (ValueError, fitz.FileDataError):
raise errors.DocCorruptedException()
elif conversion["type"] == "libreoffice":
libreoffice_ext = conversion.get("libreoffice_ext", None)
# Disable conversion for HWP/HWPX on specific platforms. See:
@ -207,11 +210,13 @@ class DocumentToPixels(DangerzoneConverter):
# https://github.com/freedomofpress/dangerzone/issues/494
if not os.path.exists(pdf_filename):
raise errors.LibreofficeFailure()
doc = fitz.open(pdf_filename)
try:
doc = fitz.open(pdf_filename)
except (ValueError, fitz.FileDataError):
raise errors.DocCorruptedException()
else:
raise errors.InvalidGMConversion(
f"Invalid conversion type {conversion['type']} for MIME type {mime_type}"
)
# NOTE: This should never be reached
raise errors.DocFormatUnsupported()
self.percentage += 3
# Obtain number of pages

View file

@ -47,12 +47,9 @@ class LibreofficeFailure(ConversionException):
error_message = "Conversion to PDF with LibreOffice failed"
class InvalidGMConversion(ConversionException):
class DocCorruptedException(ConversionException):
error_code = ERROR_SHIFT + 30
error_message = "Invalid conversion (Graphics Magic)"
def __init__(self, error_message: str) -> None:
super(error_message)
error_message = "The document appears to be corrupted and could not be opened"
class PagesException(ConversionException):
@ -89,21 +86,6 @@ class PageCountMismatch(PagesException):
)
class PDFtoPPMException(ConversionException):
error_code = ERROR_SHIFT + 50
error_message = "Error converting PDF to Pixels (pdftoppm)"
class PDFtoPPMInvalidHeader(PDFtoPPMException):
error_code = ERROR_SHIFT + 51
error_message = "Error converting PDF to Pixels (Invalid PPM header)"
class PDFtoPPMInvalidDepth(PDFtoPPMException):
error_code = ERROR_SHIFT + 52
error_message = "Error converting PDF to Pixels (Invalid PPM depth)"
class InterruptedConversion(ConversionException):
"""Protocol received num of bytes different than expected"""
@ -113,7 +95,7 @@ class InterruptedConversion(ConversionException):
)
class UnexpectedConversionError(PDFtoPPMException):
class UnexpectedConversionError(ConversionException):
error_code = ERROR_SHIFT + 100
error_message = "Some unexpected error occurred while converting the document"