refactor: 收口 onlyoffice callback 本地类型

- 去掉 callback 写回链中的宽泛 any 访问,改为最小资产/权限类型

- 复用 documentId/workspaceId/mimeType 局部变量,保持回调写回逻辑更稳定可读
This commit is contained in:
lix-2026
2026-04-15 03:35:49 +08:00
parent f1da2abda4
commit 822c730cd6
@@ -22,6 +22,16 @@ type OnlyOfficeCallbackBody = {
key?: string; key?: string;
}; };
type OnlyOfficeAssetRecord = {
document_id?: string | null;
workspace_id?: string | null;
mime_type?: string | null;
};
type OnlyOfficePermission = {
permission?: string | null;
};
const normalizeSecret = (raw: string) => { const normalizeSecret = (raw: string) => {
const trimmed = String(raw || "").trim(); const trimmed = String(raw || "").trim();
if (!trimmed) return ""; if (!trimmed) return "";
@@ -98,18 +108,23 @@ export async function POST(request: Request) {
"dev-user"; "dev-user";
const client = getConvexHttpClient(); const client = getConvexHttpClient();
const asset = await client.query(api.mediaAssets.getById, { userId, id: assetId }); const asset = (await client.query(api.mediaAssets.getById, { userId, id: assetId })) as
| OnlyOfficeAssetRecord
| null;
if (!asset) { if (!asset) {
return NextResponse.json({ error: 1 }); return NextResponse.json({ error: 1 });
} }
const documentId = String(asset.document_id || "").trim();
const workspaceId = String(asset.workspace_id || "").trim() || null;
const mimeType = String(asset.mime_type || "").trim() || "application/octet-stream";
// 只读权限:不允许通过 ONLYOFFICE 回调写回 // 只读权限:不允许通过 ONLYOFFICE 回调写回
try { try {
const perm = await client.query(api.documents.getPermissionForUser, { const perm = (await client.query(api.documents.getPermissionForUser, {
userId, userId,
id: String((asset as any).document_id || ""), id: documentId,
}); })) as OnlyOfficePermission | null;
if (!perm || (perm as any).permission !== "edit") { if (!perm || perm.permission !== "edit") {
return NextResponse.json({ error: 1 }); return NextResponse.json({ error: 1 });
} }
} catch { } catch {
@@ -131,7 +146,7 @@ export async function POST(request: Request) {
const uploadRes = await fetch(uploadUrl, { const uploadRes = await fetch(uploadUrl, {
method: "POST", method: "POST",
headers: { "Content-Type": asset.mime_type || "application/octet-stream" }, headers: { "Content-Type": mimeType },
body: buf, body: buf,
}); });
@@ -147,7 +162,7 @@ export async function POST(request: Request) {
const bridgeContext = buildDocumentBridgeContextWithActor({ const bridgeContext = buildDocumentBridgeContextWithActor({
request, request,
workspaceId: String((asset as any).workspace_id || "").trim() || null, workspaceId,
actor: { actor: {
actorType: "service", actorType: "service",
actorId: userId, actorId: userId,
@@ -163,15 +178,15 @@ export async function POST(request: Request) {
name: "media.assets.replace_storage", name: "media.assets.replace_storage",
payload: { payload: {
assetId, assetId,
documentId: String((asset as any).document_id || "").trim(), documentId,
workspaceId: String((asset as any).workspace_id || "").trim() || null, workspaceId,
storageId, storageId,
userId, userId,
} satisfies MediaAssetReplaceStoragePayload, } satisfies MediaAssetReplaceStoragePayload,
context: bridgeContext, context: bridgeContext,
target: { target: {
workspaceId: String((asset as any).workspace_id || "").trim() || null, workspaceId,
pageId: String((asset as any).document_id || "").trim() || null, pageId: documentId || null,
}, },
reason: "ONLYOFFICE 回调写回附件存储", reason: "ONLYOFFICE 回调写回附件存储",
refs: [`onlyoffice:asset:${assetId}`], refs: [`onlyoffice:asset:${assetId}`],