diff --git a/test/test.trash.ts b/test/test.trash.ts index 0bc5e15..3787487 100644 --- a/test/test.trash.ts +++ b/test/test.trash.ts @@ -21,8 +21,20 @@ tap.test('should create a valid smartbucket', async () => { expect(myBucket.name).toEqual('testzone'); }); -tap.test('', async () => { - -}) +tap.test('should put a file into the trash', async () => { + const path = 'hithere/socool.txt'; + const file = await myBucket.fastPut({ + path, + contents: 'hi there!', + }); + expect(await file.getMetaData().then((meta) => meta.metadataFile.getJsonData())).toEqual({}); + await file.delete({ mode: 'trash' }); + expect(await file.getMetaData().then((meta) => meta.metadataFile.getJsonData())).toEqual({ + custom_recycle: { + deletedAt: 123, + originalPath: 'hithere/socool.txt', + }, + }); +}); -export default tap.start(); \ No newline at end of file +export default tap.start(); diff --git a/test/test.ts b/test/test.ts index f43abd2..7bb9854 100644 --- a/test/test.ts +++ b/test/test.ts @@ -41,9 +41,12 @@ tap.test('should get data in bucket', async () => { const fileString = await myBucket.fastGet({ path: 'hithere/socool.txt', }); - const fileStringStream = await myBucket.fastGetStream({ - path: 'hithere/socool.txt', - }, 'nodestream'); + const fileStringStream = await myBucket.fastGetStream( + { + path: 'hithere/socool.txt', + }, + 'nodestream' + ); console.log(fileString); }); diff --git a/ts/classes.bucket.ts b/ts/classes.bucket.ts index 67b355a..0096bb2 100644 --- a/ts/classes.bucket.ts +++ b/ts/classes.bucket.ts @@ -331,7 +331,9 @@ export class Bucket { }): Promise { try { const destinationBucket = optionsArg.targetBucket || this; - const exists = await destinationBucket.fastExists({ path: optionsArg.destinationPath }); + const exists = await destinationBucket.fastExists({ + path: optionsArg.destinationPath, + }); if (exists && !optionsArg.overwrite) { console.error( @@ -424,8 +426,8 @@ export class Bucket { Prefix: checkPath, Delimiter: '/', }); - const response = await this.smartbucketRef.s3Client.send(command); - return response.CommonPrefixes.length > 0; + const { CommonPrefixes } = await this.smartbucketRef.s3Client.send(command); + return !!CommonPrefixes && CommonPrefixes.length > 0; } public async isFile(pathDescriptor: interfaces.IPathDecriptor): Promise { @@ -435,8 +437,8 @@ export class Bucket { Prefix: checkPath, Delimiter: '/', }); - const response = await this.smartbucketRef.s3Client.send(command); - return response.Contents.length > 0; + const { Contents } = await this.smartbucketRef.s3Client.send(command); + return !!Contents && Contents.length > 0; } public async getMagicBytes(optionsArg: { path: string; length: number }): Promise { @@ -449,7 +451,7 @@ export class Bucket { const response = await this.smartbucketRef.s3Client.send(command); const chunks = []; const stream = response.Body as any; // SdkStreamMixin includes readable stream - + for await (const chunk of stream) { chunks.push(chunk); } diff --git a/ts/classes.file.ts b/ts/classes.file.ts index 37aa8b7..0eafd61 100644 --- a/ts/classes.file.ts +++ b/ts/classes.file.ts @@ -92,16 +92,13 @@ export class File { /** * deletes this file */ - public async delete(optionsArg?: { - mode: 'trash' | 'permanent'; - }) { - + public async delete(optionsArg?: { mode: 'trash' | 'permanent' }) { optionsArg = { - ... { + ...{ mode: 'permanent', }, ...optionsArg, - } + }; if (optionsArg.mode === 'permanent') { await this.parentDirectoryRef.bucketRef.fastRemove({ @@ -126,7 +123,7 @@ export class File { path: await trash.getTrashKeyByOriginalBasePath(this.getBasePath()), }); } - + await this.parentDirectoryRef.listFiles(); } @@ -169,16 +166,19 @@ export class File { await this.parentDirectoryRef.bucketRef.fastPutStream({ path: this.getBasePath(), readableStream: optionsArg.contents, + overwrite: true, }); } else if (Buffer.isBuffer(optionsArg.contents)) { await this.parentDirectoryRef.bucketRef.fastPut({ path: this.getBasePath(), contents: optionsArg.contents, + overwrite: true, }); } else if (typeof optionsArg.contents === 'string') { await this.parentDirectoryRef.bucketRef.fastPut({ path: this.getBasePath(), contents: Buffer.from(optionsArg.contents, optionsArg.encoding), + overwrite: true, }); } } @@ -238,7 +238,7 @@ export class File { public async getMagicBytes(optionsArg: { length: number }): Promise { return this.parentDirectoryRef.bucketRef.getMagicBytes({ path: this.getBasePath(), - length: optionsArg.length - }) + length: optionsArg.length, + }); } }