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

@ -98,7 +98,27 @@ routes.post('/generate', async (c) => {
await consumeCredits(userId, 'AI_IMAGE_GENERATION', cost, `Image: ${prompt.slice(0, 50)}`);
return c.json({ imageUrl, prompt, model: model || 'flux-schnell' });
// Store generated image in mana-media for dedup, thumbnails & Photos gallery
try {
const { uploadImageToMedia } = await import('../../lib/media');
const imgRes = await fetch(imageUrl);
const imgBuffer = await imgRes.arrayBuffer();
const media = await uploadImageToMedia(imgBuffer, `generated-${Date.now()}.png`, {
app: 'picture',
userId,
});
return c.json({
imageUrl: media.urls.original,
mediaId: media.id,
thumbnailUrl: media.urls.thumbnail,
prompt,
model: model || 'flux-schnell',
});
} catch {
// Fallback: return raw imageUrl if mana-media is unavailable
return c.json({ imageUrl, prompt, model: model || 'flux-schnell' });
}
} catch (_err) {
return c.json({ error: 'Generation failed' }, 500);
}
@ -115,19 +135,19 @@ routes.post('/upload', async (c) => {
if (file.size > 10 * 1024 * 1024) return c.json({ error: 'Max 10MB' }, 400);
try {
const { createPictureStorage, generateUserFileKey, getContentType } = await import(
'@manacore/shared-storage'
const { uploadImageToMedia } = await import('../../lib/media');
const buffer = await file.arrayBuffer();
const result = await uploadImageToMedia(buffer, file.name, { app: 'picture', userId });
return c.json(
{
storagePath: result.id,
publicUrl: result.urls.original,
mediaId: result.id,
thumbnailUrl: result.urls.thumbnail,
},
201
);
const storage = createPictureStorage();
const key = generateUserFileKey(userId, file.name);
const buffer = Buffer.from(await file.arrayBuffer());
const result = await storage.upload(key, buffer, {
contentType: getContentType(file.name),
public: true,
});
return c.json({ storagePath: key, publicUrl: result.url }, 201);
} catch (_err) {
return c.json({ error: 'Upload failed' }, 500);
}