diff --git a/api-gateway/src/api/service/ipfs.ts b/api-gateway/src/api/service/ipfs.ts index 9f49c1cf7..6ae66959b 100644 --- a/api-gateway/src/api/service/ipfs.ts +++ b/api-gateway/src/api/service/ipfs.ts @@ -1,15 +1,16 @@ -import { Body, Controller, Get, HttpCode, HttpException, HttpStatus, Param, Post, StreamableFile } from '@nestjs/common'; +import { Body, Controller, Get, HttpCode, HttpException, HttpStatus, Param, Post, Req, StreamableFile } from '@nestjs/common'; import { ApiBody, ApiExtraModels, ApiInternalServerErrorResponse, ApiOkResponse, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; import { Permissions } from '@guardian/interfaces'; import { Auth } from '#auth'; import { Examples, InternalServerErrorDTO } from '#middlewares'; -import { Guardians, InternalException } from '#helpers'; +import { CacheService, getCacheKey, Guardians, InternalException, UseCache } from '#helpers'; import { PinoLogger } from '@guardian/common'; +import { CACHE, PREFIXES } from '#constants'; @Controller('ipfs') @ApiTags('ipfs') export class IpfsApi { - constructor(private readonly logger: PinoLogger) { + constructor(private readonly cacheService: CacheService, private readonly logger: PinoLogger) { } /** @@ -41,7 +42,8 @@ export class IpfsApi { @ApiExtraModels(InternalServerErrorDTO) @HttpCode(HttpStatus.CREATED) async postFile( - @Body() body: any + @Body() body: any, + @Req() req ): Promise { try { if (!Object.values(body).length) { @@ -54,6 +56,12 @@ export class IpfsApi { throw new HttpException('File is not uploaded', HttpStatus.BAD_REQUEST); } + const invalidedCacheTags = [ + `${PREFIXES.IPFS}file/${cid}`, + `${PREFIXES.IPFS}file/${cid}/dry-run`, + ]; + await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], req.user)); + return JSON.stringify(cid); } catch (error) { await InternalException(error, this.logger); @@ -97,7 +105,8 @@ export class IpfsApi { @HttpCode(HttpStatus.CREATED) async postFileDryRun( @Param('policyId') policyId: string, - @Body() body: any + @Body() body: any, + @Req() req ): Promise { try { if (!Object.values(body).length) { @@ -107,6 +116,12 @@ export class IpfsApi { const guardians = new Guardians(); const { cid } = await guardians.addFileToDryRunStorage(body, policyId); + const invalidedCacheTags = [ + `${PREFIXES.IPFS}file/${cid}`, + `${PREFIXES.IPFS}file/${cid}/dry-run`, + ]; + await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], req.user)); + return JSON.stringify(cid); } catch (error) { await InternalException(error, this.logger); @@ -145,7 +160,7 @@ export class IpfsApi { type: InternalServerErrorDTO }) @ApiExtraModels(InternalServerErrorDTO) - // @UseCache({ ttl: CACHE.LONG_TTL }) + @UseCache({ ttl: CACHE.LONG_TTL }) @HttpCode(HttpStatus.OK) async getFile( @Param('cid') cid: string @@ -194,7 +209,7 @@ export class IpfsApi { type: InternalServerErrorDTO }) @ApiExtraModels(InternalServerErrorDTO) - // @UseCache({ ttl: CACHE.LONG_TTL }) + @UseCache({ ttl: CACHE.LONG_TTL }) @HttpCode(HttpStatus.OK) async getFileDryRun( @Param('cid') cid: string diff --git a/api-gateway/src/constants/routes.ts b/api-gateway/src/constants/routes.ts index aca9315da..154caf200 100644 --- a/api-gateway/src/constants/routes.ts +++ b/api-gateway/src/constants/routes.ts @@ -8,4 +8,5 @@ export const PREFIXES = { POLICIES: '/policies/', CONTRACTS: '/contracts/', TAGS: '/tags/', + IPFS: 'ipfs', }; \ No newline at end of file