vomebook commited on
Commit ·
c3a96a2
1
Parent(s): 456b371
fix: support image page Reader streams
Browse files- record_metadata.py +3 -2
- static/reader-contract.js +26 -9
- static/reader.js +39 -8
record_metadata.py
CHANGED
|
@@ -93,7 +93,7 @@ def reader_asset_url(asset: dict) -> str:
|
|
| 93 |
def reader_extension_for_asset(asset: dict, original_extension: str = "") -> str:
|
| 94 |
asset_path = str(asset.get("p") or "")
|
| 95 |
if asset_path.endswith("page-manifest.json"):
|
| 96 |
-
return "pdf-pages"
|
| 97 |
if asset.get("c"):
|
| 98 |
return "epub-chapters"
|
| 99 |
if asset.get("m") == "e":
|
|
@@ -129,7 +129,8 @@ def reader_asset_fields(asset: dict, original_extension: str) -> dict:
|
|
| 129 |
if asset.get(key):
|
| 130 |
if key == "c" and asset.get("cb") in {"vomebook/pdf-pages-v2", "vomebook/reader-assets-v2"}:
|
| 131 |
if asset.get("cb") == "vomebook/reader-assets-v2":
|
| 132 |
-
fields[field] =
|
|
|
|
| 133 |
else:
|
| 134 |
fields[field] = ("/api/reader-bucket-resource?path="
|
| 135 |
+ quote(asset[key], safe="/"))
|
|
|
|
| 93 |
def reader_extension_for_asset(asset: dict, original_extension: str = "") -> str:
|
| 94 |
asset_path = str(asset.get("p") or "")
|
| 95 |
if asset_path.endswith("page-manifest.json"):
|
| 96 |
+
return "image-pages" if asset.get("m") == "i" else "pdf-pages"
|
| 97 |
if asset.get("c"):
|
| 98 |
return "epub-chapters"
|
| 99 |
if asset.get("m") == "e":
|
|
|
|
| 129 |
if asset.get(key):
|
| 130 |
if key == "c" and asset.get("cb") in {"vomebook/pdf-pages-v2", "vomebook/reader-assets-v2"}:
|
| 131 |
if asset.get("cb") == "vomebook/reader-assets-v2":
|
| 132 |
+
fields[field] = ("https://voiceofml-search.hf.space/api/reader-bucket-resource?path="
|
| 133 |
+
+ quote(asset[key], safe="/"))
|
| 134 |
else:
|
| 135 |
fields[field] = ("/api/reader-bucket-resource?path="
|
| 136 |
+ quote(asset[key], safe="/"))
|
static/reader-contract.js
CHANGED
|
@@ -78,6 +78,7 @@
|
|
| 78 |
const modes = Object.freeze({
|
| 79 |
pdf: "pdf",
|
| 80 |
"pdf-pages": "pdf-pages",
|
|
|
|
| 81 |
epub: "foliate",
|
| 82 |
mobi: "foliate",
|
| 83 |
azw: "foliate",
|
|
@@ -133,6 +134,14 @@
|
|
| 133 |
pagination: true,
|
| 134 |
media: false
|
| 135 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
foliate: {
|
| 137 |
toc: true,
|
| 138 |
search: true,
|
|
@@ -275,12 +284,13 @@
|
|
| 275 |
|
| 276 |
// One parser for opening preload, manifest validation and page navigation.
|
| 277 |
// Dataset assets may be unversioned; the Bucket API requires a version.
|
| 278 |
-
function pdfPageSource(raw, base, bucketOrigin, filename = "page-manifest.json") {
|
| 279 |
try {
|
| 280 |
const url = new URL(raw, base);
|
| 281 |
if (url.username || url.password || url.hash) return null;
|
| 282 |
-
const bucket =
|
| 283 |
-
|
|
|
|
| 284 |
const directBucket = url.origin === "https://huggingface.co" &&
|
| 285 |
url.pathname.startsWith(bucketPrefix) && !url.search;
|
| 286 |
let path;
|
|
@@ -288,10 +298,10 @@
|
|
| 288 |
if (url.searchParams.getAll("path").length !== 1 ||
|
| 289 |
[...url.searchParams.keys()].some((key) => key !== "path")) return null;
|
| 290 |
path = url.searchParams.get("path") || "";
|
| 291 |
-
if (!isBucketPath(path, true)) return null;
|
| 292 |
} else if (directBucket) {
|
| 293 |
path = decodeURIComponent(url.pathname.slice(bucketPrefix.length));
|
| 294 |
-
if (!isBucketPath(path, true)) return null;
|
| 295 |
} else {
|
| 296 |
if (
|
| 297 |
url.protocol !== "https:" ||
|
|
@@ -302,7 +312,7 @@
|
|
| 302 |
const prefix = /^\/datasets\/vomebook\/Reader-Assets\/resolve\/[^/]+\//.exec(url.pathname);
|
| 303 |
if (!prefix) return null;
|
| 304 |
path = decodeURIComponent(url.pathname.slice(prefix[0].length));
|
| 305 |
-
if (!isBucketPath(path)) return null;
|
| 306 |
}
|
| 307 |
if (!["page-manifest.json", "ocr-manifest.json"].includes(filename) ||
|
| 308 |
!path.endsWith(`/${filename}`)) return null;
|
|
@@ -311,7 +321,7 @@
|
|
| 311 |
return {
|
| 312 |
root: rootPath,
|
| 313 |
assetUrl(relativePath) {
|
| 314 |
-
if (!isBucketPath(relativePath,
|
| 315 |
!relativePath.startsWith(rootPath.split("/").slice(0, 3).join("/") + "/"))
|
| 316 |
throw new Error("PDF_ASSET_INVALID");
|
| 317 |
const target = new URL(url.href);
|
|
@@ -332,6 +342,10 @@
|
|
| 332 |
}
|
| 333 |
}
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
function txtRelativePath(path) {
|
| 336 |
const value = String(path || "");
|
| 337 |
const dot = value.lastIndexOf(".");
|
|
@@ -367,6 +381,8 @@
|
|
| 367 |
? nativeExtension
|
| 368 |
: asset.m === "p" && path.endsWith("page-manifest.json")
|
| 369 |
? "pdf-pages"
|
|
|
|
|
|
|
| 370 |
: assetModes[asset.m];
|
| 371 |
const cdnBucket = (bucketName === "vomebook/pdf-pages-v2" || bucketName === "vomebook/reader-assets-v2") && staticBucketPathPattern.test(path);
|
| 372 |
const cdnUrl = cdnBucket
|
|
@@ -374,14 +390,14 @@
|
|
| 374 |
: "";
|
| 375 |
return {
|
| 376 |
ReaderLink: chapterBucket
|
| 377 |
-
|
| 378 |
: bucket
|
| 379 |
? cdnUrl || `${bucketBase}?path=${encodeURIComponent(path)}`
|
| 380 |
: assetBase + path,
|
| 381 |
ReaderExtension: asset.c ? "epub-chapters" : extension,
|
| 382 |
ReaderChapterManifest: asset.c
|
| 383 |
? chapterBucket
|
| 384 |
-
? `
|
| 385 |
: assetBase + asset.c
|
| 386 |
: "",
|
| 387 |
ReaderFallback: asset.f ? assetBase + asset.f : "",
|
|
@@ -477,6 +493,7 @@
|
|
| 477 |
isAssetSourcePath,
|
| 478 |
isBucketPath,
|
| 479 |
pdfPageSource,
|
|
|
|
| 480 |
txtRelativePath,
|
| 481 |
canonicalSourceUrl,
|
| 482 |
shortSourceId,
|
|
|
|
| 78 |
const modes = Object.freeze({
|
| 79 |
pdf: "pdf",
|
| 80 |
"pdf-pages": "pdf-pages",
|
| 81 |
+
"image-pages": "image-pages",
|
| 82 |
epub: "foliate",
|
| 83 |
mobi: "foliate",
|
| 84 |
azw: "foliate",
|
|
|
|
| 134 |
pagination: true,
|
| 135 |
media: false
|
| 136 |
},
|
| 137 |
+
"image-pages": {
|
| 138 |
+
toc: false,
|
| 139 |
+
search: false,
|
| 140 |
+
zoom: true,
|
| 141 |
+
bookmarks: true,
|
| 142 |
+
pagination: true,
|
| 143 |
+
media: false
|
| 144 |
+
},
|
| 145 |
foliate: {
|
| 146 |
toc: true,
|
| 147 |
search: true,
|
|
|
|
| 284 |
|
| 285 |
// One parser for opening preload, manifest validation and page navigation.
|
| 286 |
// Dataset assets may be unversioned; the Bucket API requires a version.
|
| 287 |
+
function pdfPageSource(raw, base, bucketOrigin, filename = "page-manifest.json", bucketName = "vomebook/pdf-pages-v2") {
|
| 288 |
try {
|
| 289 |
const url = new URL(raw, base);
|
| 290 |
if (url.username || url.password || url.hash) return null;
|
| 291 |
+
const bucket = [bucketOrigin, "https://voiceofml-search.hf.space"].includes(url.origin) &&
|
| 292 |
+
url.pathname === "/api/reader-bucket-resource";
|
| 293 |
+
const bucketPrefix = `/buckets/${bucketName}/resolve/`;
|
| 294 |
const directBucket = url.origin === "https://huggingface.co" &&
|
| 295 |
url.pathname.startsWith(bucketPrefix) && !url.search;
|
| 296 |
let path;
|
|
|
|
| 298 |
if (url.searchParams.getAll("path").length !== 1 ||
|
| 299 |
[...url.searchParams.keys()].some((key) => key !== "path")) return null;
|
| 300 |
path = url.searchParams.get("path") || "";
|
| 301 |
+
if (!isBucketPath(path, true, bucketName)) return null;
|
| 302 |
} else if (directBucket) {
|
| 303 |
path = decodeURIComponent(url.pathname.slice(bucketPrefix.length));
|
| 304 |
+
if (!isBucketPath(path, true, bucketName)) return null;
|
| 305 |
} else {
|
| 306 |
if (
|
| 307 |
url.protocol !== "https:" ||
|
|
|
|
| 312 |
const prefix = /^\/datasets\/vomebook\/Reader-Assets\/resolve\/[^/]+\//.exec(url.pathname);
|
| 313 |
if (!prefix) return null;
|
| 314 |
path = decodeURIComponent(url.pathname.slice(prefix[0].length));
|
| 315 |
+
if (!isBucketPath(path, false, bucketName)) return null;
|
| 316 |
}
|
| 317 |
if (!["page-manifest.json", "ocr-manifest.json"].includes(filename) ||
|
| 318 |
!path.endsWith(`/${filename}`)) return null;
|
|
|
|
| 321 |
return {
|
| 322 |
root: rootPath,
|
| 323 |
assetUrl(relativePath) {
|
| 324 |
+
if (!isBucketPath(relativePath, true, bucketName) ||
|
| 325 |
!relativePath.startsWith(rootPath.split("/").slice(0, 3).join("/") + "/"))
|
| 326 |
throw new Error("PDF_ASSET_INVALID");
|
| 327 |
const target = new URL(url.href);
|
|
|
|
| 342 |
}
|
| 343 |
}
|
| 344 |
|
| 345 |
+
function imagePageSource(raw, base, bucketOrigin) {
|
| 346 |
+
return pdfPageSource(raw, base, bucketOrigin, "page-manifest.json", "vomebook/reader-assets-v2");
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
function txtRelativePath(path) {
|
| 350 |
const value = String(path || "");
|
| 351 |
const dot = value.lastIndexOf(".");
|
|
|
|
| 381 |
? nativeExtension
|
| 382 |
: asset.m === "p" && path.endsWith("page-manifest.json")
|
| 383 |
? "pdf-pages"
|
| 384 |
+
: asset.m === "i" && path.endsWith("page-manifest.json")
|
| 385 |
+
? "image-pages"
|
| 386 |
: assetModes[asset.m];
|
| 387 |
const cdnBucket = (bucketName === "vomebook/pdf-pages-v2" || bucketName === "vomebook/reader-assets-v2") && staticBucketPathPattern.test(path);
|
| 388 |
const cdnUrl = cdnBucket
|
|
|
|
| 390 |
: "";
|
| 391 |
return {
|
| 392 |
ReaderLink: chapterBucket
|
| 393 |
+
? `https://huggingface.co/buckets/vomebook/reader-assets-v2/resolve/${asset.c.split("/").map(encodeURIComponent).join("/")}`
|
| 394 |
: bucket
|
| 395 |
? cdnUrl || `${bucketBase}?path=${encodeURIComponent(path)}`
|
| 396 |
: assetBase + path,
|
| 397 |
ReaderExtension: asset.c ? "epub-chapters" : extension,
|
| 398 |
ReaderChapterManifest: asset.c
|
| 399 |
? chapterBucket
|
| 400 |
+
? `${bucketBase}?path=${encodeURIComponent(asset.c)}`
|
| 401 |
: assetBase + asset.c
|
| 402 |
: "",
|
| 403 |
ReaderFallback: asset.f ? assetBase + asset.f : "",
|
|
|
|
| 493 |
isAssetSourcePath,
|
| 494 |
isBucketPath,
|
| 495 |
pdfPageSource,
|
| 496 |
+
imagePageSource,
|
| 497 |
txtRelativePath,
|
| 498 |
canonicalSourceUrl,
|
| 499 |
shortSourceId,
|
static/reader.js
CHANGED
|
@@ -900,13 +900,13 @@ async function goToPage(value, generation = beginReaderNavigation()) {
|
|
| 900 |
let shell = content.querySelector(
|
| 901 |
`.reader-page[data-page="${page}"], .reader-docx-page[data-page="${page}"]`
|
| 902 |
);
|
| 903 |
-
if (!shell &&
|
| 904 |
await pdfShellsReady;
|
| 905 |
shell = content.querySelector(`.reader-page[data-page="${page}"]`);
|
| 906 |
}
|
| 907 |
if (!isReaderGenerationCurrent("navigation", generation)) return false;
|
| 908 |
if (!shell) return false;
|
| 909 |
-
const imagePage =
|
| 910 |
if (imagePage && shell.dataset.renderState !== "rendered")
|
| 911 |
prioritizePdfManifestPrefetch(pdfPageManifest?.pageUrl(page));
|
| 912 |
const pendingPdf =
|
|
@@ -2583,7 +2583,9 @@ async function preloadPdfFirstPage(total) {
|
|
| 2583 |
} catch (_) {}
|
| 2584 |
}
|
| 2585 |
function pdfManifestSource() {
|
| 2586 |
-
return
|
|
|
|
|
|
|
| 2587 |
}
|
| 2588 |
function pdfPageEntry(page) {
|
| 2589 |
if (!pdfPageManifest || page < 1 || page > pdfPageManifest.pageCount) return null;
|
|
@@ -2660,7 +2662,7 @@ function prioritizePdfManifestPrefetch(target) {
|
|
| 2660 |
if (record && pdfManifestPrefetchActive.has(record)) record.image.fetchPriority = "high";
|
| 2661 |
}
|
| 2662 |
function schedulePdfManifestPrefetch(page) {
|
| 2663 |
-
if (
|
| 2664 |
const origin = Math.max(1, Math.min(documentState.pageCount + 1, page + 1));
|
| 2665 |
if (pdfManifestPrefetchOrigin === origin && pdfManifestPrefetchNext) {
|
| 2666 |
if (!pdfManifestPrefetchActive.size && !pdfManifestPrefetchTimer) queuePdfManifestPrefetch();
|
|
@@ -3278,6 +3280,31 @@ async function renderPdfPages(prepared) {
|
|
| 3278 |
releaseInitialRenderGate();
|
| 3279 |
}
|
| 3280 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3281 |
async function renderPdf(prepared) {
|
| 3282 |
const pdf = await prepared;
|
| 3283 |
assertReaderActive();
|
|
@@ -3443,11 +3470,11 @@ function renderPdfInBackground(shell, force = false, priority = pdfPagePriority(
|
|
| 3443 |
if (priority) {
|
| 3444 |
shell.dataset.renderPriority = "1";
|
| 3445 |
promotePdfRenderWaiter(shell);
|
| 3446 |
-
if (
|
| 3447 |
prioritizePdfManifestPrefetch(pdfPageManifest.pageUrl(Number(shell.dataset.page)));
|
| 3448 |
}
|
| 3449 |
if (shell._backgroundRender) {
|
| 3450 |
-
if (priority &&
|
| 3451 |
const image = shell.querySelector("img");
|
| 3452 |
if (image) image.fetchPriority = priority >= 3 ? "high" : "auto";
|
| 3453 |
}
|
|
@@ -3455,7 +3482,7 @@ function renderPdfInBackground(shell, force = false, priority = pdfPagePriority(
|
|
| 3455 |
return shell._backgroundRender;
|
| 3456 |
}
|
| 3457 |
const task =
|
| 3458 |
-
|
| 3459 |
? renderPdfManifestShell(shell, force, priority)
|
| 3460 |
: renderPdfShell(shell, force, priority);
|
| 3461 |
shell._backgroundRender = task;
|
|
@@ -4612,7 +4639,7 @@ function disposeFormatResources(mode) {
|
|
| 4612 |
swfPlayer?.remove();
|
| 4613 |
swfPlayer = null;
|
| 4614 |
}
|
| 4615 |
-
if (["pdf", "pdf-pages"].includes(mode)) {
|
| 4616 |
pdfDocument = null;
|
| 4617 |
pdfPageManifest = null;
|
| 4618 |
pdfOcrManifest = null;
|
|
@@ -4753,6 +4780,10 @@ function registerReaderFormatAdapters() {
|
|
| 4753 |
},
|
| 4754 |
renderPdfPages
|
| 4755 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4756 |
pdf: [loadPdfDocument, renderPdf],
|
| 4757 |
image: [loadImageDocument, renderImageDocument],
|
| 4758 |
text: [loadTextDocument, renderTextDocument],
|
|
|
|
| 900 |
let shell = content.querySelector(
|
| 901 |
`.reader-page[data-page="${page}"], .reader-docx-page[data-page="${page}"]`
|
| 902 |
);
|
| 903 |
+
if (!shell && ["pdf-pages", "image-pages"].includes(capability.mode)) {
|
| 904 |
await pdfShellsReady;
|
| 905 |
shell = content.querySelector(`.reader-page[data-page="${page}"]`);
|
| 906 |
}
|
| 907 |
if (!isReaderGenerationCurrent("navigation", generation)) return false;
|
| 908 |
if (!shell) return false;
|
| 909 |
+
const imagePage = ["pdf-pages", "image-pages"].includes(capability.mode);
|
| 910 |
if (imagePage && shell.dataset.renderState !== "rendered")
|
| 911 |
prioritizePdfManifestPrefetch(pdfPageManifest?.pageUrl(page));
|
| 912 |
const pendingPdf =
|
|
|
|
| 2583 |
} catch (_) {}
|
| 2584 |
}
|
| 2585 |
function pdfManifestSource() {
|
| 2586 |
+
return capability.mode === "image-pages"
|
| 2587 |
+
? VoiceOfMLReader.imagePageSource(sourceUrl, location.href, location.origin)
|
| 2588 |
+
: VoiceOfMLReader.pdfPageSource(sourceUrl, location.href, location.origin);
|
| 2589 |
}
|
| 2590 |
function pdfPageEntry(page) {
|
| 2591 |
if (!pdfPageManifest || page < 1 || page > pdfPageManifest.pageCount) return null;
|
|
|
|
| 2662 |
if (record && pdfManifestPrefetchActive.has(record)) record.image.fetchPriority = "high";
|
| 2663 |
}
|
| 2664 |
function schedulePdfManifestPrefetch(page) {
|
| 2665 |
+
if (!["pdf-pages", "image-pages"].includes(capability.mode) || !pdfPageManifest) return;
|
| 2666 |
const origin = Math.max(1, Math.min(documentState.pageCount + 1, page + 1));
|
| 2667 |
if (pdfManifestPrefetchOrigin === origin && pdfManifestPrefetchNext) {
|
| 2668 |
if (!pdfManifestPrefetchActive.size && !pdfManifestPrefetchTimer) queuePdfManifestPrefetch();
|
|
|
|
| 3280 |
releaseInitialRenderGate();
|
| 3281 |
}
|
| 3282 |
}
|
| 3283 |
+
async function renderImagePages(prepared) {
|
| 3284 |
+
const response = await prepared;
|
| 3285 |
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
| 3286 |
+
const manifest = JSON.parse(
|
| 3287 |
+
await VoiceOfMLReaderSecurity.readText(response, VoiceOfMLReaderSecurity.LIMITS.manifestBytes)
|
| 3288 |
+
);
|
| 3289 |
+
if (!manifest || manifest.version !== 1 || manifest.kind !== "image-page-stream" ||
|
| 3290 |
+
!Number.isInteger(manifest.page_count) || manifest.page_count < 1 ||
|
| 3291 |
+
!Array.isArray(manifest.pages) || manifest.pages.length !== manifest.page_count)
|
| 3292 |
+
throw new Error("IMAGE_MANIFEST_INVALID");
|
| 3293 |
+
const normalized = {
|
| 3294 |
+
version: 2,
|
| 3295 |
+
kind: "pdf-pages",
|
| 3296 |
+
page_count: manifest.page_count,
|
| 3297 |
+
entries: manifest.pages.map((entry, index) => ({
|
| 3298 |
+
page: index + 1,
|
| 3299 |
+
path: entry.path,
|
| 3300 |
+
bytes: entry.bytes,
|
| 3301 |
+
sha256: entry.sha256
|
| 3302 |
+
}))
|
| 3303 |
+
};
|
| 3304 |
+
return renderPdfPages(Promise.resolve(new Response(JSON.stringify(normalized), {
|
| 3305 |
+
headers: { "Content-Type": "application/json" }
|
| 3306 |
+
})));
|
| 3307 |
+
}
|
| 3308 |
async function renderPdf(prepared) {
|
| 3309 |
const pdf = await prepared;
|
| 3310 |
assertReaderActive();
|
|
|
|
| 3470 |
if (priority) {
|
| 3471 |
shell.dataset.renderPriority = "1";
|
| 3472 |
promotePdfRenderWaiter(shell);
|
| 3473 |
+
if (["pdf-pages", "image-pages"].includes(capability.mode) && pdfPageManifest)
|
| 3474 |
prioritizePdfManifestPrefetch(pdfPageManifest.pageUrl(Number(shell.dataset.page)));
|
| 3475 |
}
|
| 3476 |
if (shell._backgroundRender) {
|
| 3477 |
+
if (priority && ["pdf-pages", "image-pages"].includes(capability.mode)) {
|
| 3478 |
const image = shell.querySelector("img");
|
| 3479 |
if (image) image.fetchPriority = priority >= 3 ? "high" : "auto";
|
| 3480 |
}
|
|
|
|
| 3482 |
return shell._backgroundRender;
|
| 3483 |
}
|
| 3484 |
const task =
|
| 3485 |
+
["pdf-pages", "image-pages"].includes(capability.mode)
|
| 3486 |
? renderPdfManifestShell(shell, force, priority)
|
| 3487 |
: renderPdfShell(shell, force, priority);
|
| 3488 |
shell._backgroundRender = task;
|
|
|
|
| 4639 |
swfPlayer?.remove();
|
| 4640 |
swfPlayer = null;
|
| 4641 |
}
|
| 4642 |
+
if (["pdf", "pdf-pages", "image-pages"].includes(mode)) {
|
| 4643 |
pdfDocument = null;
|
| 4644 |
pdfPageManifest = null;
|
| 4645 |
pdfOcrManifest = null;
|
|
|
|
| 4780 |
},
|
| 4781 |
renderPdfPages
|
| 4782 |
],
|
| 4783 |
+
"image-pages": [
|
| 4784 |
+
() => fetchReaderResponse(),
|
| 4785 |
+
renderImagePages
|
| 4786 |
+
],
|
| 4787 |
pdf: [loadPdfDocument, renderPdf],
|
| 4788 |
image: [loadImageDocument, renderImageDocument],
|
| 4789 |
text: [loadTextDocument, renderTextDocument],
|