in export/securedrop_export/print/service.py [0:0]
def _needs_pdf_conversion(self, filename: Path):
"""
Checks mimetype of a file and returns True if file must be converted
to PDF before attempting to print.
Raises ExportException for unprintable mimetypes or on mimetype
discovery error.
"""
mimetype = None
supported_types: set[str] = set()
try:
supported_types = self._get_supported_mimetypes_libreoffice(LIBREOFFICE_DESKTOP_DIR)
except OSError as e:
logger.error(f"Could not get supported mimetypes list: {e}")
raise ExportException(sdstatus=Status.ERROR_MIMETYPE_DISCOVERY)
if len(supported_types) == 0:
raise ExportException(sdstatus=Status.ERROR_MIMETYPE_DISCOVERY)
try:
# b'filename that may have spaces.docx: application/bla\n'
# use magic bytes (-M) for filetype detection
mimetype = (
subprocess.check_output(["mimetype", filename]).decode().split(":")[-1].strip()
)
except subprocess.CalledProcessError:
logger.error(f"Could not process mimetype of {filename}")
raise ExportException(sdstatus=Status.ERROR_MIMETYPE_DISCOVERY)
# Don't print "audio/*", "video/*", or archive mimetypes
if mimetype.startswith(MIMETYPE_UNPRINTABLE) or mimetype in MIMETYPE_ARCHIVE:
logger.info(f"Unprintable file {filename}")
raise ExportException(sdstatus=Status.ERROR_UNPRINTABLE_TYPE)
elif mimetype in MIMETYPE_PRINT_WITHOUT_CONVERSION:
# Print directly, no need to convert
logger.debug(f"{filename} can skip PDF conversion")
return False
elif mimetype in supported_types:
logger.debug(f"{filename} will be converted to PDF")
return True
else:
logger.error("Mimetype is unknown or unsupported.")
raise ExportException(sdstatus=Status.ERROR_MIMETYPE_UNSUPPORTED)