All files / src/presentation/http/routes upload.routes.ts

95.45% Statements 21/22
90.9% Branches 10/11
100% Functions 3/3
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                  21x           21x             1x       22x                                             6x 6x   1x     5x 1x     4x   4x 1x             3x 3x   1x 1x           2x 2x 2x   2x   2x        
import { randomUUID } from 'node:crypto';
import fs from 'node:fs/promises';
import path from 'node:path';
import { z } from 'zod';
import type { App } from '../../../infrastructure/http/build-app';
import { MILESTONE_PHOTOS_DIR } from '../../../shared/config/uploads';
import { authenticate } from '../plugins/authenticate';
import { authErrorResponseSchema } from '../schemas/auth.schema';
 
const ALLOWED_MIME_TYPES: Record<string, string> = {
  'image/jpeg': 'jpg',
  'image/png': 'png',
  'image/webp': 'webp',
};
 
const uploadResponseSchema = z
  .object({
    url: z.string().url().describe('Absolute, publicly reachable URL of the uploaded photo'),
  })
  .describe('The uploaded milestone photo');
 
function isFileTooLargeError(error: unknown): boolean {
  return error instanceof Error && 'code' in error && (error as { code?: string }).code === 'FST_REQ_FILE_TOO_LARGE';
}
 
export async function uploadRoutes(app: App) {
  app.route({
    method: 'POST',
    url: '/uploads/milestone-photos',
    preHandler: authenticate,
    schema: {
      tags: ['Uploads'],
      summary: 'Upload a milestone photo',
      description:
        'Uploads an image (JPEG, PNG or WEBP, up to 5MB) as multipart/form-data under the "photo" field and ' +
        'returns its absolute public URL, to be used as a `Milestone.photoUrl`. Requires authentication to ' +
        'upload, but the resulting file is served publicly with no access control, consistent with `photoUrl` ' +
        'already accepting arbitrary public image URLs today.',
      consumes: ['multipart/form-data'],
      response: {
        201: uploadResponseSchema,
        400: authErrorResponseSchema,
        401: authErrorResponseSchema,
        500: authErrorResponseSchema,
      },
    },
    handler: async (request, reply) => {
      let file;
 
      try {
        file = await request.file();
      } catch {
        return reply.status(400).send({ status: 'error', message: 'Request must be multipart/form-data' });
      }
 
      if (!file || file.fieldname !== 'photo') {
        return reply.status(400).send({ status: 'error', message: 'Missing "photo" file field' });
      }
 
      const extension = ALLOWED_MIME_TYPES[file.mimetype];
 
      if (!extension) {
        return reply
          .status(400)
          .send({ status: 'error', message: 'Unsupported file type. Allowed types: image/jpeg, image/png, image/webp' });
      }
 
      let buffer: Buffer;
 
      try {
        buffer = await file.toBuffer();
      } catch (error) {
        Eif (isFileTooLargeError(error)) {
          return reply.status(400).send({ status: 'error', message: 'File exceeds the maximum allowed size of 5MB' });
        }
 
        throw error;
      }
 
      const filename = `${randomUUID()}.${extension}`;
      await fs.mkdir(MILESTONE_PHOTOS_DIR, { recursive: true });
      await fs.writeFile(path.join(MILESTONE_PHOTOS_DIR, filename), buffer);
 
      const url = `${request.protocol}://${request.headers.host}/uploads/milestone-photos/${filename}`;
 
      return reply.status(201).send({ url });
    },
  });
}