diff --git a/server/src/code-snippets/controllers/code-snippets.controller.ts b/server/src/code-snippets/controllers/code-snippets.controller.ts index 7b6b749..4c3794b 100644 --- a/server/src/code-snippets/controllers/code-snippets.controller.ts +++ b/server/src/code-snippets/controllers/code-snippets.controller.ts @@ -12,7 +12,7 @@ import { CodeSnippetsService } from '../services/code-snippets.service'; export class CodeSnippetsController { constructor(private readonly codeSnippetsService: CodeSnippetsService) {} - @Get(':subdirectory(*)') + @Get(':subdirectory(*)|/') @UseGuards(AuthSessionGuard) @ApiCookieAuth() @ApiOkResponse({ description: 'Tree of user files.' }) @@ -23,7 +23,7 @@ export class CodeSnippetsController { ) { return this.codeSnippetsService.listUserCodeSnippets( req.user['username'], - subdirectory, + subdirectory ?? '', ); } } diff --git a/server/src/code-snippets/services/code-snippets.service.ts b/server/src/code-snippets/services/code-snippets.service.ts index 283ebe0..fef0c8b 100644 --- a/server/src/code-snippets/services/code-snippets.service.ts +++ b/server/src/code-snippets/services/code-snippets.service.ts @@ -9,10 +9,17 @@ export class CodeSnippetsService { async listUserCodeSnippets(username: string, subdirectory?: string) { const tree = {}; + const prefix = `${username}/${ + subdirectory + ? subdirectory.endsWith('/') + ? subdirectory + : subdirectory + '/' + : '' + }`; const queryResponse = await this.s3 .listObjectsV2({ Bucket: configObject.aws.codeSnippetsS3Bucket, - Prefix: `${username}/${subdirectory ?? ''}`, + Prefix: prefix, }) .promise(); @@ -30,7 +37,7 @@ export class CodeSnippetsService { // Each such array is being iterated over a recursive function which keeps nesting // items (strings from the array) until the last one is reached. queryResponse.Contents.filter((item) => !item.Key.endsWith('/')) - .map((item) => item.Key.replace(`${username}/`, '').split('/')) + .map((item) => item.Key.replace(prefix, '').split('/')) .forEach((item) => this.addToFileTree(tree, item)); return tree;