Skip to content

Commit

Permalink
[Fix] Issue with subdirectory passing
Browse files Browse the repository at this point in the history
Resolved an issue where listing would sometimes return empty filepaths.
References #18.
  • Loading branch information
angel-penchev committed Oct 20, 2021
1 parent 9468fa7 commit 7cbad9a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.' })
Expand All @@ -23,7 +23,7 @@ export class CodeSnippetsController {
) {
return this.codeSnippetsService.listUserCodeSnippets(
req.user['username'],
subdirectory,
subdirectory ?? '',
);
}
}
11 changes: 9 additions & 2 deletions server/src/code-snippets/services/code-snippets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down

0 comments on commit 7cbad9a

Please sign in to comment.