import { Body, Controller, Get, HttpStatus, Param, Post, Query, Req, Res, UseGuards, } from '@nestjs/common'; import { Response } from 'express'; import { JwtAuthGuard } from '../../auth/guards/jwt.guard'; import { MediaDataService } from '../service/media-data.service'; @Controller('media') @UseGuards(JwtAuthGuard) export class MediaController { constructor(private readonly mediaDataService: MediaDataService) {} // Method for initiating an upload @Post('initiate-upload') async initiateUpload( @Body() body: { file_name: string; attribute_key: string; mapped_entity_type?: string; mapped_entity_id?: number; parent_id?: number; parent_type?: string; }, @Req() req: Request & { user: any }, @Res() res: Response, ) { const loggedInUser = req.user.userData; const result = await this.mediaDataService.generateMediaUploadDetails( body.file_name, body.attribute_key, loggedInUser, body.mapped_entity_type, body.mapped_entity_id, body.parent_id, body.parent_type, ); if (result) { res.status(HttpStatus.OK).json({ message: 'Upload initiated successfully', success: true, data: result, }); } else { throw new Error('Something went wrong!'); } } // Method to fetch the download URL for a media file @Get('download/:id') async getDownloadUrl( @Param('id') id: number, @Res() res: Response, @Req() req: Request & { user: any }, @Query('expiresIn') expiresIn?: number, ) { const loggedInUser = req.user.userData; try { const downloadUrl = await this.mediaDataService.getMediaDownloadUrl( id, loggedInUser, expiresIn, ); res.status(HttpStatus.OK).json({ message: 'Download URL fetched successfully', success: true, data: downloadUrl, }); } catch (error) { res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: error.message || 'Error fetching download URL', success: false, }); } } @Post('generateUploadPath') async generateUploadPath( @Req() req: Request & { user: any }, @Body() body: { mapped_entity_type: string; mapped_entity_id?: number; parent_id?: number; parent_type?: string; }, ) { const loggedInUser = req.user.userData; return this.mediaDataService.buildUploadPathGeneric( body.mapped_entity_type, loggedInUser, body.mapped_entity_id, body.parent_id, body.parent_type, ); } // media.controller.ts @Get('inline/:id') async getInlineUrl( @Param('id') id: number, @Res() res: Response, @Req() req: Request & { user: any }, @Query('expiresIn') expiresIn?: number, ) { const loggedInUser = req.user.userData; try { const inlineUrl = await this.mediaDataService.getMediaInlineUrl( id, loggedInUser, expiresIn, ); res.status(HttpStatus.OK).json({ message: 'Inline URL fetched successfully', success: true, data: { inlineUrl }, }); } catch (error) { res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: error.message || 'Error fetching inline URL', success: false, }); } } @Post('update/review-status/:id') async updateMediaReviewStatus( @Param('id') id: number, @Body() body: any, @Req() req: Request & { user: any }, @Res() res: Response, ) { const loggedInUser = req.user.userData; try { const result = await this.mediaDataService.updateMediaReviewStatus( id, body, loggedInUser, ); res.status(HttpStatus.OK).json({ message: 'Media review status updated successfully', success: true, data: result, }); } catch (error) { res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: error.message || 'Error updating media review status', success: false, }); } } }