From 2a0425ff542d9bb6829505c67deaebb1849b80d2 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 18 Jun 2024 18:44:58 +0200 Subject: [PATCH] fix(delete functions): ensure more consistency between methods and trash behaviour --- test/test.trash.ts | 24 ++++++++++++++++++++++++ test/test.ts | 20 +++++++++----------- ts/00_commitinfo_data.ts | 2 +- ts/classes.directory.ts | 35 +++++++++++++++++++++++------------ ts/classes.metadata.ts | 2 +- ts/classes.trash.ts | 2 +- 6 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 test/test.trash.ts diff --git a/test/test.trash.ts b/test/test.trash.ts new file mode 100644 index 0000000..01573f9 --- /dev/null +++ b/test/test.trash.ts @@ -0,0 +1,24 @@ +import { expect, expectAsync, tap } from '@push.rocks/tapbundle'; +import { Qenv } from '@push.rocks/qenv'; + +import * as smartbucket from '../ts/index.js'; + +const testQenv = new Qenv('./', './.nogit/'); + +let testSmartbucket: smartbucket.SmartBucket; +let myBucket: smartbucket.Bucket; +let baseDirectory: smartbucket.Directory; + +tap.test('should create a valid smartbucket', async () => { + testSmartbucket = new smartbucket.SmartBucket({ + accessKey: await testQenv.getEnvVarOnDemand('S3_KEY'), + accessSecret: await testQenv.getEnvVarOnDemand('S3_SECRET'), + endpoint: 's3.eu-central-1.wasabisys.com', + }); + expect(testSmartbucket).toBeInstanceOf(smartbucket.SmartBucket); + myBucket = await testSmartbucket.getBucketByName('testzone'); + expect(myBucket).toBeInstanceOf(smartbucket.Bucket); + expect(myBucket.name).toEqual('testzone'); +}); + +export default tap.start(); \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index 41f1970..f9c9c4a 100644 --- a/test/test.ts +++ b/test/test.ts @@ -15,22 +15,20 @@ tap.test('should create a valid smartbucket', async () => { accessSecret: await testQenv.getEnvVarOnDemand('S3_SECRET'), endpoint: 's3.eu-central-1.wasabisys.com', }); -}); - -tap.skip.test('should create testbucket', async () => { - // await testSmartbucket.createBucket('testzone'); -}); - -tap.skip.test('should remove testbucket', async () => { - // await testSmartbucket.removeBucket('testzone'); -}); - -tap.test('should get a bucket', async () => { + expect(testSmartbucket).toBeInstanceOf(smartbucket.SmartBucket); myBucket = await testSmartbucket.getBucketByName('testzone'); expect(myBucket).toBeInstanceOf(smartbucket.Bucket); expect(myBucket.name).toEqual('testzone'); }); +tap.skip.test('should create testbucket', async () => { + // await testSmartbucket.createBucket('testzone2'); +}); + +tap.skip.test('should remove testbucket', async () => { + // await testSmartbucket.removeBucket('testzone2'); +}); + // Fast operations tap.test('should store data in bucket fast', async () => { await myBucket.fastPut({ diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index f660db8..70c42bc 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartbucket', - version: '3.0.18', + version: '3.0.19', description: 'A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.' } diff --git a/ts/classes.directory.ts b/ts/classes.directory.ts index 4584285..dc3e04a 100644 --- a/ts/classes.directory.ts +++ b/ts/classes.directory.ts @@ -66,13 +66,13 @@ export class Directory { * gets a file by name */ public async getFile(optionsArg: { - name: string; + path: string; createWithContents?: string | Buffer; getFromTrash?: boolean; }): Promise { const pathDescriptor = { directory: this, - path: optionsArg.name, + path: optionsArg.path, }; const exists = await this.bucketRef.fastExists({ path: await helpers.reducePathDescriptorToPath(pathDescriptor), @@ -88,13 +88,13 @@ export class Directory { if (!exists && optionsArg.createWithContents) { await File.create({ directory: this, - name: optionsArg.name, + name: optionsArg.path, contents: optionsArg.createWithContents, }); } return new File({ directoryRefArg: this, - fileName: optionsArg.name, + fileName: optionsArg.path, }); } @@ -283,19 +283,30 @@ export class Directory { /** * removes a file within the directory + * uses file class to make sure effects for metadata etc. are handled correctly * @param optionsArg */ - public async fastRemove(optionsArg: { path: string }) { - const path = plugins.path.join(this.getBasePath(), optionsArg.path); - await this.bucketRef.fastRemove({ - path, + public async fastRemove(optionsArg: { + path: string + /** + * wether the file should be placed into trash. Default is false. + */ + mode?: 'permanent' | 'trash'; + }) { + const file = await this.getFile({ + path: optionsArg.path, + }); + await file.delete({ + mode: optionsArg.mode ? optionsArg.mode : 'permanent', }); } /** * deletes the directory with all its contents */ - public async delete() { + public async delete(optionsArg: { + mode?: 'permanent' | 'trash'; + }) { const deleteDirectory = async (directoryArg: Directory) => { const childDirectories = await directoryArg.listDirectories(); if (childDirectories.length === 0) { @@ -307,9 +318,9 @@ export class Directory { } const files = await directoryArg.listFiles(); for (const file of files) { - await directoryArg.fastRemove({ - path: file.name, - }); + await file.delete({ + mode: optionsArg.mode ? optionsArg.mode : 'permanent', + }) } }; await deleteDirectory(this); diff --git a/ts/classes.metadata.ts b/ts/classes.metadata.ts index 343ea9f..0abd085 100644 --- a/ts/classes.metadata.ts +++ b/ts/classes.metadata.ts @@ -10,7 +10,7 @@ export class MetaData { // lets find the existing metadata file metaData.metadataFile = await metaData.fileRef.parentDirectoryRef.getFile({ - name: metaData.fileRef.name + '.metadata', + path: metaData.fileRef.name + '.metadata', createWithContents: '{}', }); diff --git a/ts/classes.trash.ts b/ts/classes.trash.ts index f42e854..e1ed861 100644 --- a/ts/classes.trash.ts +++ b/ts/classes.trash.ts @@ -21,7 +21,7 @@ export class Trash { const trashDir = await this.getTrashDir(); const originalPath = await helpers.reducePathDescriptorToPath(pathDescriptor); const trashKey = await this.getTrashKeyByOriginalBasePath(originalPath); - return trashDir.getFile({ name: trashKey }); + return trashDir.getFile({ path: trashKey }); } public async getTrashKeyByOriginalBasePath (originalPath: string): Promise {