feat: socketio event

This commit is contained in:
Methapon2001 2023-12-07 17:25:56 +07:00
parent 0a1265b78c
commit 25752dac19
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
9 changed files with 200 additions and 8 deletions

View file

@ -1,6 +1,7 @@
import { StorageFile } from "../interfaces/storage-fs";
import esClient from "../elasticsearch";
import minioClient from "../minio";
import { getInstance } from "../lib/websocket";
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
@ -27,7 +28,7 @@ export async function handler(key: string, event: string): Promise<boolean> {
cachedBuffer[key] = buffer;
} catch (e: any) {
if (e.code === "NoSuchKey") {
console.info(`[AMQ] Key: ${key} received but cannot be found.`)
console.info(`[AMQ] Key: ${key} received but cannot be found.`);
delete cachedBuffer[key];
delete cachedMetadata[key];
await ensureDelete(pathname);
@ -43,7 +44,7 @@ export async function handler(key: string, event: string): Promise<boolean> {
const rec = await popInfo(pathname);
console.info(`[AMQ] Key: ${key} - ${rec ?? 'Not Found.'}`)
console.info(`[AMQ] Key: ${key} - ${rec ?? "Not Found."}`);
const result = rec
? await handleFoundRecord(rec, cachedBuffer[key], cachedMetadata[key])
@ -94,6 +95,10 @@ async function ensureDelete(pathname: string) {
conflicts: "proceed",
})
.catch((e) => console.error(e));
const io = getInstance();
io?.send("FileDelete", { pathname });
return true;
}
@ -132,12 +137,16 @@ async function handleNotFoundRecord(
pipeline: "attachment",
index: DEFAULT_INDEX!,
document: { data: base64, ...metadata },
refresh: "wait_for",
})
.catch((e) => console.error(e));
if (result) return true;
if (!result) return false;
return false;
const io = getInstance();
io?.send("FileUpdate", metadata);
return true;
}
async function handleFoundRecord(
@ -154,10 +163,14 @@ async function handleFoundRecord(
pipeline: "attachment",
index: DEFAULT_INDEX!,
document: { data: Buffer.from(buffer).toString("base64"), ...metadata },
refresh: "wait_for",
})
.catch((e) => console.error(e));
if (result) return true;
if (!result) return false;
return false;
const io = getInstance();
io?.send("FileUpdate", metadata);
return true;
}