upload download fixes

This commit is contained in:
Rolands 2023-02-22 13:30:46 +02:00
parent d2241e8fd5
commit 5985a111b6
4 changed files with 31 additions and 20 deletions

View file

@ -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();
}

View file

@ -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 });

View file

@ -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 }; }
}

1
core/types.d.ts vendored
View file

@ -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';