refactor: compose export controller with explicit snapshots and task lifecycle
This commit is contained in:
+16
-144
@@ -1,123 +1,13 @@
|
||||
"""Save a rendered, self-contained browser archive without a visible browser."""
|
||||
"""Browser task for a portable MHTML archive, without a visible browser."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import base64
|
||||
import hashlib
|
||||
import mimetypes
|
||||
import re
|
||||
import tempfile
|
||||
from email import encoders, policy
|
||||
from email.parser import BytesParser
|
||||
from html import escape, unescape
|
||||
from html.parser import HTMLParser
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from PySide6.QtCore import QTimer, QUrl
|
||||
from PySide6.QtWebEngineCore import QWebEngineDownloadRequest, QWebEnginePage, QWebEngineProfile
|
||||
from PySide6.QtWidgets import QMessageBox, QProgressDialog
|
||||
|
||||
|
||||
def portable_archive(data: bytes) -> bytes:
|
||||
"""Use archive IDs instead of file URLs, and embed CSS font dependencies.
|
||||
|
||||
Chromium can block file: subresources even when present in an MHTML archive.
|
||||
Native page saving also omits local fonts that were not painted on the page.
|
||||
"""
|
||||
import tinycss2
|
||||
message = BytesParser(policy=policy.default).parsebytes(data)
|
||||
parts = [p for p in message.walk() if not p.is_multipart()]
|
||||
aliases = {}
|
||||
for part in parts:
|
||||
location = part.get("Content-Location", "")
|
||||
if location:
|
||||
identifier = "mdhelper-" + hashlib.sha256(location.encode()).hexdigest()[:24]
|
||||
if "Content-ID" in part:
|
||||
identifier = part["Content-ID"].strip("<>")
|
||||
else:
|
||||
part["Content-ID"] = f"<{identifier}>"
|
||||
aliases[location] = "cid:" + identifier
|
||||
|
||||
def css_resource(url, base):
|
||||
if url.startswith(("#", "data:", "cid:")):
|
||||
return url
|
||||
absolute = urljoin(base, url)
|
||||
if absolute in aliases:
|
||||
return aliases[absolute]
|
||||
qurl = QUrl(absolute)
|
||||
if qurl.isLocalFile():
|
||||
path = Path(qurl.toLocalFile())
|
||||
# Bundled styles list legacy font fallbacks not shipped with the app.
|
||||
if not path.is_file():
|
||||
return "data:,"
|
||||
mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
||||
return f"data:{mime};base64," + base64.b64encode(path.read_bytes()).decode("ascii")
|
||||
if qurl.scheme() in {"http", "https"}:
|
||||
raise ValueError("以下样式资源未能包含在离线归档中,请改用本地资源后重试:" + absolute)
|
||||
return absolute
|
||||
|
||||
def css_text(text, base):
|
||||
tokens = tinycss2.parse_stylesheet(text)
|
||||
def visit(items):
|
||||
for token in items:
|
||||
if token.type == "url":
|
||||
value = css_resource(token.value, base)
|
||||
token.value = value
|
||||
token.representation = 'url("' + value.replace('"', '%22') + '")'
|
||||
elif token.type == "function" and token.lower_name == "url":
|
||||
arguments = [item for item in token.arguments if item.type not in {"whitespace", "comment"}]
|
||||
value = arguments[0].value if len(arguments) == 1 and arguments[0].type == "string" else tinycss2.serialize(token.arguments).strip()
|
||||
token.arguments = tinycss2.parse_component_value_list('"' + css_resource(value, base).replace('"', '%22') + '"')
|
||||
else:
|
||||
if token.type == "at-rule" and token.lower_at_keyword == "import":
|
||||
for value in token.prelude:
|
||||
if value.type == "string":
|
||||
value.value = css_resource(value.value, base)
|
||||
value.representation = '"' + value.value + '"'
|
||||
for name in ("content", "prelude", "arguments"):
|
||||
if getattr(token, name, None):
|
||||
visit(getattr(token, name))
|
||||
visit(tokens)
|
||||
return tinycss2.serialize(tokens)
|
||||
|
||||
class HtmlResources(HTMLParser):
|
||||
def __init__(self, text):
|
||||
super().__init__(convert_charrefs=False)
|
||||
self.changes = []
|
||||
self.offsets = [0] + [m.end() for m in re.finditer("\n", text)]
|
||||
self.feed(text)
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
raw = self.get_starttag_text()
|
||||
line, column = self.getpos()
|
||||
offset = self.offsets[line - 1] + column
|
||||
for match in re.finditer(r'''\b(?:src|href|poster)\s*=\s*(["'])(.*?)\1''', raw, re.S):
|
||||
value = unescape(match[2])
|
||||
if value in aliases:
|
||||
self.changes.append((offset + match.start(2), offset + match.end(2), escape(aliases[value], quote=True)))
|
||||
|
||||
for part in parts:
|
||||
kind = part.get_content_type()
|
||||
if kind not in {"text/html", "text/css"}:
|
||||
continue
|
||||
text = part.get_payload(decode=True).decode(part.get_content_charset() or "utf-8")
|
||||
if kind == "text/css":
|
||||
text = css_text(text, part.get("Content-Location", ""))
|
||||
else:
|
||||
chunks, offset = [], 0
|
||||
for start, end, replacement in HtmlResources(text).changes:
|
||||
chunks.extend((text[offset:start], replacement)); offset = end
|
||||
chunks.append(text[offset:])
|
||||
text = "".join(chunks)
|
||||
if "Content-Transfer-Encoding" in part:
|
||||
del part["Content-Transfer-Encoding"]
|
||||
part.set_param("charset", "utf-8")
|
||||
part.set_payload(text.encode("utf-8"))
|
||||
encoders.encode_base64(part)
|
||||
# Chromium expects literal Content-Location values, not RFC 2047 folded URLs.
|
||||
return message.as_bytes(policy=policy.SMTP.clone(max_line_length=0, refold_source="none", utf8=True))
|
||||
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
from .export_lifecycle import ExportLifecycle, page_readiness_script
|
||||
from .mhtml_archive import portable_archive
|
||||
|
||||
class MhtmlExportController:
|
||||
def __init__(self, owner, output: Path, content: str, source_path: Path):
|
||||
@@ -125,14 +15,9 @@ class MhtmlExportController:
|
||||
self.base_url = QUrl.fromLocalFile(str(source_path.parent.resolve()) + os.sep)
|
||||
self.finished = False
|
||||
self.request = self.page = self.profile = self.temporary = None
|
||||
self.progress = QProgressDialog("正在准备离线文档…", "取消", 0, 0, owner)
|
||||
self.progress.setWindowTitle("导出 MHTML")
|
||||
self.progress.setMinimumDuration(0)
|
||||
self.progress.setAutoClose(False)
|
||||
self.progress.canceled.connect(self.cancel)
|
||||
self.timeout = QTimer(self.progress)
|
||||
self.timeout.setSingleShot(True)
|
||||
self.timeout.timeout.connect(lambda: self.fail("离线文档导出超时,请检查图片和公式资源。"))
|
||||
self.lifecycle = ExportLifecycle(owner.context.parent, "导出 MHTML", "正在准备离线文档…", self.cancel,
|
||||
lambda: self.fail("离线文档导出超时,请检查图片和公式资源。"))
|
||||
self.progress = self.lifecycle.progress
|
||||
|
||||
def start(self):
|
||||
try:
|
||||
@@ -140,14 +25,13 @@ class MhtmlExportController:
|
||||
self.temporary = tempfile.TemporaryDirectory(prefix=".mdhelper-mhtml-", dir=self.output.parent, ignore_cleanup_errors=True)
|
||||
self.staged = Path(self.temporary.name) / "document.mhtml"
|
||||
# A separate profile keeps unrelated downloads out of this operation.
|
||||
self.profile = QWebEngineProfile(self.owner)
|
||||
self.page = QWebEnginePage(self.profile, self.owner)
|
||||
self.owner._configure_web_settings(self.page.settings())
|
||||
self.profile = QWebEngineProfile(self.owner.context.parent)
|
||||
self.page = QWebEnginePage(self.profile, self.owner.context.parent)
|
||||
self.owner.context.configure_web_settings(self.page.settings())
|
||||
self.profile.downloadRequested.connect(self.download_requested)
|
||||
self.page.loadFinished.connect(self.loaded)
|
||||
self.timeout.start(120_000)
|
||||
self.page.setHtml(self.content, self.base_url)
|
||||
self.progress.show()
|
||||
self.lifecycle.start()
|
||||
except OSError as exc:
|
||||
self.fail(str(exc))
|
||||
|
||||
@@ -163,16 +47,7 @@ class MhtmlExportController:
|
||||
def wait_ready(self):
|
||||
if self.finished:
|
||||
return
|
||||
self.page.runJavaScript(r"""(() => {
|
||||
const images = [...document.images].filter(i => i.getAttribute('src') || i.getAttribute('srcset'));
|
||||
const lazy = images.filter(i => i.loading === 'lazy');
|
||||
lazy.forEach(i => i.loading = 'eager');
|
||||
if (lazy.length || images.some(i => !i.complete) ||
|
||||
(document.fonts && document.fonts.status !== 'loaded') ||
|
||||
window.mdhelperRenderReady === false) return 'waiting';
|
||||
const missing = images.filter(i => !i.naturalWidth);
|
||||
return missing.length ? 'missing:' + missing.map(i => i.src).join('\n') : 'ready';
|
||||
})();""", self.ready)
|
||||
self.page.runJavaScript(page_readiness_script(True), self.ready)
|
||||
|
||||
def ready(self, state):
|
||||
if self.finished:
|
||||
@@ -208,22 +83,22 @@ class MhtmlExportController:
|
||||
except (OSError, ValueError, UnicodeError) as exc:
|
||||
self.fail(str(exc))
|
||||
return
|
||||
self.owner.status_label.setText(f"已导出:{self.output}")
|
||||
self.owner.context.status(f"已导出:{self.output}")
|
||||
self.cleanup()
|
||||
|
||||
def cancel(self):
|
||||
if not self.finished:
|
||||
self.owner.status_label.setText("已取消 MHTML 导出,原有输出文件保持不变")
|
||||
self.owner.context.status("已取消 MHTML 导出,原有输出文件保持不变")
|
||||
self.cleanup()
|
||||
|
||||
def fail(self, message):
|
||||
if not self.finished:
|
||||
self.cleanup()
|
||||
QMessageBox.warning(self.owner, "导出失败", message)
|
||||
QMessageBox.warning(self.owner.context.parent, "导出失败", message)
|
||||
|
||||
def cleanup(self):
|
||||
self.finished = True
|
||||
self.timeout.stop()
|
||||
self.lifecycle.close()
|
||||
if self.request is not None and not self.request.isFinished():
|
||||
self.request.cancel()
|
||||
if self.page is not None:
|
||||
@@ -235,7 +110,4 @@ class MhtmlExportController:
|
||||
if self.temporary is not None:
|
||||
self.temporary.cleanup()
|
||||
self.temporary = None
|
||||
self.progress.blockSignals(True)
|
||||
self.progress.close()
|
||||
self.progress.deleteLater()
|
||||
self.owner._mhtml_exports.discard(self)
|
||||
|
||||
Reference in New Issue
Block a user