feat(api): route all image uploads through mana-media for CAS, thumbnails & Photos gallery

Picture, Contacts, Planta, Storage, and NutriPhi image uploads now go
through mana-media instead of directly to S3. This enables SHA-256
deduplication, automatic thumbnail generation, EXIF extraction, and
makes all images visible in the Photos gallery. Non-image files (PDFs,
audio, docs) continue to use shared-storage directly. SVG avatars in
Contacts also stay on shared-storage since Sharp can't process SVGs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-04-04 10:38:30 +02:00
parent 46dae20fa3
commit 502813f49c
10 changed files with 238 additions and 213 deletions

View file

@ -25,11 +25,15 @@ const routes = new Hono();
// ─── Photo Analysis (server-only: Gemini Vision) ────────────
routes.post('/analysis/photo', async (c) => {
const userId = c.get('userId');
const { imageBase64, mimeType } = await c.req.json();
if (!imageBase64) return c.json({ error: 'imageBase64 required' }, 400);
const mime = mimeType || 'image/jpeg';
try {
const res = await fetch(`${LLM_URL}/api/v1/chat/completions`, {
// Run AI analysis and mana-media upload in parallel
const analysisPromise = fetch(`${LLM_URL}/api/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
@ -41,7 +45,7 @@ routes.post('/analysis/photo', async (c) => {
{ type: 'text', text: 'Analysiere diese Mahlzeit.' },
{
type: 'image_url',
image_url: { url: `data:${mimeType || 'image/jpeg'};base64,${imageBase64}` },
image_url: { url: `data:${mime};base64,${imageBase64}` },
},
],
},
@ -52,12 +56,29 @@ routes.post('/analysis/photo', async (c) => {
}),
});
// Store meal photo in mana-media for Photos gallery & persistence
const ext = mime.split('/')[1] || 'jpg';
const { uploadImageToMedia } = await import('../../lib/media');
const buffer = Uint8Array.from(atob(imageBase64), (ch) => ch.charCodeAt(0));
const mediaPromise = uploadImageToMedia(buffer.buffer, `meal-${Date.now()}.${ext}`, {
app: 'nutriphi',
userId,
}).catch(() => null); // Don't fail analysis if media upload fails
const [res, mediaResult] = await Promise.all([analysisPromise, mediaPromise]);
if (!res.ok) return c.json({ error: 'AI analysis failed' }, 502);
const data = await res.json();
const content = data.choices?.[0]?.message?.content;
const analysis = typeof content === 'string' ? JSON.parse(content) : content;
// Attach media info so the frontend can store photoMediaId on the meal
if (mediaResult) {
analysis.mediaId = mediaResult.id;
analysis.photoUrl = mediaResult.urls.thumbnail || mediaResult.urls.original;
}
return c.json(analysis);
} catch (err) {
console.error('Photo analysis failed:', err);