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'),
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({

View File

@ -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.'
}

View File

@ -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<File> {
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);

View File

@ -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: '{}',
});

View File

@ -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<string> {