import { Body, Controller, HttpCode, HttpStatus, Inject, Param, ParseFilePipe, Post, Put, Res, UploadedFiles, UseInterceptors, } from '@nestjs/common'; import { ApiConsumes, ApiTags } from '@nestjs/swagger'; import { Response } from 'express'; import { EndpointLifecycle } from 'src/decorators'; import { AssetBulkUploadCheckResponseDto, AssetMediaResponseDto, AssetMediaStatusEnum, CheckExistingAssetsResponseDto, } from 'src/dtos/asset-media-response.dto'; import { AssetBulkUploadCheckDto, AssetMediaReplaceDto, CheckExistingAssetsDto, UploadFieldName, } from 'src/dtos/asset-media.dto'; import { AuthDto } from 'src/dtos/auth.dto'; import { ILoggerRepository } from 'src/interfaces/logger.interface'; import { Auth, Authenticated } from 'src/middleware/auth.guard'; import { FileUploadInterceptor, Route, UploadFiles, getFiles } from 'src/middleware/file-upload.interceptor'; import { AssetMediaService } from 'src/services/asset-media.service'; import { FileNotEmptyValidator, UUIDParamDto } from 'src/validation'; @ApiTags('Assets') @Controller(Route.ASSET) export class AssetMediaController { constructor( @Inject(ILoggerRepository) private logger: ILoggerRepository, private service: AssetMediaService, ) {} /** * Replace the asset with new file, without changing its id */ @Put(':id/file') @UseInterceptors(FileUploadInterceptor) @ApiConsumes('multipart/form-data') @Authenticated({ sharedLink: true }) @EndpointLifecycle({ addedAt: 'v1.106.0' }) async replaceAsset( @Auth() auth: AuthDto, @Param() { id }: UUIDParamDto, @UploadedFiles(new ParseFilePipe({ validators: [new FileNotEmptyValidator([UploadFieldName.ASSET_DATA])] })) files: UploadFiles, @Body() dto: AssetMediaReplaceDto, @Res({ passthrough: true }) res: Response, ): Promise { const { file } = getFiles(files); const responseDto = await this.service.replaceAsset(auth, id, dto, file); if (responseDto.status === AssetMediaStatusEnum.DUPLICATE) { res.status(HttpStatus.OK); } return responseDto; } /** * Checks if multiple assets exist on the server and returns all existing - used by background backup */ @Post('exist') @HttpCode(HttpStatus.OK) @Authenticated() checkExistingAssets( @Auth() auth: AuthDto, @Body() dto: CheckExistingAssetsDto, ): Promise { return this.service.checkExistingAssets(auth, dto); } /** * Checks if assets exist by checksums */ @Post('bulk-upload-check') @HttpCode(HttpStatus.OK) @Authenticated() checkBulkUpload( @Auth() auth: AuthDto, @Body() dto: AssetBulkUploadCheckDto, ): Promise { return this.service.bulkUploadCheck(auth, dto); } }