From 3d2789857cdbec048fcff66bfbf80cb09684191e Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 3 Nov 2023 01:25:37 +0100 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartfile.classes.smartfile.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index b231469..8fb3172 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: '10.0.34', + version: '10.0.35', description: 'offers smart ways to work with files in nodejs' } diff --git a/ts/smartfile.classes.smartfile.ts b/ts/smartfile.classes.smartfile.ts index 5334e4d..6e07bed 100644 --- a/ts/smartfile.classes.smartfile.ts +++ b/ts/smartfile.classes.smartfile.ts @@ -65,6 +65,33 @@ export class Smartfile extends plugins.smartjson.Smartjson { return new Smartfile(plugins.smartjson.parse(foldedJsonArg)); } + /** + * creates a Smartfile from a ReadableStream + * @param stream a readable stream that provides file content + * @param filePath the file path to associate with the content + * @param baseArg the base path to use for the file + */ + public static async fromStream( + stream: plugins.stream.Readable, + filePath: string, + baseArg: string = process.cwd() + ): Promise { + return new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + stream.on('data', (chunk) => chunks.push(Buffer.from(chunk))); + stream.on('error', (error) => reject(error)); + stream.on('end', () => { + const contentBuffer = Buffer.concat(chunks); + const smartfile = new Smartfile({ + contentBuffer: contentBuffer, + base: baseArg, + path: plugins.path.relative(baseArg, filePath), + }); + resolve(smartfile); + }); + }); + } + // ======== // INSTANCE // ========