Skip to content

Commit

Permalink
feat: add deleteDir and readDir methods to FileStorageS3
Browse files Browse the repository at this point in the history
  • Loading branch information
eamon0989 committed Oct 27, 2022
1 parent f32db73 commit 1f9d517
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/file-storage/src/file-storage-s3.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ function config(setup: FileStorageS3Setup) {
};
}

function removeTrailingForwardSlash(string: string) {
return string.endsWith('/') ? string.slice(0, -1) : string;
}

function addTrailingForwardSlash(string: string) {
return string.endsWith('/') ? string : `${string}/`;
}

// TODO: control filesize limit
export class FileStorageS3 implements FileStorage {
config: FileStorageConfig & FileStorageS3Config;
Expand Down Expand Up @@ -121,4 +129,58 @@ export class FileStorageS3 implements FileStorage {
await s3.deleteObject({ Bucket, Key, ...options }).promise();
return true;
}

async deleteDir(args: { dirPath: string; request?: Request }): Promise<void> {
const { dirPath, request } = args;
const { s3, bucket: Bucket } = this.config;
const listKey = await this.transformFilePath(dirPath, request);
const listParams = {
Bucket,
Prefix: listKey,
};
// get list of objects in a dir
const listedObjects = await s3.listObjectsV2(listParams).promise();
if (listedObjects.Contents.length === 0) return;

const deleteParams = {
Bucket,
Delete: { Objects: [] },
};
listedObjects.Contents.forEach(({ Key }) => {
deleteParams.Delete.Objects.push({ Key });
});
await s3.deleteObjects(deleteParams).promise();

if (listedObjects.IsTruncated) await this.deleteDir({ dirPath });
}

async readDir(args: { dirPath: string }) {
const { dirPath } = args;
const { s3, bucket: Bucket } = this.config;
const Key = await this.transformFilePath(dirPath.toLowerCase());
const listParams = {
Bucket,
Delimiter: '/',
};
// Passing in / as Key breaks the folder matching
if (Key !== '/' && Key !== '') {
listParams['Prefix'] = addTrailingForwardSlash(Key);
}
const listedObjects = await s3.listObjectsV2(listParams).promise();
const schemaList: string[] = [];

listedObjects.CommonPrefixes?.forEach((prefixObject) => {
const schema = removeTrailingForwardSlash(prefixObject.Prefix);
const key = listParams['Prefix'];
// If key exists, we are looking for a nested folder such as v0.1.0
if (key) {
const schemaVersion = schema.slice(key.length); // e.g. v0.1.0
schemaList.push(schemaVersion);
} else {
schemaList.push(schema); // e.g. en10168-schemas
}
});

return schemaList;
}
}

0 comments on commit 1f9d517

Please sign in to comment.