支持自部署需求管理、交付流程与飞书集成

This commit is contained in:
xyt committed 2026-11-25 12:40:33 +08:00
1 parent 4556a5b483
commit e22b66d9dd
148 files changed
+8300 -6393

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import { assertSameOrigin, jsonResponse, requireApiUser } from '@/lib/server/auth';
import { deleteStagedAttachment, downloadAttachment, attachmentFailure } from '@/lib/server/attachments';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
type Context = { params: Promise<{ id: string }> };
export async function GET(_request: Request, { params }: Context) {
try {
const user = await requireApiUser();
const { id } = await params;
const { file, bytes } = downloadAttachment(id, user);
const filename = encodeURIComponent(file.name).replace(/[!'()*]/g, char => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
return new Response(new Uint8Array(bytes), { headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="attachment"; filename*=UTF-8''${filename}`,
'Content-Length': String(file.size),
'Cache-Control': 'private, no-store',
'X-Content-Type-Options': 'nosniff',
'Content-Security-Policy': "sandbox; default-src 'none'",
} });
} catch (error) { return attachmentFailure(error); }
}
export async function DELETE(request: Request, { params }: Context) {
try {
const user = await requireApiUser();
assertSameOrigin(request);
deleteStagedAttachment((await params).id, user.userId);
return jsonResponse({ deleted: true });
} catch (error) { return attachmentFailure(error); }
}
+41
View File
@@ -0,0 +1,41 @@
import { MAX_ATTACHMENT_BYTES } from '@/lib/attachments';
import { AuthError, assertSameOrigin, jsonResponse, requireApiUser } from '@/lib/server/auth';
import { storeUpload, listStagedAttachments, attachmentFailure } from '@/lib/server/attachments';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
export async function POST(request: Request) {
try {
const user = await requireApiUser();
assertSameOrigin(request);
const contentType = request.headers.get('content-type') || '';
if (!contentType.toLowerCase().startsWith('multipart/form-data;')) throw new AuthError('请选择文件后上传。', 415);
const limit = MAX_ATTACHMENT_BYTES + 65536;
if (Number(request.headers.get('content-length')) > limit) throw new AuthError('单个文件不能超过 20 MB。', 413);
const reader = request.body?.getReader();
if (!reader) throw new AuthError('没有收到文件,请重新选择。');
const chunks: Uint8Array[] = [];
let size = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
size += value.byteLength;
if (size > limit) { await reader.cancel(); throw new AuthError('单个文件不能超过 20 MB。', 413); }
chunks.push(value);
}
let form: FormData;
try { form = await new Response(new Uint8Array(Buffer.concat(chunks)), { headers: { 'Content-Type': contentType } }).formData(); }
catch { throw new AuthError('上传内容不完整,请重新选择文件上传。'); }
const files = form.getAll('file');
if (files.length !== 1 || !(files[0] instanceof File)) throw new AuthError('每次上传一个文件。');
const file = files[0];
if (file.size > MAX_ATTACHMENT_BYTES) throw new AuthError('单个文件不能超过 20 MB。', 413);
return jsonResponse({ attachment: storeUpload(user.userId, file.name, new Uint8Array(await file.arrayBuffer())) }, 201);
} catch (error) { return attachmentFailure(error); }
}
export async function GET() {
try { return jsonResponse({ attachments: listStagedAttachments((await requireApiUser()).userId) }); }
catch (error) { return attachmentFailure(error); }
}