feat(frontend): POST for images

This commit is contained in:
h
2026-01-21 10:20:17 +01:00
parent 5441454993
commit 7e3d80b832
3 changed files with 44 additions and 7 deletions

12
frontend/src/app.d.ts vendored
View File

@@ -1,12 +1,10 @@
// See https://svelte.dev/docs/kit/types#app.d.ts
// for information about these interfaces
import type { ConvexHttpClient } from 'convex/browser';
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface PageState {}
// interface Platform {}
interface Locals {
convex: ConvexHttpClient;
}
}
}

View File

@@ -0,0 +1,8 @@
import { ConvexHttpClient } from 'convex/browser';
import { PUBLIC_CONVEX_URL } from '$env/static/public';
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
event.locals.convex = new ConvexHttpClient(PUBLIC_CONVEX_URL);
return resolve(event);
};

View File

@@ -0,0 +1,31 @@
import { api } from '$lib/convex/_generated/api';
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const POST: RequestHandler = async ({ params, request, locals }) => {
const mnemonic = params.mnemonic;
const chatData = await locals.convex.query(api.chats.getByMnemonic, { mnemonic });
if (!chatData) {
throw error(404, 'Chat not found');
}
const contentType = request.headers.get('content-type') || 'image/jpeg';
const buffer = await request.arrayBuffer();
const base64 = Buffer.from(buffer).toString('base64');
const caption = request.headers.get('x-caption') || '';
await locals.convex.mutation(api.messages.create, {
chatId: chatData._id,
role: 'user',
content: caption,
source: 'web',
imageBase64: base64,
imageMediaType: contentType
});
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json' }
});
};