fix(delete functions): ensure more consistency between methods and trash behaviour

This commit is contained in:
Philipp Kunz 2024-06-18 18:44:58 +02:00
parent 9adcdee0a0
commit 2a0425ff54
6 changed files with 59 additions and 26 deletions

24
test/test.trash.ts Normal file
View File

@ -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();

View File

@ -15,22 +15,20 @@ tap.test('should create a valid smartbucket', async () => {
accessSecret: await testQenv.getEnvVarOnDemand('S3_SECRET'), accessSecret: await testQenv.getEnvVarOnDemand('S3_SECRET'),
endpoint: 's3.eu-central-1.wasabisys.com', endpoint: 's3.eu-central-1.wasabisys.com',
}); });
}); expect(testSmartbucket).toBeInstanceOf(smartbucket.SmartBucket);
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 () => {
myBucket = await testSmartbucket.getBucketByName('testzone'); myBucket = await testSmartbucket.getBucketByName('testzone');
expect(myBucket).toBeInstanceOf(smartbucket.Bucket); expect(myBucket).toBeInstanceOf(smartbucket.Bucket);
expect(myBucket.name).toEqual('testzone'); 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 // Fast operations
tap.test('should store data in bucket fast', async () => { tap.test('should store data in bucket fast', async () => {
await myBucket.fastPut({ await myBucket.fastPut({

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartbucket', 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.' description: 'A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.'
} }

View File

@ -66,13 +66,13 @@ export class Directory {
* gets a file by name * gets a file by name
*/ */
public async getFile(optionsArg: { public async getFile(optionsArg: {
name: string; path: string;
createWithContents?: string | Buffer; createWithContents?: string | Buffer;
getFromTrash?: boolean; getFromTrash?: boolean;
}): Promise<File> { }): Promise<File> {
const pathDescriptor = { const pathDescriptor = {
directory: this, directory: this,
path: optionsArg.name, path: optionsArg.path,
}; };
const exists = await this.bucketRef.fastExists({ const exists = await this.bucketRef.fastExists({
path: await helpers.reducePathDescriptorToPath(pathDescriptor), path: await helpers.reducePathDescriptorToPath(pathDescriptor),
@ -88,13 +88,13 @@ export class Directory {
if (!exists && optionsArg.createWithContents) { if (!exists && optionsArg.createWithContents) {
await File.create({ await File.create({
directory: this, directory: this,
name: optionsArg.name, name: optionsArg.path,
contents: optionsArg.createWithContents, contents: optionsArg.createWithContents,
}); });
} }
return new File({ return new File({
directoryRefArg: this, directoryRefArg: this,
fileName: optionsArg.name, fileName: optionsArg.path,
}); });
} }
@ -283,19 +283,30 @@ export class Directory {
/** /**
* removes a file within the directory * removes a file within the directory
* uses file class to make sure effects for metadata etc. are handled correctly
* @param optionsArg * @param optionsArg
*/ */
public async fastRemove(optionsArg: { path: string }) { public async fastRemove(optionsArg: {
const path = plugins.path.join(this.getBasePath(), optionsArg.path); path: string
await this.bucketRef.fastRemove({ /**
path, * 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 * deletes the directory with all its contents
*/ */
public async delete() { public async delete(optionsArg: {
mode?: 'permanent' | 'trash';
}) {
const deleteDirectory = async (directoryArg: Directory) => { const deleteDirectory = async (directoryArg: Directory) => {
const childDirectories = await directoryArg.listDirectories(); const childDirectories = await directoryArg.listDirectories();
if (childDirectories.length === 0) { if (childDirectories.length === 0) {
@ -307,9 +318,9 @@ export class Directory {
} }
const files = await directoryArg.listFiles(); const files = await directoryArg.listFiles();
for (const file of files) { for (const file of files) {
await directoryArg.fastRemove({ await file.delete({
path: file.name, mode: optionsArg.mode ? optionsArg.mode : 'permanent',
}); })
} }
}; };
await deleteDirectory(this); await deleteDirectory(this);

View File

@ -10,7 +10,7 @@ export class MetaData {
// lets find the existing metadata file // lets find the existing metadata file
metaData.metadataFile = await metaData.fileRef.parentDirectoryRef.getFile({ metaData.metadataFile = await metaData.fileRef.parentDirectoryRef.getFile({
name: metaData.fileRef.name + '.metadata', path: metaData.fileRef.name + '.metadata',
createWithContents: '{}', createWithContents: '{}',
}); });

View File

@ -21,7 +21,7 @@ export class Trash {
const trashDir = await this.getTrashDir(); const trashDir = await this.getTrashDir();
const originalPath = await helpers.reducePathDescriptorToPath(pathDescriptor); const originalPath = await helpers.reducePathDescriptorToPath(pathDescriptor);
const trashKey = await this.getTrashKeyByOriginalBasePath(originalPath); const trashKey = await this.getTrashKeyByOriginalBasePath(originalPath);
return trashDir.getFile({ name: trashKey }); return trashDir.getFile({ path: trashKey });
} }
public async getTrashKeyByOriginalBasePath (originalPath: string): Promise<string> { public async getTrashKeyByOriginalBasePath (originalPath: string): Promise<string> {