From ddf9ef48602a40592cae6021f4795fa72967f8b8 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 30 Sep 2024 11:30:14 +0200 Subject: [PATCH] Add method to read file from storage Introduced a new `read` method in `storage.service.ts` to fetch files from storage based on checksum, extension, and type. This method handles potential exceptions by throwing a `NotFoundException` if the file is not found. --- .../src/app/storage/storage.service.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/apps/backend/src/app/storage/storage.service.ts b/apps/backend/src/app/storage/storage.service.ts index 9ee57b7..34947fd 100644 --- a/apps/backend/src/app/storage/storage.service.ts +++ b/apps/backend/src/app/storage/storage.service.ts @@ -177,6 +177,32 @@ export class StorageService { }; } + /** + * Reads a file from storage based on the provided checksum, extension, and type. + * + * @param {string} checksum - The checksum of the file for validation purposes. + * @param {string} extension - The file extension to define the type of file. + * @param {boolean} isDocumentation - A flag to determine if the file is documentation or a regular file. + * @return {Promise} - A promise that resolves to the file data buffer. + * @throws {NotFoundException} - Throws a NotFoundException if the file is not found in storage. + */ + public async read( + checksum: string, + extension: string, + isDocumentation: boolean + ) { + try { + const fileName = `${isDocumentation ? "doc" : "file"}-${checksum}.${extension.toLowerCase()}`; + console.log(`Fetching file "${fileName}" from storage...`); + const file = await this.getFile(fileName) + console.log(`Got a ${file.byteLength / (1024 * 1024)}MiB file from the storage.`) + return file; + } catch (err) { + console.log("File not found.") + throw new NotFoundException(err); + } + } + /** * Processes and saves a new file after validating its properties and conditions. *