Skip to content

Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演

修复完整备份中的 RTF 文档恢复

MigenAId56d2e23741f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c361ec..f770e01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog
+## 1.18.8
+
+- 修复完整资料夹备份中的 RTF 文档恢复:旧备份即使以 `.bin` 保存,也会通过真实文件签名确认后安全恢复。
+- 新备份使用 `.rtf` 扩展名,并在浏览器未提供文件类型时沿用已保存的文档类型。
+- 损坏或伪装成 RTF 的文件仍会在写入资料库前被拒绝。
+
## 1.18.7
- 锁定 Chrome Web Store 正式扩展身份;正式打包先用项目公钥校验目标 ID,再从商店上传清单中移除仅供本地开发使用的 `key` 字段。
diff --git a/library-package.js b/library-package.js
index f1dfe9e..e12f139 100644
--- a/library-package.js
+++ b/library-package.js
@@ -13,6 +13,23 @@ import { isFixedTagTree, migrateLegacyFacetState } from "./tag-taxonomy.js";
import { migrateLibraryState } from "./migration.js";
import { LIBRARY_PACKAGE_FORMAT, isSupportedLibraryPackageVersion } from "./library-package-format.js";
import { remapArticleDocumentAssets } from "./article-document.js";
+import { boundedMediaBlobFromResponse, isSupportedDocumentMimeType } from "./bounded-media.js";
+
+export async function parseCompleteFolderBackup(value, files = new Map(), limitsValue = {}) {
+ const preparedFiles = new Map(files);
+ const limits = portableLibraryLimits(limitsValue);
+ for (const [path, mimeType] of completeBackupDocumentPaths(value)) {
+ const blob = preparedFiles.get(path);
+ if (!(blob instanceof Blob)) continue;
+ const verified = await boundedMediaBlobFromResponse(new Response(blob), {
+ kind: "document",
+ expectedMimeType: mimeType,
+ maxBytes: limits.maxFileBytes
+ });
+ preparedFiles.set(path, verified);
+ }
+ return parseLibraryPackage(value, preparedFiles, limitsValue);
+}
export function parseLibraryPackage(value, files = new Map(), limitsValue = {}) {
const limits = portableLibraryLimits(limitsValue);
@@ -625,6 +642,7 @@ function mediaType(path, kind) {
if (/\.mkv$/i.test(path)) return "video/x-matroska";
if (/\.avi$/i.test(path)) return "video/x-msvideo";
if (/\.pdf$/i.test(path)) return "application/pdf";
+ if (/\.rtf$/i.test(path)) return "application/rtf";
if (/\.html?$/i.test(path)) return "text/html";
if (/\.md$/i.test(path)) return "text/markdown";
return "text/plain";
@@ -634,7 +652,7 @@ function validMediaPath(path, kind) {
if (!path || path.includes("..")) return false;
if (kind === "image") return /^images\/[A-Za-z0-9._/-]+\.(?:png|jpe?g|webp)$/i.test(path);
if (kind === "video") return /^videos\/[A-Za-z0-9._/-]+\.(?:mp4|webm|mov|mkv|avi|video)$/i.test(path);
- return /^documents\/[A-Za-z0-9._/-]+\.(?:pdf|md|txt|html?|bin)$/i.test(path);
+ return /^documents\/[A-Za-z0-9._/-]+\.(?:pdf|rtf|md|txt|html?|bin)$/i.test(path);
}
function validCreativeResultPath(path, kind) {
@@ -646,7 +664,32 @@ function validCreativeResultPath(path, kind) {
function blobMatchesKind(blob, kind) {
if (kind === "image") return blob.type.startsWith("image/");
if (kind === "video") return blob.type.startsWith("video/");
- return ["application/pdf", "text/plain", "text/markdown", "text/html"].includes(blob.type);
+ return isSupportedDocumentMimeType(blob.type);
+}
+
+function completeBackupDocumentPaths(value = {}) {
+ const paths = new Map();
+ for (const entry of Array.isArray(value?.entries) ? value.entries : []) {
+ const mediaAssets = Array.isArray(entry?.mediaAssets)
+ ? entry.mediaAssets
+ : Array.isArray(entry?.visuals) ? entry.visuals : [];
+ for (const asset of mediaAssets) {
+ if (asset?.kind === "document" && asset.storageMode !== "reference" && asset.assetPath) {
+ paths.set(String(asset.assetPath), String(asset.mimeType ?? ""));
+ }
+ }
+ }
+ for (const session of Array.isArray(value?.composerSessions) ? value.composerSessions : []) {
+ for (const reference of Array.isArray(session?.referenceSnapshots) ? session.referenceSnapshots : []) {
+ if (reference?.sourceType !== "temporary") continue;
+ for (const asset of Array.isArray(reference.assetRefs) ? reference.assetRefs : []) {
+ if (asset?.kind === "document" && asset.archivePath) {
+ paths.set(String(asset.archivePath), String(asset.mimeType ?? ""));
+ }
+ }
+ }
+ }
+ return paths;
}
function uniqueId(prefix, used) {
diff --git a/library.js b/library.js
index faf8862..e1cbaac 100644
--- a/library.js
+++ b/library.js
@@ -11,7 +11,7 @@ import {
saveDerivedMedia,
saveMediaBlob
} from "./media-store.js";
-import { parseLibraryPackage } from "./library-package.js";
+import { parseCompleteFolderBackup, parseLibraryPackage } from "./library-package.js";
import { parseCreativeExperimentPackage } from "./creative-experiment-package.js";
import { readZipBlob } from "./zip.js";
import {
@@ -2075,7 +2075,7 @@ async function createCompleteFolderBackup() {
}
const blob = await getMediaBlob(asset.id);
if (!blob) throw new Error(`“${entry.title || "未命名案例"}”的媒体缺失,完整备份已停止`);
- const assetPath = folderAssetPath(entry.id, asset, blob.type);
+ const assetPath = folderAssetPath(entry.id, asset, blob.type || asset.mimeType);
showDataSafetyFeedback(`正在写入第 ${mediaCount + 1} 项媒体 · ${formatBytes(byteSize + blob.size)}`);
await writeDirectoryFile(directory, assetPath, blob);
mediaAssets.push({
@@ -2185,7 +2185,7 @@ async function restoreCompleteFolderBackup() {
if (completion.mediaCount !== backupPaths.size || completion.byteSize !== totalBytes) {
throw new Error("完整备份的媒体数量或大小校验失败,未写入资料库");
}
- const parsed = parseLibraryPackage(library, files, { maxFileBytes: Number.MAX_SAFE_INTEGER });
+ const parsed = await parseCompleteFolderBackup(library, files, { maxFileBytes: Number.MAX_SAFE_INTEGER });
await validateImportedImageDimensions(parsed.images);
const restoredLibrary = { ...parsed };
delete restoredLibrary.assets;
@@ -2377,6 +2377,7 @@ function folderExtension(mimeType, kind) {
const known = {
"image/png": "png", "image/jpeg": "jpg", "image/webp": "webp", "video/mp4": "mp4", "video/webm": "webm",
"video/quicktime": "mov", "video/x-matroska": "mkv", "video/x-msvideo": "avi", "application/pdf": "pdf",
+ "application/rtf": "rtf", "text/rtf": "rtf", "application/x-rtf": "rtf",
"text/plain": "txt", "text/markdown": "md", "text/html": "html"
};
return known[mimeType] || (kind === "video" ? "video" : kind === "document" ? "bin" : "webp");
diff --git a/manifest.json b/manifest.json
index c1a1aef..bac0a3e 100644
--- a/manifest.json
+++ b/manifest.json
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "__MSG_extensionName__",
- "version": "1.18.7",
+ "version": "1.18.8",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq2m8W9ACXJTkJqPAd36sPrtzonmHhOYU34uS7c+D7DXX6ggWNMwB4xZapGFJ4ScBQJv71PeG9KfsEnhcXFZ9RoX/X3wY+57evL19hf6woNX1xxhKOe1rtx5QmzWCEYIr99TzW50k/N9U6tS9Ri+2DHKYA85ssXTC8B2yRQ5zUXhsAWZe/HDU18UFuvUKXYt3ZdIUi31c2vIlgFFBfsyKbIUMny9LlxL/smb4vJzU/+A3LwJDjjvOtS/lmQ2vHoQGjHJItO18bYI6h67fIEvmWk+HJKtO08Ca+5RLS8TYhI+K7l7S0RyDVNujyVWtvhAULot1H0BvteYL87fKu3SNywIDAQAB",
"homepage_url": "https://github.com/wchao6891/PromptDirector",
"default_locale": "zh_CN",
diff --git a/package-lock.json b/package-lock.json
index 85c53f5..5e0ed63 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "prompt-case-collector",
- "version": "1.18.7",
+ "version": "1.18.8",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "prompt-case-collector",
- "version": "1.18.7",
+ "version": "1.18.8",
"license": "Apache-2.0",
"dependencies": {
"@jonahschulte/rtf-toolkit": "0.5.0",
diff --git a/package.json b/package.json
index cd98d4e..f698565 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "prompt-case-collector",
- "version": "1.18.7",
+ "version": "1.18.8",
"private": false,
"license": "Apache-2.0",
"repository": "https://github.com/wchao6891/PromptDirector",
diff --git a/test/library-package.test.js b/test/library-package.test.js
index 7a69d1b..fbea942 100644
--- a/test/library-package.test.js
+++ b/test/library-package.test.js
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
import {
mergeLibraryPackage,
+ parseCompleteFolderBackup,
parseLibraryPackage,
selectLibraryPackage,
selectProjectPackage
@@ -144,6 +145,54 @@ test("v3 packages restore video documents and time notes with their media relati
])), /大小校验失败/);
});
+test("complete folder backups restore genuine legacy RTF documents without trusting a .bin MIME type", async () => {
+ const rtf = String.raw`{\rtf1\ansi PromptDirector backup}`;
+ const path = "documents/rtf-case/rtf-asset.bin";
+ const data = packageData([], catalog());
+ data.entries = [{
+ ...entry("rtf-case"), hasScreenshot: undefined, screenshotPath: undefined,
+ mediaAssets: [{
+ id: "rtf-asset", kind: "document", storageMode: "managed", mimeType: "application/rtf",
+ byteSize: new Blob([rtf]).size, assetPath: path
+ }],
+ primaryMediaId: "rtf-asset"
+ }];
+
+ const parsed = await parseCompleteFolderBackup(data, new Map([[
+ path,
+ new Blob([rtf], { type: "application/octet-stream" })
+ ]]));
+ assert.equal(parsed.assets.get("rtf-asset").type, "application/rtf");
+
+ await assert.rejects(
+ () => parseCompleteFolderBackup(data, new Map([[
+ path,
+ new Blob(["not an RTF document"], { type: "application/octet-stream" })
+ ]])),
+ /来源没有返回有效文档文件/
+ );
+});
+
+test("portable packages accept the explicit .rtf path used by new complete backups", () => {
+ const rtf = String.raw`{\rtf1\ansi PromptDirector backup}`;
+ const path = "documents/rtf-case/rtf-asset.rtf";
+ const data = packageData([], catalog());
+ data.entries = [{
+ ...entry("rtf-case"), hasScreenshot: undefined, screenshotPath: undefined,
+ mediaAssets: [{
+ id: "rtf-asset", kind: "document", storageMode: "managed", mimeType: "application/rtf",
+ byteSize: new Blob([rtf]).size, assetPath: path
+ }],
+ primaryMediaId: "rtf-asset"
+ }];
+
+ const parsed = parseLibraryPackage(data, new Map([[
+ path,
+ new Blob([rtf], { type: "application/rtf" })
+ ]]));
+ assert.equal(parsed.assets.get("rtf-asset").type, "application/rtf");
+});
+
test("metadata-only import preview never compares declared bytes to its 11-byte placeholder", () => {
const data = packageData([], catalog());
data.entries = [{
diff --git a/test/media-ui.test.js b/test/media-ui.test.js
index 7b2b5d6..aeb0786 100644
--- a/test/media-ui.test.js
+++ b/test/media-ui.test.js
@@ -120,12 +120,14 @@ test("complete folder backup writes a completion marker after library metadata",
assert.ok(backup.indexOf('"library.json"') < backup.indexOf('"complete.json"'));
assert.match(backup, /await writeDirectoryFile\(directory, assetPath, blob\)/);
assert.match(backup, /byteSize:\s*blob\.size/);
+ assert.match(backup, /folderAssetPath\(entry\.id, asset, blob\.type \|\| asset\.mimeType\)/);
+ assert.match(source, /"application\/rtf":\s*"rtf"/);
});
test("complete folder restore validates aggregate and real per-media sizes before preview", () => {
const restore = source.slice(source.indexOf("async function restoreCompleteFolderBackup"), source.indexOf("function backupMediaPaths"));
const aggregateValidation = restore.indexOf("completion.mediaCount !== backupPaths.size");
- const packageParsing = restore.indexOf("parseLibraryPackage(library, files");
+ const packageParsing = restore.indexOf("parseCompleteFolderBackup(library, files");
assert.ok(aggregateValidation >= 0 && aggregateValidation < packageParsing);
assert.match(restore, /PREVIEW_LIBRARY_IMPORT[^}]*library:\s*restoredLibrary/);
assert.match(restore, /APPLY_LIBRARY_IMPORT[^}]*library:\s*restoredLibrary/);