A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
add unittest for transform_inline_html
phaile384bd78c5ee
diff --git a/src/paperless_mail/parsers.py b/src/paperless_mail/parsers.pyindex ed084618a..ccfdfe3a3 100644--- a/src/paperless_mail/parsers.py+++ b/src/paperless_mail/parsers.py@@ -296,22 +296,26 @@ class MailDocumentParser(DocumentParser):return response.content- def transform_inline_html(self, orig_html, attachments):+ @staticmethod+ def transform_inline_html(html, attachments):def clean_html_script(text: str):- text = text.replace("<script", "<div hidden ")- text = text.replace("</script", "</div")+ compiled_open = re.compile(re.escape("<script"), re.IGNORECASE)+ text = compiled_open.sub("<div hidden ", text)++ compiled_close = re.compile(re.escape("</script"), re.IGNORECASE)+ text = compiled_close.sub("</div", text)return text- orig_html = clean_html_script(orig_html)+ html_clean = clean_html_script(html)files = []for a in attachments:name_cid = "cid:" + a.content_idname_clean = "".join(e for e in name_cid if e.isalnum())files.append((name_clean, BytesIO(a.payload)))- orig_html = orig_html.replace(name_cid, name_clean)+ html_clean = html_clean.replace(name_cid, name_clean)- files.append(("index.html", StringIO(orig_html)))+ files.append(("index.html", StringIO(html_clean)))return filesdiff --git a/src/paperless_mail/tests/samples/sample.html b/src/paperless_mail/tests/samples/sample.htmlnew file mode 100644index 000000000..3aa5f615d--- /dev/null+++ b/src/paperless_mail/tests/samples/sample.html@@ -0,0 +1,15 @@+ <html>+ <head>+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">+ </head>+ <body>+ <p>Some Text</p>+ <p><img src="cid:[email protected]" alt=""></p>+ <p>and an embedded image.<br>+ </p>+ <p id="changeme">Paragraph unchanged.</p>+ <scRipt>+ document.getElementById("changeme").innerHTML = "Paragraph changed via Java Script.";+ </script>+ </body>+ </html>diff --git a/src/paperless_mail/tests/samples/sample.png b/src/paperless_mail/tests/samples/sample.pngnew file mode 100644index 000000000..162aadad0Binary files /dev/null and b/src/paperless_mail/tests/samples/sample.png differdiff --git a/src/paperless_mail/tests/test_eml.py b/src/paperless_mail/tests/test_eml.pyindex a0f4c0b77..cfbe31edb 100644--- a/src/paperless_mail/tests/test_eml.py+++ b/src/paperless_mail/tests/test_eml.py@@ -282,3 +282,27 @@ class TestParser(TestCase):# Check successful parsingparsed = parser.tika_parse(html)self.assertEqual(expected_text, parsed)++ def test_transform_inline_html(self):+ class MailAttachmentMock:+ def __init__(self, payload, content_id):+ self.payload = payload+ self.content_id = content_id++ parser = MailDocumentParser(None)++ result = None++ with open(os.path.join(self.SAMPLE_FILES, "sample.html")) as html_file:+ with open(os.path.join(self.SAMPLE_FILES, "sample.png"), "rb") as png_file:+ html = html_file.read()+ png = png_file.read()+ attachments = [+ MailAttachmentMock(png, "[email protected]"),+ ]+ result = parser.transform_inline_html(html, attachments)++ resulting_html = result[-1][1].read()+ self.assertTrue(result[-1][0] == "index.html")+ self.assertTrue(result[0][0] in resulting_html)+ self.assertFalse("<script" in resulting_html.lower())