This commit is contained in:
2026-02-16 19:34:35 +00:00
commit 9ab0940ca4
784 changed files with 41710 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import { createRouter } from '../../create-router';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { getTmpAttachmentsDir, getAttachmentsDir } from '@@/data-path';
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5 MB
export const uploadRouter = createRouter();
uploadRouter.post('/', async (ctx) => {
const user = ctx.get('user');
const body = ctx.get('body') as Record<string, unknown>;
const file = body.file as File | null;
const sessionId = (body.sessionId as string) || null;
const provider = (body.provider as 'claude' | 'opencode') || null;
if (!file || !(file instanceof File)) {
return ctx.json({ error: 'file is required' }, 400);
}
if (!file.type.startsWith('image/')) {
return ctx.json({ error: 'Only image files are allowed' }, 400);
}
if (file.size > MAX_FILE_SIZE) {
return ctx.json({ error: 'File exceeds 5 MB limit' }, 400);
}
try {
const buffer = Buffer.from(await file.arrayBuffer());
const ext = file.name.split('.').pop() || 'png';
const filename = `${crypto.randomUUID()}.${ext}`;
let saveDir: string;
if (sessionId && provider) {
saveDir = getAttachmentsDir(user.email, provider, sessionId);
} else {
saveDir = getTmpAttachmentsDir(user.email);
}
await mkdir(saveDir, { recursive: true });
await Bun.write(join(saveDir, filename), buffer);
const base64 = buffer.toString('base64');
const dataUrl = `data:${file.type};base64,${base64}`;
return ctx.json({ filename: file.name, dataUrl, attachmentId: filename });
} catch (err) {
const message = err instanceof Error ? err.message : 'Upload failed';
return ctx.json({ error: message }, 500);
}
});