import { FastifyInstance } from 'fastify'; import multipart from '@fastify/multipart'; import fastifyStatic from '@fastify/static'; import path from 'path'; import { fileURLToPath } from 'url'; import * as svc from '../services/image.service.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); export async function imageRoutes(app: FastifyInstance) { await app.register(multipart, { limits: { fileSize: 10 * 1024 * 1024 } }); await app.after(); await app.register(fastifyStatic, { root: path.resolve(__dirname, '../../data/images'), prefix: '/images/', decorateReply: false, }); await app.after(); app.post('/api/recipes/:id/image', async (request, reply) => { const { id } = request.params as { id: string }; const file = await request.file(); if (!file) return reply.status(400).send({ error: 'No file uploaded' }); const buffer = await file.toBuffer(); const result = await svc.saveRecipeImage(id, buffer); if (!result) return reply.status(404).send({ error: 'Recipe not found' }); return result; }); app.post('/api/recipes/:id/steps/:stepNumber/image', async (request, reply) => { const { id, stepNumber } = request.params as { id: string; stepNumber: string }; const file = await request.file(); if (!file) return reply.status(400).send({ error: 'No file uploaded' }); const buffer = await file.toBuffer(); const result = await svc.saveStepImage(id, Number(stepNumber), buffer); if (!result) return reply.status(404).send({ error: 'Step not found' }); return result; }); }