import { Controller, Delete, Get, Param, Post, Query, Req, UseGuards } from '@nestjs/common'; import { ApiBearerAuth, ApiBody, ApiConsumes, ApiCreatedResponse, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; import { FastifyRequest } from 'fastify'; import { CurrentUser } from '../../common/decorators/current-user.decorator'; import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; import { MediaService } from './media.service'; import { MediaItemDto } from '../profiles/dto/profile-response.dto'; import { MessageResponseDto } from '../../common/dto/message-response.dto'; @ApiTags('media') @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Controller('profiles/:profileId/media') export class MediaController { constructor(private readonly mediaService: MediaService) {} @Post('upload') @ApiOperation({ summary: 'Upload photo / video / audio to profile' }) @ApiConsumes('multipart/form-data') @ApiBody({ schema: { type: 'object', required: ['file'], properties: { file: { type: 'string', format: 'binary' }, }, }, }) @ApiCreatedResponse({ type: MediaItemDto }) async upload( @CurrentUser('id') userId: string, @Param('profileId') profileId: string, @Req() req: FastifyRequest, @Query('type') type: 'photo' | 'video' | 'audio' = 'photo', ) { const data = await (req as any).file(); if (!data) throw new Error('No file provided'); const buffer = await data.toBuffer(); return this.mediaService.upload( userId, profileId, { buffer, originalname: data.filename, mimetype: data.mimetype }, type, ); } @Get() @ApiOperation({ summary: 'Get all media for a profile' }) @ApiOkResponse({ type: [MediaItemDto] }) getMedia(@Param('profileId') profileId: string) { return this.mediaService.getByProfileId(profileId); } @Delete(':mediaId') @ApiOperation({ summary: 'Delete media item' }) @ApiOkResponse({ type: MessageResponseDto }) deleteMedia( @CurrentUser('id') userId: string, @Param('mediaId') mediaId: string, ) { return this.mediaService.delete(userId, mediaId); } }