From 14adec5ba3652e2f04f076d6055cb9c02de25165 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 7 Jun 2024 17:13:07 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/classes.smartfile.ts | 7 +++++++ ts/classes.streamfile.ts | 25 +++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 0cfb3e0..65fae3e 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfile', - version: '11.0.19', + version: '11.0.20', description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.' } diff --git a/ts/classes.smartfile.ts b/ts/classes.smartfile.ts index 961eb3b..f84cc8e 100644 --- a/ts/classes.smartfile.ts +++ b/ts/classes.smartfile.ts @@ -306,4 +306,11 @@ export class SmartFile extends plugins.smartjson.Smartjson { stream.push(null); // Push null to signify the end of the stream (EOF) return stream; } + + /** + * Returns the size of the file in bytes + */ + public async getSize(): Promise { + return this.contentBuffer.length; + } } diff --git a/ts/classes.streamfile.ts b/ts/classes.streamfile.ts index 1279d80..a284878 100644 --- a/ts/classes.streamfile.ts +++ b/ts/classes.streamfile.ts @@ -13,9 +13,13 @@ export class StreamFile { // STATIC public static async fromPath(filePath: string): Promise { - const streamSource: TStreamSource = async (stremFileArg) => smartfileFsStream.createReadStream(filePath); + const streamSource: TStreamSource = async (streamFileArg) => smartfileFsStream.createReadStream(filePath); const streamFile = new StreamFile(streamSource, filePath); streamFile.multiUse = true; + streamFile.byteLengthComputeFunction = async () => { + const stats = await smartfileFs.stat(filePath); + return stats.size; + } return streamFile; } @@ -23,6 +27,10 @@ export class StreamFile { const streamSource: TStreamSource = async (streamFileArg) => plugins.smartrequest.getStream(url); // Replace with actual plugin method const streamFile = new StreamFile(streamSource); streamFile.multiUse = true; + streamFile.byteLengthComputeFunction = async () => { + const response = await plugins.smartrequest.getBinary(url); // TODO: switch to future .getBinaryByteLength() + return response.body.length; + } return streamFile; } @@ -35,6 +43,7 @@ export class StreamFile { }; const streamFile = new StreamFile(streamSource, relativeFilePath); streamFile.multiUse = true; + streamFile.byteLengthComputeFunction = async () => buffer.length; return streamFile; } @@ -86,6 +95,7 @@ export class StreamFile { private cachedStreamBuffer?: Buffer; public multiUse: boolean; public used: boolean = false; + public byteLengthComputeFunction: () => Promise; private constructor(streamSource: TStreamSource, relativeFilePath?: string) { this.streamSource = streamSource; @@ -150,4 +160,15 @@ export class StreamFile { const contentBuffer = await this.getContentAsBuffer(); return contentBuffer.toString(formatArg); } -} + + /** + * Returns the size of the file content in bytes. + */ + public async getSize(): Promise { + if (this.byteLengthComputeFunction) { + return this.byteLengthComputeFunction(); + } else { + return null; + } + } +} \ No newline at end of file