From 5985a111b60dfd0dca80d8b19f906656582bd004 Mon Sep 17 00:00:00 2001 From: Rolands Date: Wed, 22 Feb 2023 13:30:46 +0200 Subject: [PATCH] upload download fixes --- core/core-session.ts | 2 +- core/core.ts | 8 ++++++-- core/fs-utils.ts | 40 +++++++++++++++++++++++----------------- core/types.d.ts | 1 + 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/core/core-session.ts b/core/core-session.ts index 207599a..0a5043c 100644 --- a/core/core-session.ts +++ b/core/core-session.ts @@ -55,7 +55,7 @@ export class SocioSession extends LogHandler { if(this.#destroyed) return; //if this session is marked for destruction if (data.length < 1) throw new E('Not enough arguments to send data! kind;data:', kind, data); //the first argument must always be the data to send. Other params may be objects with aditional keys to be added in the future this.#ws.send(JSON.stringify(Object.assign({}, { kind: kind, data: data[0] }, ...data.slice(1)))); - this.HandleInfo('sent:', kind, ...data); + this.HandleInfo('sent:', kind, ...(kind != 'RECV_FILES' ? data : [])); this.last_seen_now(); } diff --git a/core/core.ts b/core/core.ts index cf75577..f1a5d7d 100644 --- a/core/core.ts +++ b/core/core.ts @@ -411,8 +411,12 @@ export class SocioServer extends LogHandler { } break; case 'GET_FILES': - if (this.#lifecycle_hooks?.file_download) - client.Send('RECV_FILES', { id: data.id, files: await this.#lifecycle_hooks.file_download(client, data?.data), result:1 }); + if (this.#lifecycle_hooks?.file_download){ + const response = await this.#lifecycle_hooks.file_download(client, data?.data); + if (!response?.result) + this.HandleError(new E('file_download hook returned unsuccessful result.', response?.error)); + client.Send('RECV_FILES', { id: data.id, files: response.files, result: response?.result }); + } else { this.HandleError('file_download hook not registered. [#no-file_download-hook]'); client.Send('RES', { id: data.id, result: 0 }); diff --git a/core/fs-utils.ts b/core/fs-utils.ts index 8e31a70..1c0b6a7 100644 --- a/core/fs-utils.ts +++ b/core/fs-utils.ts @@ -3,24 +3,30 @@ import b64 from 'base64-js' import { default as os_path } from "path"; //types -import type { SocioFiles } from './types.js'; +import type { SocioFiles, FS_Util_Response } from './types.js'; -export function SaveFilesToDiskPath(path_array: string[], files: SocioFiles){ - if(!path_array || !files) return; - for (const [filename, file_data] of Object.entries(files)) { - const file_path = os_path.join(...path_array, filename); - const bin = b64.toByteArray(file_data.bin); - fs.writeFileSync(file_path, bin, {flag:'w'}); - } +export function SaveFilesToDiskPath(path_array: string[], files: SocioFiles): FS_Util_Response{ + try{ + if (!path_array || !files) return {result: 0, error:'function arguments are falsy'}; + for (const [filename, file_data] of Object.entries(files)) { + const file_path = os_path.join(...path_array, filename); + const bin = b64.toByteArray(file_data.bin); + fs.writeFileSync(file_path, bin, { flag: 'w' }); + } + return { result: 1 }; + } catch (e) { return { result: 0, error: e }; } } -export function ReadFilesFromDisk(file_paths: string[]) { - if (!file_paths) return; - const files: SocioFiles = {}; - for(const path in file_paths){ - const filename = os_path.basename(path); - const file = fs.readFileSync(path); - const file_base64_string = b64.fromByteArray(file); - files[filename] = { meta: { size: file.byteLength }, bin: file_base64_string } - } +export function ReadFilesFromDisk(file_paths: string[]): FS_Util_Response { + try{ + if (!file_paths?.length) return { result: 0, error:'no file_paths provided' }; + const files: SocioFiles = {}; + for (const path of file_paths) { + const filename = os_path.basename(path); + const file = fs.readFileSync(path); + const file_base64_string = b64.fromByteArray(file); + files[filename] = { meta: { size: file.byteLength }, bin: file_base64_string } + } + return { result: 1, files }; + } catch (e:any) { return { result: 0, error:e }; } } \ No newline at end of file diff --git a/core/types.d.ts b/core/types.d.ts index 3da9fea..51f0368 100644 --- a/core/types.d.ts +++ b/core/types.d.ts @@ -11,6 +11,7 @@ export type PropAssigner = (key: PropKey, new_val:PropValue) => boolean; //misc export type SocioFiles = { [filename: string]: { meta: { size: number, lastModified?: number, type?: string }, bin: Base64String } }; //bin is a base64 string of the bytes of the raw file export type QueryMarker = 'socio' | 'auth' | 'perm'; +export type FS_Util_Response = { result: Bit, error?: string | Error | E | object | any, files?: SocioFiles } //msg kinds export type CoreMessageKind = 'SUB' | 'UNSUB' | 'SQL' | 'PING' | 'AUTH' | 'GET_PERM' | 'PROP_SUB' | 'PROP_UNSUB' | 'PROP_GET' | 'PROP_SET' | 'SERV' | 'ADMIN' | 'RECON' | 'UP_FILES' | 'GET_FILES';