vomebook commited on
Commit ·
a4ddaa3
1
Parent(s): 76bdfe9
Fix PDF page stream OCR loading and search
Browse files- static/reader-contract.js +1 -1
- static/reader.js +38 -30
- tests/test_reader_refactor.py +115 -0
static/reader-contract.js
CHANGED
|
@@ -118,7 +118,7 @@
|
|
| 118 |
pdf: { toc: true, search: true, zoom: true, bookmarks: true, pagination: true, media: false },
|
| 119 |
"pdf-pages": {
|
| 120 |
toc: true,
|
| 121 |
-
search:
|
| 122 |
zoom: true,
|
| 123 |
bookmarks: true,
|
| 124 |
pagination: true,
|
|
|
|
| 118 |
pdf: { toc: true, search: true, zoom: true, bookmarks: true, pagination: true, media: false },
|
| 119 |
"pdf-pages": {
|
| 120 |
toc: true,
|
| 121 |
+
search: true,
|
| 122 |
zoom: true,
|
| 123 |
bookmarks: true,
|
| 124 |
pagination: true,
|
static/reader.js
CHANGED
|
@@ -860,11 +860,14 @@ async function goToPage(value, generation = beginReaderNavigation()) {
|
|
| 860 |
if (!documentState.pageCount || !isReaderGenerationCurrent("navigation", generation))
|
| 861 |
return false;
|
| 862 |
const page = VoiceOfMLReader.clampNumber(value, 1, documentState.pageCount, 1);
|
| 863 |
-
|
| 864 |
-
if (!isReaderGenerationCurrent("navigation", generation)) return false;
|
| 865 |
-
const shell = content.querySelector(
|
| 866 |
`.reader-page[data-page="${page}"], .reader-docx-page[data-page="${page}"]`
|
| 867 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
if (!shell) return false;
|
| 869 |
const imagePage = capability.mode === "pdf-pages";
|
| 870 |
if (imagePage && shell.dataset.renderState !== "rendered")
|
|
@@ -875,6 +878,7 @@ async function goToPage(value, generation = beginReaderNavigation()) {
|
|
| 875 |
: null;
|
| 876 |
if (!isReaderGenerationCurrent("navigation", generation)) return false;
|
| 877 |
viewport.scrollTop = shell.offsetTop;
|
|
|
|
| 878 |
pageNavigationLockUntil = performance.now() + 500;
|
| 879 |
if (pendingPdf) cancelSpeculativePdfRenders(shell);
|
| 880 |
const pendingImage = imagePage ? renderPdfInBackground(shell, false, true) : null;
|
|
@@ -2364,7 +2368,7 @@ function preloadPdfFirstPage() {
|
|
| 2364 |
else if (!navigator.connection?.saveData) {
|
| 2365 |
pdfFirstPagePreload = new Image();
|
| 2366 |
pdfFirstPagePreload.decoding = "async";
|
| 2367 |
-
pdfFirstPagePreload.fetchPriority = "
|
| 2368 |
pdfFirstPagePreload.src = pageUrl.href;
|
| 2369 |
}
|
| 2370 |
} catch (_) {}
|
|
@@ -2494,31 +2498,37 @@ async function loadPdfOcrManifest() {
|
|
| 2494 |
catch (error) { pdfOcrManifestPromise = null; throw error; }
|
| 2495 |
}
|
| 2496 |
|
| 2497 |
-
async function renderPdfOcrText(shell
|
| 2498 |
-
if (shell.dataset.textReady === "1" || !
|
| 2499 |
if (shell._ocrPromise) return shell._ocrPromise;
|
| 2500 |
const layer = shell.querySelector(".reader-pdf-text");
|
| 2501 |
if (!layer) return;
|
| 2502 |
const epoch = shell._textEpoch || 0;
|
| 2503 |
const task = (async () => {
|
| 2504 |
-
|
| 2505 |
-
|
| 2506 |
-
|
| 2507 |
-
|
| 2508 |
-
|
| 2509 |
-
|
| 2510 |
-
|
| 2511 |
-
|
| 2512 |
-
|
| 2513 |
-
|
| 2514 |
-
|
| 2515 |
-
|
| 2516 |
-
|
| 2517 |
-
|
| 2518 |
-
|
| 2519 |
-
|
| 2520 |
-
|
| 2521 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2522 |
})().finally(() => { if (shell._ocrPromise === task) delete shell._ocrPromise; });
|
| 2523 |
shell._ocrPromise = task;
|
| 2524 |
return task;
|
|
@@ -2703,7 +2713,6 @@ async function renderPdfPages(prepared) {
|
|
| 2703 |
const totalPages = manifest.page_count;
|
| 2704 |
if (!source) throw new Error("PDF_MANIFEST_INVALID");
|
| 2705 |
pdfPageManifest = { ...source, pageCount: totalPages };
|
| 2706 |
-
if (ocrManifestUrl) loadPdfOcrManifest().catch(() => {});
|
| 2707 |
updateDocumentState({ pageCount: totalPages });
|
| 2708 |
pageInput.max = String(totalPages);
|
| 2709 |
document.querySelector("#page-total").textContent = `/ ${totalPages}`;
|
|
@@ -2734,6 +2743,7 @@ async function renderPdfPages(prepared) {
|
|
| 2734 |
if (entry.isIntersecting) {
|
| 2735 |
promotePdfRenderWaiter(entry.target);
|
| 2736 |
renderPdfInBackground(entry.target, false, true);
|
|
|
|
| 2737 |
}
|
| 2738 |
}
|
| 2739 |
},
|
|
@@ -2983,9 +2993,7 @@ function renderPdfManifestShell(shell, force = false, priority = false) {
|
|
| 2983 |
});
|
| 2984 |
assertReaderActive();
|
| 2985 |
shell.style.aspectRatio = `${image.naturalWidth || 1} / ${image.naturalHeight || 1}`;
|
| 2986 |
-
const loadText =
|
| 2987 |
-
? pdfOcrManifestPromise.then(() => renderPdfOcrText(shell, pdfPageEntry(Number(shell.dataset.page))))
|
| 2988 |
-
: renderPdfOcrText(shell, pdfPageEntry(Number(shell.dataset.page)));
|
| 2989 |
loadText.catch(() => {});
|
| 2990 |
image.classList.add("ready");
|
| 2991 |
shell.dataset.renderState = "rendered";
|
|
@@ -5065,7 +5073,7 @@ document
|
|
| 5065 |
const fullSearchButton = document.querySelector("#full-search-toggle");
|
| 5066 |
fullSearchButton.textContent = "全文搜索";
|
| 5067 |
fullSearchButton.setAttribute("aria-label", "全文搜索");
|
| 5068 |
-
fullSearchButton.hidden = !capability.features.search;
|
| 5069 |
const fullSearchInput = fullSearchView.querySelector("#full-search-input"),
|
| 5070 |
fullSearchStatus = fullSearchView.querySelector("#full-search-status"),
|
| 5071 |
fullSearchResultsNode = fullSearchView.querySelector("#full-search-results"),
|
|
@@ -6036,7 +6044,7 @@ function syncCapabilityControls() {
|
|
| 6036 |
document.querySelector(".page-controls").hidden = !capability.features.pagination;
|
| 6037 |
document.querySelector(".zoom-controls").hidden = !capability.features.zoom;
|
| 6038 |
mediaTab.hidden = !capability.features.media;
|
| 6039 |
-
fullSearchButton.hidden = !capability.features.search;
|
| 6040 |
bookmarkRibbon.hidden = !capability.features.bookmarks;
|
| 6041 |
readingProgressSummary.querySelector(".reader-progress-bookmark").hidden =
|
| 6042 |
!capability.features.bookmarks;
|
|
|
|
| 860 |
if (!documentState.pageCount || !isReaderGenerationCurrent("navigation", generation))
|
| 861 |
return false;
|
| 862 |
const page = VoiceOfMLReader.clampNumber(value, 1, documentState.pageCount, 1);
|
| 863 |
+
let shell = content.querySelector(
|
|
|
|
|
|
|
| 864 |
`.reader-page[data-page="${page}"], .reader-docx-page[data-page="${page}"]`
|
| 865 |
);
|
| 866 |
+
if (!shell && capability.mode === "pdf-pages") {
|
| 867 |
+
await pdfShellsReady;
|
| 868 |
+
shell = content.querySelector(`.reader-page[data-page="${page}"]`);
|
| 869 |
+
}
|
| 870 |
+
if (!isReaderGenerationCurrent("navigation", generation)) return false;
|
| 871 |
if (!shell) return false;
|
| 872 |
const imagePage = capability.mode === "pdf-pages";
|
| 873 |
if (imagePage && shell.dataset.renderState !== "rendered")
|
|
|
|
| 878 |
: null;
|
| 879 |
if (!isReaderGenerationCurrent("navigation", generation)) return false;
|
| 880 |
viewport.scrollTop = shell.offsetTop;
|
| 881 |
+
if (imagePage && shell.dataset.renderState === "rendered") renderPdfOcrText(shell);
|
| 882 |
pageNavigationLockUntil = performance.now() + 500;
|
| 883 |
if (pendingPdf) cancelSpeculativePdfRenders(shell);
|
| 884 |
const pendingImage = imagePage ? renderPdfInBackground(shell, false, true) : null;
|
|
|
|
| 2368 |
else if (!navigator.connection?.saveData) {
|
| 2369 |
pdfFirstPagePreload = new Image();
|
| 2370 |
pdfFirstPagePreload.decoding = "async";
|
| 2371 |
+
pdfFirstPagePreload.fetchPriority = "high";
|
| 2372 |
pdfFirstPagePreload.src = pageUrl.href;
|
| 2373 |
}
|
| 2374 |
} catch (_) {}
|
|
|
|
| 2498 |
catch (error) { pdfOcrManifestPromise = null; throw error; }
|
| 2499 |
}
|
| 2500 |
|
| 2501 |
+
async function renderPdfOcrText(shell) {
|
| 2502 |
+
if (shell.dataset.textReady === "1" || !pdfPageManifest || !ocrManifestUrl) return;
|
| 2503 |
if (shell._ocrPromise) return shell._ocrPromise;
|
| 2504 |
const layer = shell.querySelector(".reader-pdf-text");
|
| 2505 |
if (!layer) return;
|
| 2506 |
const epoch = shell._textEpoch || 0;
|
| 2507 |
const task = (async () => {
|
| 2508 |
+
try {
|
| 2509 |
+
const manifest = await loadPdfOcrManifest();
|
| 2510 |
+
if (epoch !== (shell._textEpoch || 0)) return;
|
| 2511 |
+
const page = Number(shell.dataset.page);
|
| 2512 |
+
const entry = manifest.pages[page - 1];
|
| 2513 |
+
await acquirePdfTextSlot(isPdfPageVisible(shell));
|
| 2514 |
+
try {
|
| 2515 |
+
if (epoch !== (shell._textEpoch || 0)) return;
|
| 2516 |
+
const payload = await readPdfOcrJson(pdfOcrSource().assetUrl(entry.o),
|
| 2517 |
+
VoiceOfMLReaderSecurity.LIMITS.chapterBytes, entry.os, entry.ob);
|
| 2518 |
+
assertReaderActive();
|
| 2519 |
+
if (epoch !== (shell._textEpoch || 0)) return;
|
| 2520 |
+
VoiceOfMLReaderSecurity.validatePdfOcrPage(payload, page);
|
| 2521 |
+
populateOcrTextLayer(layer, payload.blocks);
|
| 2522 |
+
shell._bookmarkTextItems = payload.blocks.map((block) => ({ text: block.t, y: block.b?.[1] || 0 }));
|
| 2523 |
+
shell.dataset.textReady = "1";
|
| 2524 |
+
highlightPdfText(shell);
|
| 2525 |
+
} finally {
|
| 2526 |
+
releasePdfTextSlot();
|
| 2527 |
+
}
|
| 2528 |
+
} catch (error) {
|
| 2529 |
+
if (!readerAbortController.signal.aborted)
|
| 2530 |
+
console.warn(`PDF page ${shell.dataset.page} OCR text failed`, error);
|
| 2531 |
+
}
|
| 2532 |
})().finally(() => { if (shell._ocrPromise === task) delete shell._ocrPromise; });
|
| 2533 |
shell._ocrPromise = task;
|
| 2534 |
return task;
|
|
|
|
| 2713 |
const totalPages = manifest.page_count;
|
| 2714 |
if (!source) throw new Error("PDF_MANIFEST_INVALID");
|
| 2715 |
pdfPageManifest = { ...source, pageCount: totalPages };
|
|
|
|
| 2716 |
updateDocumentState({ pageCount: totalPages });
|
| 2717 |
pageInput.max = String(totalPages);
|
| 2718 |
document.querySelector("#page-total").textContent = `/ ${totalPages}`;
|
|
|
|
| 2743 |
if (entry.isIntersecting) {
|
| 2744 |
promotePdfRenderWaiter(entry.target);
|
| 2745 |
renderPdfInBackground(entry.target, false, true);
|
| 2746 |
+
if (entry.target.dataset.renderState === "rendered") renderPdfOcrText(entry.target);
|
| 2747 |
}
|
| 2748 |
}
|
| 2749 |
},
|
|
|
|
| 2993 |
});
|
| 2994 |
assertReaderActive();
|
| 2995 |
shell.style.aspectRatio = `${image.naturalWidth || 1} / ${image.naturalHeight || 1}`;
|
| 2996 |
+
const loadText = renderPdfOcrText(shell);
|
|
|
|
|
|
|
| 2997 |
loadText.catch(() => {});
|
| 2998 |
image.classList.add("ready");
|
| 2999 |
shell.dataset.renderState = "rendered";
|
|
|
|
| 5073 |
const fullSearchButton = document.querySelector("#full-search-toggle");
|
| 5074 |
fullSearchButton.textContent = "全文搜索";
|
| 5075 |
fullSearchButton.setAttribute("aria-label", "全文搜索");
|
| 5076 |
+
fullSearchButton.hidden = !capability.features.search || (capability.mode === "pdf-pages" && !ocrManifestUrl);
|
| 5077 |
const fullSearchInput = fullSearchView.querySelector("#full-search-input"),
|
| 5078 |
fullSearchStatus = fullSearchView.querySelector("#full-search-status"),
|
| 5079 |
fullSearchResultsNode = fullSearchView.querySelector("#full-search-results"),
|
|
|
|
| 6044 |
document.querySelector(".page-controls").hidden = !capability.features.pagination;
|
| 6045 |
document.querySelector(".zoom-controls").hidden = !capability.features.zoom;
|
| 6046 |
mediaTab.hidden = !capability.features.media;
|
| 6047 |
+
fullSearchButton.hidden = !capability.features.search || (capability.mode === "pdf-pages" && !ocrManifestUrl);
|
| 6048 |
bookmarkRibbon.hidden = !capability.features.bookmarks;
|
| 6049 |
readingProgressSummary.querySelector(".reader-progress-bookmark").hidden =
|
| 6050 |
!capability.features.bookmarks;
|
tests/test_reader_refactor.py
CHANGED
|
@@ -122,6 +122,121 @@ class ReaderRefactorTest(unittest.TestCase):
|
|
| 122 |
image.wait_for()
|
| 123 |
self.assertEqual(image.get_attribute("src"), source + "/pages/page-000003.webp")
|
| 124 |
self.assertTrue(image.evaluate("image => image.complete && image.naturalWidth > 0"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
def test_download_during_prepare_does_not_interrupt_reader(self):
|
| 127 |
pending_content = []
|
|
|
|
| 122 |
image.wait_for()
|
| 123 |
self.assertEqual(image.get_attribute("src"), source + "/pages/page-000003.webp")
|
| 124 |
self.assertTrue(image.evaluate("image => image.complete && image.naturalWidth > 0"))
|
| 125 |
+
self.assertTrue(self.page.locator("#full-search-toggle").evaluate("node => node.hidden"))
|
| 126 |
+
|
| 127 |
+
def test_pdf_pages_recovers_ocr_text_and_searches_valid_index(self):
|
| 128 |
+
root = "objects/aa/" + "a" * 64 + "/" + "b" * 16
|
| 129 |
+
source = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/" + root
|
| 130 |
+
ocr_path = root + "/ocr-manifest.json"
|
| 131 |
+
book_path = root + "/ocr/book-text.json.gz"
|
| 132 |
+
book = gzip.compress(json.dumps({"version": 2, "kind": "pdf-book-text", "complete": True,
|
| 133 |
+
"offset_unit": "unicode-codepoint", "source_sha256": "a" * 64, "page_count": 2,
|
| 134 |
+
"pages": [{"page": page, "text": "indexed text" if page == 1 else "",
|
| 135 |
+
"layout": {"offset_unit": "unicode-codepoint"}, "text_spans": []}
|
| 136 |
+
for page in (1, 2)]}).encode())
|
| 137 |
+
manifest = {"version": 1, "kind": "pdf-ocr", "complete": True,
|
| 138 |
+
"source_sha256": "a" * 64, "profile": "test-layout-v1-retry", "page_count": 2,
|
| 139 |
+
"pages": [{"p": page, "o": root + f"/ocr/page-{page:06d}.json.gz"} for page in (1, 2)],
|
| 140 |
+
"book_text": {"path": book_path, "bytes": len(book), "sha256": hashlib.sha256(book).hexdigest()}}
|
| 141 |
+
attempts, manifest_attempts, image_requests = [], [], []
|
| 142 |
+
self.serve(json.dumps({"version": 2, "kind": "pdf-pages", "page_count": 2}), "application/json")
|
| 143 |
+
def image(route):
|
| 144 |
+
image_requests.append(route.request.url)
|
| 145 |
+
route.fulfill(content_type="image/webp", body=support.IMAGE_FIXTURES["webp"][1])
|
| 146 |
+
def resource(route):
|
| 147 |
+
path = urllib.parse.parse_qs(urllib.parse.urlsplit(route.request.url).query)["path"][0]
|
| 148 |
+
if path == ocr_path:
|
| 149 |
+
manifest_attempts.append(path)
|
| 150 |
+
if len(manifest_attempts) == 1:
|
| 151 |
+
route.fulfill(status=503, body="temporarily unavailable")
|
| 152 |
+
return
|
| 153 |
+
route.fulfill(json=manifest)
|
| 154 |
+
return
|
| 155 |
+
if path == book_path:
|
| 156 |
+
route.fulfill(content_type="application/gzip", body=book)
|
| 157 |
+
return
|
| 158 |
+
if path.endswith("page-000001.json.gz"):
|
| 159 |
+
attempts.append(path)
|
| 160 |
+
if len(attempts) == 1:
|
| 161 |
+
route.fulfill(status=503, body="temporarily unavailable")
|
| 162 |
+
return
|
| 163 |
+
number = int(path.rsplit("page-", 1)[1][:6])
|
| 164 |
+
payload = {"version": 1, "kind": "pdf-ocr-page", "page": number,
|
| 165 |
+
"width": 100, "height": 140,
|
| 166 |
+
"blocks": [{"t": "Recovered text", "b": [0.1, 0.2, 0.8, 0.3]}]}
|
| 167 |
+
route.fulfill(content_type="application/gzip", body=gzip.compress(json.dumps(payload).encode()))
|
| 168 |
+
self.page.route(source + "/pages/**", image)
|
| 169 |
+
self.page.route("**/api/reader-bucket-resource**", resource)
|
| 170 |
+
self.open(self.reader_url("pdf-pages", url=source + "/page-manifest.json",
|
| 171 |
+
ocr_manifest=self.origin + "/api/reader-bucket-resource?path=" + ocr_path))
|
| 172 |
+
self.assertFalse(self.page.locator("#full-search-toggle").evaluate("node => node.hidden"))
|
| 173 |
+
self.page.wait_for_function("() => document.querySelector('.reader-page[data-page=\"1\"]')?.dataset.renderState === 'rendered' && !document.querySelector('.reader-page[data-page=\"1\"]')._ocrPromise")
|
| 174 |
+
self.assertEqual(len(manifest_attempts), 2)
|
| 175 |
+
self.assertEqual(len(attempts), 1)
|
| 176 |
+
first_image_requests = image_requests.count(source + "/pages/page-000001.webp")
|
| 177 |
+
self.page.locator("#page-number").fill("2")
|
| 178 |
+
self.page.locator("#page-number").dispatch_event("change")
|
| 179 |
+
self.page.wait_for_function("() => document.querySelector('#page-number').value === '2'")
|
| 180 |
+
self.page.locator("#page-number").fill("1")
|
| 181 |
+
self.page.locator("#page-number").dispatch_event("change")
|
| 182 |
+
self.page.locator('.reader-page[data-page="1"][data-text-ready="1"]').wait_for()
|
| 183 |
+
self.assertEqual(len(attempts), 2)
|
| 184 |
+
self.assertEqual(image_requests.count(source + "/pages/page-000001.webp"), first_image_requests)
|
| 185 |
+
self.page.locator("#history").click()
|
| 186 |
+
self.page.locator("#full-search-toggle").click()
|
| 187 |
+
self.page.locator("#full-search-input").fill("indexed")
|
| 188 |
+
self.page.wait_for_function("() => document.querySelector('#full-search-status').textContent === '1 个结果'")
|
| 189 |
+
self.assertEqual(self.page.locator(".full-search-result").count(), 1)
|
| 190 |
+
|
| 191 |
+
def test_pdf_pages_ocr_uses_versioned_manifest_paths_after_image(self):
|
| 192 |
+
root = "objects/aa/" + "a" * 64
|
| 193 |
+
image_root = root + "/" + "1" * 16
|
| 194 |
+
ocr_root = root + "/" + "2" * 16
|
| 195 |
+
text_root = root + "/" + "3" * 16
|
| 196 |
+
source = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/" + image_root
|
| 197 |
+
ocr_path = ocr_root + "/ocr-manifest.json"
|
| 198 |
+
payloads = [gzip.compress(json.dumps({"version": 1, "kind": "pdf-ocr-page", "page": number,
|
| 199 |
+
"width": 100, "height": 140,
|
| 200 |
+
"blocks": [{"t": f"Page {number} text", "b": [0.2, 0.2, 0.7, 0.3]}]}).encode())
|
| 201 |
+
for number in (1, 2)]
|
| 202 |
+
manifest = {"version": 1, "kind": "pdf-ocr", "complete": True,
|
| 203 |
+
"source_sha256": "a" * 64, "profile": "test-layout-v1-1234567890abcdef",
|
| 204 |
+
"page_count": 2, "pages": [
|
| 205 |
+
{"p": number, "o": text_root + f"/ocr/page-{number:06d}.json.gz",
|
| 206 |
+
"os": hashlib.sha256(payloads[number - 1]).hexdigest(), "ob": len(payloads[number - 1])}
|
| 207 |
+
for number in (1, 2)],
|
| 208 |
+
"book_text": {"path": ocr_root + "/ocr/book-text.json.gz", "bytes": 1,
|
| 209 |
+
"sha256": "a" * 64},
|
| 210 |
+
"page_manifest": {"path": image_root + "/page-manifest.json"}}
|
| 211 |
+
held, requested = [], []
|
| 212 |
+
self.serve(json.dumps({"version": 2, "kind": "pdf-pages", "page_count": 2}), "application/json")
|
| 213 |
+
def serve_ocr(route):
|
| 214 |
+
path = urllib.parse.parse_qs(urllib.parse.urlsplit(route.request.url).query)["path"][0]
|
| 215 |
+
requested.append(path)
|
| 216 |
+
if path == ocr_path:
|
| 217 |
+
held.append(route)
|
| 218 |
+
elif path in [entry["o"] for entry in manifest["pages"]]:
|
| 219 |
+
number = int(path.rsplit("page-", 1)[1][:6])
|
| 220 |
+
route.fulfill(content_type="application/gzip", body=payloads[number - 1])
|
| 221 |
+
else:
|
| 222 |
+
route.fulfill(status=404)
|
| 223 |
+
self.page.route("**/api/reader-bucket-resource**", serve_ocr)
|
| 224 |
+
self.page.route(source + "/pages/**", lambda route: route.fulfill(
|
| 225 |
+
content_type="image/webp", body=support.IMAGE_FIXTURES["webp"][1]))
|
| 226 |
+
self.open(self.reader_url("pdf-pages", url=source + "/page-manifest.json",
|
| 227 |
+
ocr_manifest=self.origin + "/api/reader-bucket-resource?path=" + ocr_path))
|
| 228 |
+
self.page.locator('.reader-page[data-page="1"] img.ready').wait_for()
|
| 229 |
+
self.assertEqual(len(held), 1)
|
| 230 |
+
self.assertNotEqual(self.page.locator('.reader-page[data-page="1"]').get_attribute("data-text-ready"), "1")
|
| 231 |
+
held.pop().fulfill(json=manifest)
|
| 232 |
+
self.page.locator('.reader-page[data-page="1"][data-text-ready="1"]').wait_for()
|
| 233 |
+
self.assertIn("Page 1 text", self.page.locator('.reader-page[data-page="1"] .reader-pdf-text').text_content())
|
| 234 |
+
self.page.locator("#page-number").fill("2")
|
| 235 |
+
self.page.locator("#page-number").dispatch_event("change")
|
| 236 |
+
self.page.locator('.reader-page[data-page="2"][data-text-ready="1"]').wait_for()
|
| 237 |
+
self.assertIn("Page 2 text", self.page.locator('.reader-page[data-page="2"] .reader-pdf-text').text_content())
|
| 238 |
+
self.assertEqual(requested, [ocr_path, text_root + "/ocr/page-000001.json.gz",
|
| 239 |
+
text_root + "/ocr/page-000002.json.gz"])
|
| 240 |
|
| 241 |
def test_download_during_prepare_does_not_interrupt_reader(self):
|
| 242 |
pending_content = []
|