mirror of
https://github.com/Rolands-Laucis/Socio.git
synced 2026-05-15 14:15:57 -06:00
file upload working
This commit is contained in:
parent
1b90bf624a
commit
96a68f2986
4 changed files with 16 additions and 11 deletions
|
|
@ -838,7 +838,7 @@ export function ParseSocioFiles(files: SocioFiles) {
|
|||
// Handle both Map (original) and plain object (MessagePack converts Maps to objects)
|
||||
const entries = files instanceof Map ? files.entries() : Object.entries(files);
|
||||
for (const [filename, file_data] of entries)
|
||||
files_array.push(new File([(typeof file_data.bin === 'string' ? SocioFileBase64ToUint8Array(file_data.bin) : pako.inflate(file_data.bin)) as BlobPart], filename, { type: file_data.meta.type, lastModified: file_data.meta.lastModified }));
|
||||
files_array.push(new File([pako.inflate(file_data.bin as Uint8Array) as BlobPart], filename, { type: file_data.meta.type, lastModified: file_data.meta.lastModified }));
|
||||
return files_array;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -263,7 +263,7 @@ export class SocioServer extends LogHandler {
|
|||
}
|
||||
|
||||
if (kind !== ServerMessageKind.OK) //this
|
||||
this.HandleInfo(`recv: [${ServerMessageKind[kind]}] from [${client.name ? client.name + ' | ' : ''}${client_id}]`, kind != ServerMessageKind.UP_FILES ? data : `File count: ${(data as S_UP_FILES_data).files?.size}`);
|
||||
this.HandleInfo(`recv: [${ServerMessageKind[kind]}] from [${client.name ? client.name + ' | ' : ''}${client_id}]`, kind != ServerMessageKind.UP_FILES ? data : `File count: ${(data as S_UP_FILES_data).files instanceof Map ? (data as S_UP_FILES_data).files.size : Object.keys((data as S_UP_FILES_data).files || {}).length}`);
|
||||
|
||||
//let the developer handle the msg
|
||||
if (this.lifecycle_hooks.msg)
|
||||
|
|
@ -559,14 +559,19 @@ export class SocioServer extends LogHandler {
|
|||
|
||||
//recon procedure
|
||||
const old_client = this.#sessions.get(old_c_id) as SocioSession;
|
||||
this.ReconnectClientSession(client, old_client, data.id as id);
|
||||
this.HandleInfo(`RECON | old id: ${old_c_id} -> new id: ${client.id}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ServerMessageKind.UP_FILES: {
|
||||
if (this.lifecycle_hooks?.file_upload)
|
||||
client.Send(ClientMessageKind.RES, { id: data.id, result: { success: await this.lifecycle_hooks.file_upload(client, (data as S_UP_FILES_data)?.files, (data as S_UP_FILES_data)?.data) ? 1 : 0 } } as C_RES_data);
|
||||
if (this.lifecycle_hooks?.file_upload) {
|
||||
let files = (data as S_UP_FILES_data)?.files;
|
||||
// MessagePack deserializes Maps as objects, so we need to convert back to Map if needed
|
||||
if (files && !(files instanceof Map)) {
|
||||
files = new Map(Object.entries(files));
|
||||
}
|
||||
const success = await this.lifecycle_hooks.file_upload(client, files, (data as S_UP_FILES_data)?.data) ? 1 : 0;
|
||||
client.Send(ClientMessageKind.RES, { id: data.id, result: { success, res: success } } as C_RES_data);
|
||||
}
|
||||
else {
|
||||
const error = 'file_upload hook not registered. [#no-file_upload-hook]';
|
||||
this.HandleError(error);
|
||||
|
|
|
|||
|
|
@ -14,16 +14,16 @@ export function SaveFilesToDiskPath(string_array_path: string[], files: SocioFil
|
|||
const entries = files instanceof Map ? files.entries() : Object.entries(files);
|
||||
for (const [filename, file_data] of entries) {
|
||||
const file_path = os_path.join(...string_array_path, filename);
|
||||
console.log('DEBUG file_data.bin:', typeof file_data.bin, file_data.bin instanceof Uint8Array, file_data.bin instanceof Buffer, Array.isArray(file_data.bin));
|
||||
const bin = pako.inflate(file_data.bin); // Decompress binary data
|
||||
?pako.inflate(Buffer.from(file_data.bin, 'base64').buffer as ArrayBuffer) // Legacy Base64 format
|
||||
: pako.inflate(file_data.bin); // MessagePack sends raw compressed binary (Uint8Array)
|
||||
// console.log('DEBUG file_data.bin:', typeof file_data.bin, file_data.bin instanceof Uint8Array, file_data.bin instanceof Buffer, Array.isArray(file_data.bin));
|
||||
// MessagePack sends raw compressed binary (Uint8Array)
|
||||
const bin = pako.inflate(file_data.bin as Uint8Array);
|
||||
fs.writeFileSync(file_path, bin, { flag: 'w' });
|
||||
}
|
||||
res({ result: 1 });
|
||||
} catch (e) { rej({ result: 0, error: e }); }
|
||||
})
|
||||
}
|
||||
|
||||
export function ReadFilesFromDisk(file_paths: string[]): Promise<FS_Util_Response> {
|
||||
return new Promise((res, rej) => {
|
||||
try {
|
||||
|
|
|
|||
2
core/types.d.ts
vendored
2
core/types.d.ts
vendored
|
|
@ -18,7 +18,7 @@ type PropAssigner = (key: PropKey, new_val: PropValue, sender_client?: SocioSess
|
|||
type PropOpts = { client_writable?: boolean, send_as_diff?: boolean, emit_to_sender?: boolean, observationaly_temporary?: boolean };
|
||||
|
||||
//misc
|
||||
type SocioFiles = Map<string, { meta: { size: number, lastModified?: number, type?: string }, bin: Uint8Array | Base64String }> | { [filename: string]: { meta: { size: number, lastModified?: number, type?: string }, bin: Uint8Array | Base64String } }; //bin is either raw compressed binary (MessagePack) or base64 string (legacy), files can be Map or object
|
||||
type SocioFiles = Map<string, { meta: { size: number, lastModified?: number, type?: string }, bin: Uint8Array }> | { [filename: string]: { meta: { size: number, lastModified?: number, type?: string }, bin: Uint8Array } }; //bin is raw compressed binary (MessagePack), files can be Map or object
|
||||
type QueryMarker = 'socio' | 'auth' | 'perm';
|
||||
type FS_Util_Response = { result: Bit, error?: string | Error | E | object | any, files?: SocioFiles }
|
||||
type LoggingOpts = { logging?: LoggerOptions };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue