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

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

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