vomebook commited on
Commit ·
9ae8ec6
1
Parent(s): 0232c8f
feat: support additional Reader formats
Browse files- app.py +2 -1
- reader_assets.py +4 -3
- record_metadata.py +3 -2
- static/app.js +5 -5
- static/reader-contract.js +14 -4
- static/reader-resources.js +5 -0
- static/reader.html +1 -1
- static/reader.js +26 -0
- tests/test_reader_backend.py +18 -0
- tests/test_reader_refactor.py +26 -0
- tests/test_reader_regression.js +12 -0
- upstream_http.py +1 -1
app.py
CHANGED
|
@@ -1633,7 +1633,8 @@ READER_RESOURCE_TYPES = {
|
|
| 1633 |
"css": "text/css", "jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png",
|
| 1634 |
"gif": "image/gif", "bmp": "image/bmp", "webp": "image/webp", "jxl": "image/jxl", "svg": "image/svg+xml", "ttf": "font/ttf",
|
| 1635 |
"otf": "font/otf", "woff": "font/woff", "woff2": "font/woff2", "mp3": "audio/mpeg",
|
| 1636 |
-
"
|
|
|
|
| 1637 |
}
|
| 1638 |
def initialize_reader_bucket_state(target_app: FastAPI) -> None:
|
| 1639 |
target_app.state.reader_bucket_cache = OrderedDict()
|
|
|
|
| 1633 |
"css": "text/css", "jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png",
|
| 1634 |
"gif": "image/gif", "bmp": "image/bmp", "webp": "image/webp", "jxl": "image/jxl", "svg": "image/svg+xml", "ttf": "font/ttf",
|
| 1635 |
"otf": "font/otf", "woff": "font/woff", "woff2": "font/woff2", "mp3": "audio/mpeg",
|
| 1636 |
+
"wav": "audio/wav", "m4a": "audio/mp4", "flac": "audio/flac", "mpga": "audio/mpeg",
|
| 1637 |
+
"mp4": "video/mp4", "mov": "video/quicktime", "swf": "application/x-shockwave-flash", "gz": "application/gzip",
|
| 1638 |
}
|
| 1639 |
def initialize_reader_bucket_state(target_app: FastAPI) -> None:
|
| 1640 |
target_app.state.reader_bucket_cache = OrderedDict()
|
reader_assets.py
CHANGED
|
@@ -17,13 +17,14 @@ READER_ASSET_FILENAMES = {
|
|
| 17 |
"t": {"document.txt"},
|
| 18 |
"k": {"document.md"},
|
| 19 |
"i": {"document.webp", "document.jpg", "document.jpeg", "document.png", "document.gif", "document.bmp"},
|
| 20 |
-
"a": {"audio.mp3"},
|
| 21 |
-
"v": {"video.mp4"},
|
|
|
|
| 22 |
}
|
| 23 |
READER_BUCKET_PATH_RE = re.compile(
|
| 24 |
r"^(?:objects/[0-9a-f]{2}/[0-9a-f]{64}/[0-9a-f]{16}/(?:page-manifest\.json|ocr-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl)|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz))|"
|
| 25 |
r"ebook-chapters/objects/[0-9a-f]{2}/[0-9a-f]{64}/[a-z0-9-]+/[a-z0-9-]+-epub-chapters-v[0-9]+-bucket/epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/(?!\.{1,2}(?:/|$))[^/]+(?:/(?!\.{1,2}(?:/|$))[^/]+)*|epub-search-index\.json\.gz)|"
|
| 26 |
-
r"objects/[0-9a-f]{2}/[0-9a-f]{64}/(?:[0-9a-f]{16}/)?(?:[a-z0-9-]+/)?document\.(?:epub|mobi|azw|azw3|fb2|docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp))$"
|
| 27 |
)
|
| 28 |
READER_EBOOK_BUCKET = "vomebook/pdf-pages"
|
| 29 |
|
|
|
|
| 17 |
"t": {"document.txt"},
|
| 18 |
"k": {"document.md"},
|
| 19 |
"i": {"document.webp", "document.jpg", "document.jpeg", "document.png", "document.gif", "document.bmp"},
|
| 20 |
+
"a": {"audio.mp3", "audio.wav", "audio.m4a", "audio.flac", "audio.mpga"},
|
| 21 |
+
"v": {"video.mp4", "video.mov"},
|
| 22 |
+
"f": {"document.swf"},
|
| 23 |
}
|
| 24 |
READER_BUCKET_PATH_RE = re.compile(
|
| 25 |
r"^(?:objects/[0-9a-f]{2}/[0-9a-f]{64}/[0-9a-f]{16}/(?:page-manifest\.json|ocr-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl)|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz))|"
|
| 26 |
r"ebook-chapters/objects/[0-9a-f]{2}/[0-9a-f]{64}/[a-z0-9-]+/[a-z0-9-]+-epub-chapters-v[0-9]+-bucket/epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/(?!\.{1,2}(?:/|$))[^/]+(?:/(?!\.{1,2}(?:/|$))[^/]+)*|epub-search-index\.json\.gz)|"
|
| 27 |
+
r"objects/[0-9a-f]{2}/[0-9a-f]{64}/(?:[0-9a-f]{16}/)?(?:[a-z0-9-]+/)?(?:document\.(?:epub|mobi|azw|azw3|fb2|docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp|swf)|audio\.(?:mp3|wav|m4a|flac|mpga)|video\.(?:mp4|mov)))$"
|
| 28 |
)
|
| 29 |
READER_EBOOK_BUCKET = "vomebook/pdf-pages"
|
| 30 |
|
record_metadata.py
CHANGED
|
@@ -6,7 +6,7 @@ from urllib.parse import quote
|
|
| 6 |
|
| 7 |
READER_ASSET_MODES = {
|
| 8 |
"p": "pdf", "e": "epub", "d": "docx", "h": "html",
|
| 9 |
-
"t": "txt", "k": "md", "i": "webp", "a": "audio", "v": "video",
|
| 10 |
}
|
| 11 |
READER_NATIVE_FOLIATE_EXTENSIONS = frozenset({"epub", "mobi", "azw", "azw3", "fb2"})
|
| 12 |
READER_ASSETS_BASE = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/"
|
|
@@ -82,7 +82,8 @@ def reader_asset_url(asset: dict) -> str:
|
|
| 82 |
and raw_path.endswith("/page-manifest.json")):
|
| 83 |
return "https://huggingface.co/buckets/vomebook/pdf-pages/resolve/" + path
|
| 84 |
if (bucket == "vomebook/pdf-pages" and raw_path.startswith("objects/")
|
| 85 |
-
and re.search(r"/document\.(?:docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp)
|
|
|
|
| 86 |
return "https://huggingface.co/buckets/vomebook/pdf-pages/resolve/" + path
|
| 87 |
if bucket == "vomebook/pdf-pages":
|
| 88 |
return "/api/reader-bucket-resource?path=" + path
|
|
|
|
| 6 |
|
| 7 |
READER_ASSET_MODES = {
|
| 8 |
"p": "pdf", "e": "epub", "d": "docx", "h": "html",
|
| 9 |
+
"t": "txt", "k": "md", "i": "webp", "a": "audio", "v": "video", "f": "swf",
|
| 10 |
}
|
| 11 |
READER_NATIVE_FOLIATE_EXTENSIONS = frozenset({"epub", "mobi", "azw", "azw3", "fb2"})
|
| 12 |
READER_ASSETS_BASE = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/"
|
|
|
|
| 82 |
and raw_path.endswith("/page-manifest.json")):
|
| 83 |
return "https://huggingface.co/buckets/vomebook/pdf-pages/resolve/" + path
|
| 84 |
if (bucket == "vomebook/pdf-pages" and raw_path.startswith("objects/")
|
| 85 |
+
and re.search(r"/(?:document\.(?:docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp|swf)"
|
| 86 |
+
r"|audio\.(?:mp3|wav|m4a|flac|mpga)|video\.(?:mp4|mov))$", raw_path)):
|
| 87 |
return "https://huggingface.co/buckets/vomebook/pdf-pages/resolve/" + path
|
| 88 |
if bucket == "vomebook/pdf-pages":
|
| 89 |
return "/api/reader-bucket-resource?path=" + path
|
static/app.js
CHANGED
|
@@ -7,7 +7,7 @@ const ORDERED_EXTENSIONS = [
|
|
| 7 |
"jpg", "jpeg", "png", "gif", "bmp", "tif", "psd",
|
| 8 |
"html", "htm", "mht", "vcf",
|
| 9 |
"mp3", "wav", "m4a", "flac", "mpga", "wma", "ape", "amr",
|
| 10 |
-
"mp4", "mov", "flv", "f4v", "rm", "rmvb", "mkv", "avi", "mpg", "mts", "wmv", "asx", "swf",
|
| 11 |
];
|
| 12 |
const FILE_ICON_MAP = {
|
| 13 |
pdf: "pdf",
|
|
@@ -20,9 +20,9 @@ const FILE_ICON_MAP = {
|
|
| 20 |
bmp: "image", webp: "image", svg: "image",
|
| 21 |
html: "code", htm: "code", aspx: "code", css: "code", js: "code", xml: "code",
|
| 22 |
json: "code", ini: "code", bat: "code",
|
| 23 |
-
mp4: "video", flv: "video", swf: "video", rm: "video", rmvb: "video",
|
| 24 |
wmv: "video", mpg: "video", mts: "video", f4v: "video", asx: "video",
|
| 25 |
-
mp3: "audio", wav: "audio", wma: "audio", ape: "audio", m4a: "audio", mpga: "audio",
|
| 26 |
iso: "archive", msi: "archive", dat: "archive",
|
| 27 |
exe: "file",
|
| 28 |
db: "database", itf: "database",
|
|
@@ -3062,7 +3062,7 @@ function buildResultHTML(rec, idx) {
|
|
| 3062 |
<button class="result-action-btn" data-action="copy" data-link="${escapeHTML(getCopyableLink(recordLink))}">复制链接</button>
|
| 3063 |
<button class="result-action-btn primary" data-action="download" data-filename="${escapeHTML(rec.File + (rec.Extension ? '.' + rec.Extension : ''))}" data-link="${escapeHTML(recordLink)}">下载</button>
|
| 3064 |
<a href="${escapeHTML(getPreviewLink(recordPath))}" class="result-action-btn" target="_blank" rel="noopener noreferrer">仓库查看</a>
|
| 3065 |
-
|
| 3066 |
</div>`;
|
| 3067 |
}
|
| 3068 |
|
|
@@ -3085,7 +3085,7 @@ function refreshResultReaderActions() {
|
|
| 3085 |
VSCROLL.measuredRowKeys[index] = null;
|
| 3086 |
}
|
| 3087 |
button.dataset.readerUrl = getReaderLink(actionRecord);
|
| 3088 |
-
|
| 3089 |
if (button.textContent !== label) { button.textContent = label; VSCROLL.measuredRowKeys[index] = null; }
|
| 3090 |
}
|
| 3091 |
VSCROLL.measuredWindowKey = "";
|
|
|
|
| 7 |
"jpg", "jpeg", "png", "gif", "bmp", "tif", "psd",
|
| 8 |
"html", "htm", "mht", "vcf",
|
| 9 |
"mp3", "wav", "m4a", "flac", "mpga", "wma", "ape", "amr",
|
| 10 |
+
"mp4", "mov", "flv", "f4v", "rm", "rmvb", "mkv", "avi", "mpg", "mts", "ts", "wmv", "asx", "swf",
|
| 11 |
];
|
| 12 |
const FILE_ICON_MAP = {
|
| 13 |
pdf: "pdf",
|
|
|
|
| 20 |
bmp: "image", webp: "image", svg: "image",
|
| 21 |
html: "code", htm: "code", aspx: "code", css: "code", js: "code", xml: "code",
|
| 22 |
json: "code", ini: "code", bat: "code",
|
| 23 |
+
mp4: "video", mov: "video", flv: "video", swf: "video", asx: "video", rm: "video", rmvb: "video",
|
| 24 |
wmv: "video", mpg: "video", mts: "video", f4v: "video", asx: "video",
|
| 25 |
+
mp3: "audio", wav: "audio", wma: "audio", ape: "audio", m4a: "audio", flac: "audio", mpga: "audio",
|
| 26 |
iso: "archive", msi: "archive", dat: "archive",
|
| 27 |
exe: "file",
|
| 28 |
db: "database", itf: "database",
|
|
|
|
| 3062 |
<button class="result-action-btn" data-action="copy" data-link="${escapeHTML(getCopyableLink(recordLink))}">复制链接</button>
|
| 3063 |
<button class="result-action-btn primary" data-action="download" data-filename="${escapeHTML(rec.File + (rec.Extension ? '.' + rec.Extension : ''))}" data-link="${escapeHTML(recordLink)}">下载</button>
|
| 3064 |
<a href="${escapeHTML(getPreviewLink(recordPath))}" class="result-action-btn" target="_blank" rel="noopener noreferrer">仓库查看</a>
|
| 3065 |
+
${readerActionUrl ? `<button class="result-action-btn" data-action="read" data-reader-url="${escapeHTML(readerActionUrl)}">${["audio", "video", "swf"].includes(VoiceOfMLReader.capability(readerActionRecord.ReaderExtension || readerActionRecord.Extension).mode) ? "在线播放" : "在线阅读"}</button>` : ""}
|
| 3066 |
</div>`;
|
| 3067 |
}
|
| 3068 |
|
|
|
|
| 3085 |
VSCROLL.measuredRowKeys[index] = null;
|
| 3086 |
}
|
| 3087 |
button.dataset.readerUrl = getReaderLink(actionRecord);
|
| 3088 |
+
const label = ["audio", "video", "swf"].includes(VoiceOfMLReader.capability(actionRecord.ReaderExtension || actionRecord.Extension).mode) ? "在线播放" : "在线阅读";
|
| 3089 |
if (button.textContent !== label) { button.textContent = label; VSCROLL.measuredRowKeys[index] = null; }
|
| 3090 |
}
|
| 3091 |
VSCROLL.measuredWindowKey = "";
|
static/reader-contract.js
CHANGED
|
@@ -105,7 +105,8 @@
|
|
| 105 |
audio: "audio",
|
| 106 |
mp4: "video",
|
| 107 |
mov: "video",
|
| 108 |
-
video: "video"
|
|
|
|
| 109 |
});
|
| 110 |
const articleExtensions = Object.freeze(Object.keys(modes));
|
| 111 |
const features = Object.freeze({
|
|
@@ -183,6 +184,14 @@
|
|
| 183 |
bookmarks: true,
|
| 184 |
pagination: false,
|
| 185 |
media: true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
}
|
| 187 |
});
|
| 188 |
|
|
@@ -209,7 +218,7 @@
|
|
| 209 |
const assetVersion = String.raw`(?:[0-9a-f]{16}/)?`;
|
| 210 |
const assetPages = String.raw`(?:page-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl))`;
|
| 211 |
const assetOcr = String.raw`(?:ocr-manifest\.json|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz))`;
|
| 212 |
-
const assetDocument = String.raw`(?:linearized\.pdf|document\.(?:pdf|epub|mobi|azw|azw3|fb2|docx|html|txt|md|webp)|book\.epub|audio\.mp3|video\.mp4)`;
|
| 213 |
const assetChapters = String.raw`(?:chapter-manifest\.json|epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/[A-Za-z0-9._~%+\-/]+|epub-search-index\.json\.gz))`;
|
| 214 |
const ebookBucketPathPattern = new RegExp(`^ebook-chapters/${assetRoot}[a-z0-9-]+/[a-z0-9-]+-epub-chapters-v[0-9]+-bucket/epub-chapters/(?:chapter-manifest\\.json|chapters/chapter-[0-9]{4}\\.xhtml|resources/(?!\\.{1,2}(?:/|$))[^/]+(?:/(?!\\.{1,2}(?:/|$))[^/]+)*|epub-search-index\\.json\\.gz)$`);
|
| 215 |
const assetPrimaryPattern = new RegExp(
|
|
@@ -219,7 +228,7 @@
|
|
| 219 |
`^(?:pdf_manifest\\.json|${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr}|(?:[a-z0-9-]+/)?(?:${assetDocument}|${assetChapters})))$`
|
| 220 |
);
|
| 221 |
const bucketPathPattern = new RegExp(`^${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr})$`);
|
| 222 |
-
const staticBucketPathPattern = new RegExp(`^${assetRoot}(?:[0-9a-f]{16}/)?(?:[a-z0-9-]+/)?document\\.(?:docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp)$`);
|
| 223 |
const versionedBucketPathPattern = new RegExp(`^${assetRoot}[0-9a-f]{16}/(?:${assetPages}|${assetOcr})$`);
|
| 224 |
const assetBase = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/";
|
| 225 |
const assetModes = Object.freeze({
|
|
@@ -231,7 +240,8 @@
|
|
| 231 |
k: "md",
|
| 232 |
i: "webp",
|
| 233 |
a: "audio",
|
| 234 |
-
v: "video"
|
|
|
|
| 235 |
});
|
| 236 |
|
| 237 |
function isBucketPath(path, versioned = false, bucket = "vomebook/pdf-pages") {
|
|
|
|
| 105 |
audio: "audio",
|
| 106 |
mp4: "video",
|
| 107 |
mov: "video",
|
| 108 |
+
video: "video",
|
| 109 |
+
swf: "swf"
|
| 110 |
});
|
| 111 |
const articleExtensions = Object.freeze(Object.keys(modes));
|
| 112 |
const features = Object.freeze({
|
|
|
|
| 184 |
bookmarks: true,
|
| 185 |
pagination: false,
|
| 186 |
media: true
|
| 187 |
+
},
|
| 188 |
+
swf: {
|
| 189 |
+
toc: false,
|
| 190 |
+
search: false,
|
| 191 |
+
zoom: false,
|
| 192 |
+
bookmarks: true,
|
| 193 |
+
pagination: false,
|
| 194 |
+
media: true
|
| 195 |
}
|
| 196 |
});
|
| 197 |
|
|
|
|
| 218 |
const assetVersion = String.raw`(?:[0-9a-f]{16}/)?`;
|
| 219 |
const assetPages = String.raw`(?:page-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl))`;
|
| 220 |
const assetOcr = String.raw`(?:ocr-manifest\.json|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz))`;
|
| 221 |
+
const assetDocument = String.raw`(?:linearized\.pdf|document\.(?:pdf|epub|mobi|azw|azw3|fb2|docx|html|txt|md|webp|swf)|book\.epub|audio\.(?:mp3|wav|m4a|flac|mpga)|video\.(?:mp4|mov))`;
|
| 222 |
const assetChapters = String.raw`(?:chapter-manifest\.json|epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/[A-Za-z0-9._~%+\-/]+|epub-search-index\.json\.gz))`;
|
| 223 |
const ebookBucketPathPattern = new RegExp(`^ebook-chapters/${assetRoot}[a-z0-9-]+/[a-z0-9-]+-epub-chapters-v[0-9]+-bucket/epub-chapters/(?:chapter-manifest\\.json|chapters/chapter-[0-9]{4}\\.xhtml|resources/(?!\\.{1,2}(?:/|$))[^/]+(?:/(?!\\.{1,2}(?:/|$))[^/]+)*|epub-search-index\\.json\\.gz)$`);
|
| 224 |
const assetPrimaryPattern = new RegExp(
|
|
|
|
| 228 |
`^(?:pdf_manifest\\.json|${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr}|(?:[a-z0-9-]+/)?(?:${assetDocument}|${assetChapters})))$`
|
| 229 |
);
|
| 230 |
const bucketPathPattern = new RegExp(`^${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr})$`);
|
| 231 |
+
const staticBucketPathPattern = new RegExp(`^${assetRoot}(?:[0-9a-f]{16}/)?(?:[a-z0-9-]+/)?(?:document\\.(?:docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp|swf)|audio\\.(?:mp3|wav|m4a|flac|mpga)|video\\.(?:mp4|mov))$`);
|
| 232 |
const versionedBucketPathPattern = new RegExp(`^${assetRoot}[0-9a-f]{16}/(?:${assetPages}|${assetOcr})$`);
|
| 233 |
const assetBase = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/";
|
| 234 |
const assetModes = Object.freeze({
|
|
|
|
| 240 |
k: "md",
|
| 241 |
i: "webp",
|
| 242 |
a: "audio",
|
| 243 |
+
v: "video",
|
| 244 |
+
f: "swf"
|
| 245 |
});
|
| 246 |
|
| 247 |
function isBucketPath(path, versioned = false, bucket = "vomebook/pdf-pages") {
|
static/reader-resources.js
CHANGED
|
@@ -31,6 +31,11 @@
|
|
| 31 |
url: "https://cdn.jsdelivr.net/npm/docx-preview@0.4.0/dist/docx-preview.min.js",
|
| 32 |
file: "docx-preview.min.js",
|
| 33 |
sha256: "051ef503f2677d53159a388b7384e950eda41ea4e47a103e5e36f124d7faea40"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
};
|
| 36 |
for (const vendor of Object.values(vendors)) {
|
|
|
|
| 31 |
url: "https://cdn.jsdelivr.net/npm/docx-preview@0.4.0/dist/docx-preview.min.js",
|
| 32 |
file: "docx-preview.min.js",
|
| 33 |
sha256: "051ef503f2677d53159a388b7384e950eda41ea4e47a103e5e36f124d7faea40"
|
| 34 |
+
},
|
| 35 |
+
ruffle: {
|
| 36 |
+
url: "https://unpkg.com/@ruffle-rs/ruffle@0.6.0/ruffle.js",
|
| 37 |
+
file: "ruffle.js",
|
| 38 |
+
sha256: "a686a305345b06542dddedada71869104916a61e393f174687571528ac4225f5"
|
| 39 |
}
|
| 40 |
};
|
| 41 |
for (const vendor of Object.values(vendors)) {
|
static/reader.html
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
<meta
|
| 7 |
http-equiv="Content-Security-Policy"
|
| 8 |
-
content="default-src 'self'; base-uri 'self' blob:; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline' blob:; img-src 'self' data: blob: https:; media-src 'self' https://voiceofml-search.hf.space https://hf-mirror.com https://huggingface.co https://*.hf.co https://*.huggingface.co; connect-src 'self' blob: https://huggingface.co https://*.hf.co https://*.huggingface.co https://voiceofml-search.hf.space https://hf-mirror.com; frame-src 'self' blob:;"
|
| 9 |
/>
|
| 10 |
<title>VoiceOfML Reader</title>
|
| 11 |
<script>
|
|
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
<meta
|
| 7 |
http-equiv="Content-Security-Policy"
|
| 8 |
+
content="default-src 'self'; base-uri 'self' blob:; script-src 'self' https://unpkg.com 'unsafe-inline' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline' blob:; img-src 'self' data: blob: https:; media-src 'self' https://voiceofml-search.hf.space https://hf-mirror.com https://huggingface.co https://*.hf.co https://*.huggingface.co; connect-src 'self' blob: https://unpkg.com https://huggingface.co https://*.hf.co https://*.huggingface.co https://voiceofml-search.hf.space https://hf-mirror.com; frame-src 'self' blob:;"
|
| 9 |
/>
|
| 10 |
<title>VoiceOfML Reader</title>
|
| 11 |
<script>
|
static/reader.js
CHANGED
|
@@ -18,6 +18,7 @@ const MARKED_URL = VoiceOfMLReaderResources.vendorUrl("marked", "/static/");
|
|
| 18 |
const PURIFY_URL = VoiceOfMLReaderResources.vendorUrl("purify", "/static/");
|
| 19 |
const JSZIP_URL = VoiceOfMLReaderResources.vendorUrl("jszip", "/static/");
|
| 20 |
const DOCX_PREVIEW_URL = VoiceOfMLReaderResources.vendorUrl("docx", "/static/");
|
|
|
|
| 21 |
const READER_PROXY_TIMEOUT_MS = 120000;
|
| 22 |
const readerRequestManager = VoiceOfMLReaderRequests.createReaderRequestManager();
|
| 23 |
const readerRuntime = VoiceOfMLReaderRuntime.createReaderRuntime();
|
|
@@ -555,6 +556,7 @@ let markerFrame = 0;
|
|
| 555 |
let pageNavigationLockUntil = 0;
|
| 556 |
let pendingBookmarkSnapshot = null;
|
| 557 |
let mediaElement = null;
|
|
|
|
| 558 |
function nextReaderGeneration(name) {
|
| 559 |
return readerRuntime.nextGeneration(name);
|
| 560 |
}
|
|
@@ -4541,6 +4543,10 @@ function renderImageDocument(image) {
|
|
| 4541 |
status.textContent = "图片";
|
| 4542 |
}
|
| 4543 |
function disposeFormatResources(mode) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4544 |
if (["pdf", "pdf-pages"].includes(mode)) {
|
| 4545 |
pdfDocument = null;
|
| 4546 |
pdfPageManifest = null;
|
|
@@ -4690,6 +4696,7 @@ function registerReaderFormatAdapters() {
|
|
| 4690 |
docx: [loadDocxDocument, renderDocx],
|
| 4691 |
audio: [loadMediaDocument, () => renderMedia("audio")],
|
| 4692 |
video: [loadMediaDocument, () => renderMedia("video")],
|
|
|
|
| 4693 |
foliate: [loadMediaDocument, renderFoliate]
|
| 4694 |
};
|
| 4695 |
for (const [mode, [open, render]] of Object.entries(formats))
|
|
@@ -4739,6 +4746,25 @@ function renderMedia(mode) {
|
|
| 4739 |
content.appendChild(media);
|
| 4740 |
status.textContent = mode === "audio" ? "音频" : "视频";
|
| 4741 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4742 |
// Resolve once, then keep source URLs, capability, and presentation in sync.
|
| 4743 |
async function resolveReaderSource() {
|
| 4744 |
if (!readerId || sourceUrl) return;
|
|
|
|
| 18 |
const PURIFY_URL = VoiceOfMLReaderResources.vendorUrl("purify", "/static/");
|
| 19 |
const JSZIP_URL = VoiceOfMLReaderResources.vendorUrl("jszip", "/static/");
|
| 20 |
const DOCX_PREVIEW_URL = VoiceOfMLReaderResources.vendorUrl("docx", "/static/");
|
| 21 |
+
const RUFFLE_URL = VoiceOfMLReaderResources.vendors.ruffle.url;
|
| 22 |
const READER_PROXY_TIMEOUT_MS = 120000;
|
| 23 |
const readerRequestManager = VoiceOfMLReaderRequests.createReaderRequestManager();
|
| 24 |
const readerRuntime = VoiceOfMLReaderRuntime.createReaderRuntime();
|
|
|
|
| 556 |
let pageNavigationLockUntil = 0;
|
| 557 |
let pendingBookmarkSnapshot = null;
|
| 558 |
let mediaElement = null;
|
| 559 |
+
let swfPlayer = null;
|
| 560 |
function nextReaderGeneration(name) {
|
| 561 |
return readerRuntime.nextGeneration(name);
|
| 562 |
}
|
|
|
|
| 4543 |
status.textContent = "图片";
|
| 4544 |
}
|
| 4545 |
function disposeFormatResources(mode) {
|
| 4546 |
+
if (mode === "swf") {
|
| 4547 |
+
swfPlayer?.remove();
|
| 4548 |
+
swfPlayer = null;
|
| 4549 |
+
}
|
| 4550 |
if (["pdf", "pdf-pages"].includes(mode)) {
|
| 4551 |
pdfDocument = null;
|
| 4552 |
pdfPageManifest = null;
|
|
|
|
| 4696 |
docx: [loadDocxDocument, renderDocx],
|
| 4697 |
audio: [loadMediaDocument, () => renderMedia("audio")],
|
| 4698 |
video: [loadMediaDocument, () => renderMedia("video")],
|
| 4699 |
+
swf: [loadSwfDocument, renderSwf],
|
| 4700 |
foliate: [loadMediaDocument, renderFoliate]
|
| 4701 |
};
|
| 4702 |
for (const [mode, [open, render]] of Object.entries(formats))
|
|
|
|
| 4746 |
content.appendChild(media);
|
| 4747 |
status.textContent = mode === "audio" ? "音频" : "视频";
|
| 4748 |
}
|
| 4749 |
+
async function loadSwfDocument() {
|
| 4750 |
+
await loadScript(RUFFLE_URL);
|
| 4751 |
+
const response = await fetchReaderResponse();
|
| 4752 |
+
return new Uint8Array(await response.arrayBuffer());
|
| 4753 |
+
}
|
| 4754 |
+
async function renderSwf(data) {
|
| 4755 |
+
assertReaderActive();
|
| 4756 |
+
const factory = window.RufflePlayer?.newest?.();
|
| 4757 |
+
if (!factory) throw Object.assign(new Error("Ruffle 未加载"), { code: "READER_SWF_ENGINE" });
|
| 4758 |
+
const player = factory.createPlayer();
|
| 4759 |
+
player.className = "reader-swf";
|
| 4760 |
+
player.setAttribute("aria-label", `${documentState.title} Flash 内容`);
|
| 4761 |
+
player.style.width = "100%";
|
| 4762 |
+
player.style.minHeight = "360px";
|
| 4763 |
+
content.appendChild(player);
|
| 4764 |
+
swfPlayer = player;
|
| 4765 |
+
await player.ruffle().load({ data });
|
| 4766 |
+
status.textContent = "Flash 内容";
|
| 4767 |
+
}
|
| 4768 |
// Resolve once, then keep source URLs, capability, and presentation in sync.
|
| 4769 |
async function resolveReaderSource() {
|
| 4770 |
if (!readerId || sourceUrl) return;
|
tests/test_reader_backend.py
CHANGED
|
@@ -167,6 +167,10 @@ class ReaderArchiveValidationTests(unittest.TestCase):
|
|
| 167 |
"s": 2, "m": "p", "p": OBJECT + "/1234567890abcdef/page-manifest.json",
|
| 168 |
"b": "vomebook/pdf-pages",
|
| 169 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
decoded = app.decode_reader_assets(gzip.compress(json.dumps({"v": 1, "f": files}).encode()))["f"]
|
| 171 |
self.assertEqual(set(decoded), set(files))
|
| 172 |
self.assertEqual(decoded["ebook-chapters"]["cb"], "vomebook/pdf-pages")
|
|
@@ -175,6 +179,13 @@ class ReaderArchiveValidationTests(unittest.TestCase):
|
|
| 175 |
"https://huggingface.co/buckets/vomebook/pdf-pages/resolve/"
|
| 176 |
+ OBJECT + "/1234567890abcdef/page-manifest.json",
|
| 177 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
for entry in decoded.values():
|
| 179 |
if entry.get("b"):
|
| 180 |
self.assertIsNotNone(app.READER_BUCKET_PATH_RE.fullmatch(entry["p"]))
|
|
@@ -217,6 +228,13 @@ class ReaderBackendTests(unittest.IsolatedAsyncioTestCase):
|
|
| 217 |
app.app.state.http_session = session
|
| 218 |
return session
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
async def content(self, upstream, method="GET"):
|
| 221 |
self.session(upstream)
|
| 222 |
return await app.api_reader_content(SimpleNamespace(method=method, headers={}), SOURCE, None)
|
|
|
|
| 167 |
"s": 2, "m": "p", "p": OBJECT + "/1234567890abcdef/page-manifest.json",
|
| 168 |
"b": "vomebook/pdf-pages",
|
| 169 |
}
|
| 170 |
+
files["native-audio-bucket"] = {
|
| 171 |
+
"s": 2, "m": "a", "p": OBJECT + "/native-media-cdn-v1/audio.flac",
|
| 172 |
+
"b": "vomebook/pdf-pages",
|
| 173 |
+
}
|
| 174 |
decoded = app.decode_reader_assets(gzip.compress(json.dumps({"v": 1, "f": files}).encode()))["f"]
|
| 175 |
self.assertEqual(set(decoded), set(files))
|
| 176 |
self.assertEqual(decoded["ebook-chapters"]["cb"], "vomebook/pdf-pages")
|
|
|
|
| 179 |
"https://huggingface.co/buckets/vomebook/pdf-pages/resolve/"
|
| 180 |
+ OBJECT + "/1234567890abcdef/page-manifest.json",
|
| 181 |
)
|
| 182 |
+
self.assertEqual(
|
| 183 |
+
app.reader_asset_url(decoded["native-audio-bucket"]),
|
| 184 |
+
"https://huggingface.co/buckets/vomebook/pdf-pages/resolve/"
|
| 185 |
+
+ OBJECT + "/native-media-cdn-v1/audio.flac",
|
| 186 |
+
)
|
| 187 |
+
self.assertEqual(app.READER_RESOURCE_TYPES["flac"], "audio/flac")
|
| 188 |
+
self.assertEqual(app.reader_extension_for_asset(decoded["native-audio-bucket"], "flac"), "audio")
|
| 189 |
for entry in decoded.values():
|
| 190 |
if entry.get("b"):
|
| 191 |
self.assertIsNotNone(app.READER_BUCKET_PATH_RE.fullmatch(entry["p"]))
|
|
|
|
| 228 |
app.app.state.http_session = session
|
| 229 |
return session
|
| 230 |
|
| 231 |
+
async def test_bucket_audio_resource_uses_audio_mime_type(self):
|
| 232 |
+
path = OBJECT + "/native-media-cdn-v1/audio.flac"
|
| 233 |
+
with patch.object(app, "get_reader_bucket_resource", AsyncMock(return_value=b"flac")):
|
| 234 |
+
response = await app.api_reader_bucket_resource(path)
|
| 235 |
+
self.assertEqual(response.media_type, "audio/flac")
|
| 236 |
+
self.assertEqual(response.body, b"flac")
|
| 237 |
+
|
| 238 |
async def content(self, upstream, method="GET"):
|
| 239 |
self.session(upstream)
|
| 240 |
return await app.api_reader_content(SimpleNamespace(method=method, headers={}), SOURCE, None)
|
tests/test_reader_refactor.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
"""Local Reader behavior regressions; all document/network fixtures stay offline."""
|
| 2 |
|
| 3 |
import io
|
|
|
|
| 4 |
import gzip
|
| 5 |
import hashlib
|
| 6 |
import json
|
|
@@ -124,6 +125,31 @@ class ReaderRefactorTest(unittest.TestCase):
|
|
| 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_saved_native_pdf_opens_target_page_first(self):
|
| 128 |
store = support.STORE_SCRIPT.replace(
|
| 129 |
"get: () => new Promise((resolve) => setTimeout(() => resolve(null), 300)),",
|
|
|
|
| 1 |
"""Local Reader behavior regressions; all document/network fixtures stay offline."""
|
| 2 |
|
| 3 |
import io
|
| 4 |
+
import base64
|
| 5 |
import gzip
|
| 6 |
import hashlib
|
| 7 |
import json
|
|
|
|
| 125 |
self.assertTrue(image.evaluate("image => image.complete && image.naturalWidth > 0"))
|
| 126 |
self.assertTrue(self.page.locator("#full-search-toggle").evaluate("node => node.hidden"))
|
| 127 |
|
| 128 |
+
def test_native_spreadsheet_html_keeps_cells_selectable_and_zoomable(self):
|
| 129 |
+
chart = support.IMAGE_FIXTURES["webp"][1]
|
| 130 |
+
chart_data = "data:image/webp;base64," + base64.b64encode(chart).decode()
|
| 131 |
+
document = (
|
| 132 |
+
'<!doctype html><html><head><meta charset="utf-8"><style>'
|
| 133 |
+
'body{width:max-content;min-width:100%;margin:0;padding:12px}'
|
| 134 |
+
'.sheet{width:max-content;min-width:100%}table{border-collapse:collapse}'
|
| 135 |
+
'td{min-width:900px;height:80px;border:1px solid #444}'
|
| 136 |
+
'</style></head><body><section class="sheet"><h2>工作表 1</h2>'
|
| 137 |
+
f'<table><tr><td id="cell">完整单元格文字<img src="{chart_data}"></td></tr></table>'
|
| 138 |
+
'</section></body></html>'
|
| 139 |
+
)
|
| 140 |
+
self.serve(document, "text/html")
|
| 141 |
+
self.open(self.reader_url("html", "table"))
|
| 142 |
+
frame_element = self.page.locator("iframe.html-frame")
|
| 143 |
+
frame_element.wait_for()
|
| 144 |
+
frame = frame_element.element_handle().content_frame()
|
| 145 |
+
self.assertEqual(frame.locator("#cell").text_content().strip(), "完整单元格文字")
|
| 146 |
+
self.assertGreater(frame.locator("img").evaluate("image => image.naturalWidth"), 0)
|
| 147 |
+
before = frame.locator("table").evaluate("node => node.getBoundingClientRect().width")
|
| 148 |
+
self.page.locator("#zoom-in").click(click_count=5)
|
| 149 |
+
self.assertGreater(
|
| 150 |
+
frame.locator("table").evaluate("node => node.getBoundingClientRect().width"), before,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
def test_saved_native_pdf_opens_target_page_first(self):
|
| 154 |
store = support.STORE_SCRIPT.replace(
|
| 155 |
"get: () => new Promise((resolve) => setTimeout(() => resolve(null), 300)),",
|
tests/test_reader_regression.js
CHANGED
|
@@ -58,6 +58,18 @@ assert.strictEqual(assets.assetFields({s: 2, m: 'p', p: bucket}, '/api/reader-bu
|
|
| 58 |
'/api/reader-bucket-resource?path=' + encodeURIComponent(bucket))
|
| 59 |
assert.strictEqual(assets.assetFields({s: 2, m: 'p', p: bucket, b: 'vomebook/pdf-pages'}, '/api/reader-bucket-resource').ReaderLink,
|
| 60 |
'/api/reader-bucket-resource?path=' + encodeURIComponent(bucket))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
const optimized = assetRoot + 'pdf-range-v1-document/document.pdf'
|
| 62 |
assert.strictEqual(assets.assetFields({s: 2, m: 'p', p: optimized, b: 'vomebook/pdf-optimized'}), null)
|
| 63 |
assert.strictEqual(assets.isAssetSourcePath(sourcePrefix + 'pdf_manifest.json'), true)
|
|
|
|
| 58 |
'/api/reader-bucket-resource?path=' + encodeURIComponent(bucket))
|
| 59 |
assert.strictEqual(assets.assetFields({s: 2, m: 'p', p: bucket, b: 'vomebook/pdf-pages'}, '/api/reader-bucket-resource').ReaderLink,
|
| 60 |
'/api/reader-bucket-resource?path=' + encodeURIComponent(bucket))
|
| 61 |
+
const nativeMediaPath = assetRoot + 'native-media-cdn-v1/audio.flac'
|
| 62 |
+
const nativeMedia = assets.assetFields({s: 2, m: 'a', p: nativeMediaPath, b: 'vomebook/pdf-pages'}, '/api/reader-bucket-resource')
|
| 63 |
+
assert.strictEqual(nativeMedia.ReaderExtension, 'audio')
|
| 64 |
+
assert.strictEqual(nativeMedia.ReaderLink,
|
| 65 |
+
'https://huggingface.co/buckets/vomebook/pdf-pages/resolve/' + nativeMediaPath)
|
| 66 |
+
assert.strictEqual(assets.isAssetSourcePath(sourcePrefix + nativeMediaPath), true)
|
| 67 |
+
const swfPath = assetRoot + 'native-swf-ruffle-v1/document.swf'
|
| 68 |
+
const swf = assets.assetFields({s: 2, m: 'f', p: swfPath, b: 'vomebook/pdf-pages'}, '/api/reader-bucket-resource')
|
| 69 |
+
assert.strictEqual(swf.ReaderExtension, 'swf')
|
| 70 |
+
assert.strictEqual(assets.capability('swf').mode, 'swf')
|
| 71 |
+
assert.strictEqual(swf.ReaderLink,
|
| 72 |
+
'https://huggingface.co/buckets/vomebook/pdf-pages/resolve/' + swfPath)
|
| 73 |
const optimized = assetRoot + 'pdf-range-v1-document/document.pdf'
|
| 74 |
assert.strictEqual(assets.assetFields({s: 2, m: 'p', p: optimized, b: 'vomebook/pdf-optimized'}), null)
|
| 75 |
assert.strictEqual(assets.isAssetSourcePath(sourcePrefix + 'pdf_manifest.json'), true)
|
upstream_http.py
CHANGED
|
@@ -9,7 +9,7 @@ ALLOWED_DOWNLOAD_HOSTS = {"huggingface.co", "hf-mirror.com", "hf.co"}
|
|
| 9 |
ALLOWED_DOWNLOAD_HOST_SUFFIXES = (".hf.co", ".huggingface.co", ".xethub.hf.co")
|
| 10 |
MAX_REDIRECTS = 5
|
| 11 |
READER_ASSET_PATH_RE = re.compile(
|
| 12 |
-
r"objects/[0-9a-f]{2}/[0-9a-f]{64}/(?:[0-9a-f]{16}/)?(?:page-manifest\.json|ocr-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl)|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz)|(?:[a-z0-9-]+/)?(?:linearized\.pdf|chapter-manifest\.json|document\.(?:pdf|epub|mobi|azw|azw3|fb2|docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp)|book\.epub|audio\.mp3|video\.mp4|epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/[A-Za-z0-9._~%+\-/]+|epub-search-index\.json\.gz)))"
|
| 13 |
)
|
| 14 |
READER_ASSET_SOURCE_RE = re.compile(
|
| 15 |
r"/datasets/vomebook/Reader-Assets/resolve/main/(?:pdf_manifest\.json|" + READER_ASSET_PATH_RE.pattern + r")"
|
|
|
|
| 9 |
ALLOWED_DOWNLOAD_HOST_SUFFIXES = (".hf.co", ".huggingface.co", ".xethub.hf.co")
|
| 10 |
MAX_REDIRECTS = 5
|
| 11 |
READER_ASSET_PATH_RE = re.compile(
|
| 12 |
+
r"objects/[0-9a-f]{2}/[0-9a-f]{64}/(?:[0-9a-f]{16}/)?(?:page-manifest\.json|ocr-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl)|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz)|(?:[a-z0-9-]+/)?(?:linearized\.pdf|chapter-manifest\.json|document\.(?:pdf|epub|mobi|azw|azw3|fb2|docx|html|txt|md|webp|jpg|jpeg|png|gif|bmp|swf)|book\.epub|audio\.(?:mp3|wav|m4a|flac|mpga)|video\.(?:mp4|mov)|epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/[A-Za-z0-9._~%+\-/]+|epub-search-index\.json\.gz)))"
|
| 13 |
)
|
| 14 |
READER_ASSET_SOURCE_RE = re.compile(
|
| 15 |
r"/datasets/vomebook/Reader-Assets/resolve/main/(?:pdf_manifest\.json|" + READER_ASSET_PATH_RE.pattern + r")"
|