From b3d5f0052dbcafdbd8539ca2449ad4dd2af5e07d Mon Sep 17 00:00:00 2001 From: Angel Penchev <26301867+angel-penchev@users.noreply.github.com> Date: Wed, 20 Oct 2021 20:03:13 +0300 Subject: [PATCH] [Feat] Get code snippet contents Added a service method for retrieving code snippet file contents. Added the service method to the GET "/code-snppets" endpoint. References #18. --- .../controllers/code-snippets.controller.ts | 27 ++++++++++++------- .../services/code-snippets.service.ts | 19 ++++++++++++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/server/src/code-snippets/controllers/code-snippets.controller.ts b/server/src/code-snippets/controllers/code-snippets.controller.ts index 17a1d22..ff3ff0d 100644 --- a/server/src/code-snippets/controllers/code-snippets.controller.ts +++ b/server/src/code-snippets/controllers/code-snippets.controller.ts @@ -26,20 +26,27 @@ import { CodeSnippetsService } from '../services/code-snippets.service'; export class CodeSnippetsController { constructor(private readonly codeSnippetsService: CodeSnippetsService) {} - @Get(':subdirectory(*)|/') + @Get(':path(*)') @HttpCode(HttpStatus.OK) @UseGuards(AuthSessionGuard) @ApiCookieAuth() - @ApiOkResponse({ description: 'Tree of user files.' }) + @ApiOkResponse({ + description: + 'Tree of user files in a directory or the contents of a file specified.', + }) @ApiForbiddenResponse({ description: 'No user logon.' }) - async getSubdirectory( - @Req() req: Request, - @Param('subdirectory') subdirectory: string, - ) { - return await this.codeSnippetsService.listUserCodeSnippets( - req.user['username'], - subdirectory ?? '', - ); + async getSubdirectory(@Req() req: Request, @Param('path') path: string) { + path ??= ''; + + return path.endsWith('/') || path === '' + ? await this.codeSnippetsService.listUserCodeSnippets( + req.user['username'], + path, + ) + : await this.codeSnippetsService.getUserCodeSnippet( + req.user['username'], + path, + ); } @Put(':filepath(*)') diff --git a/server/src/code-snippets/services/code-snippets.service.ts b/server/src/code-snippets/services/code-snippets.service.ts index 2a12a04..212a6b4 100644 --- a/server/src/code-snippets/services/code-snippets.service.ts +++ b/server/src/code-snippets/services/code-snippets.service.ts @@ -45,6 +45,23 @@ export class CodeSnippetsService { return tree; } + async getUserCodeSnippet(username: string, filepath: string) { + const completeFilepath = `${username}/${filepath}`; + + if (completeFilepath.endsWith('/')) { + throw new CodeSnippetsError(CodeSnippetsErrorCode.INVALID_FILE_PATH); + } + + const queryResponse = await this.s3 + .getObject({ + Bucket: configObject.aws.codeSnippetsS3Bucket, + Key: completeFilepath, + }) + .promise(); + + return queryResponse.Body.toString(); + } + async saveUserCodeSnippet(username: string, filepath: string, body: string) { const completeFilepath = `${username}/${filepath}`; @@ -68,6 +85,6 @@ export class CodeSnippetsService { } // If directory => create empty object if it doesn't already exist and run again - return this.addToFileTree((tree[item.shift()] ??= {}), item); + return this.addToFileTree((tree[item.shift() + '/'] ??= {}), item); } }