Add file deletion method in storage service

Implemented a new method to delete files from the storage by checksum and extension. This method logs the deletion process and throws a NotFoundException if the file cannot be found. Updated dependencies to include 'rm' from 'node:fs/promises'.
This commit is contained in:
Mathis H (Avnyr) 2024-10-14 12:10:52 +02:00
parent 79b2dec9e9
commit 8686f0c27b
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -1,6 +1,6 @@
import * as console from "node:console"; import * as console from "node:console";
import * as crypto from "node:crypto"; import * as crypto from "node:crypto";
import { readFile, writeFile } from "node:fs/promises"; import { readFile, writeFile, rm } from "node:fs/promises";
import { join } from "node:path"; import { join } from "node:path";
import { import {
BadRequestException, BadRequestException,
@ -263,4 +263,29 @@ export class StorageService {
throw err; throw err;
} }
} }
/**
* Deletes a file from the storage.
*
* @param {string} checksum - The checksum of the file to delete.
* @param {string} extension - The extension of the file to delete.
* @param {boolean} isDocumentation - A flag indicating whether the file is a documentation file.
* @return {Promise<void>} A promise that resolves when the file is successfully deleted.
* @throws {NotFoundException} Throws an exception if the file cannot be found.
*/
public async delete(
checksum: string,
extension: string,
isDocumentation: boolean,
) {
try {
const fileName = `${isDocumentation ? "doc" : "file"}-${checksum}.${extension.toLowerCase()}`;
console.log(`Deleting file "${fileName}" from storage...`);
await rm(join(process.cwd(), "assets/", fileName));
console.log(`File "${fileName}" deleted successfully.`);
} catch (err) {
console.log("File not found.");
throw new NotFoundException(err);
}
}
} }