From 171aaf144cf4df8ef1428453fc38436f80f3c0da Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Sun, 10 Dec 2023 10:16:01 +0700 Subject: [PATCH] feat: file event emit --- .../server/src/controllers/fileController.ts | 43 ++++++++++++++++--- .../controllers/subFolderFileController.ts | 37 ++++++++++++++-- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/Services/server/src/controllers/fileController.ts b/Services/server/src/controllers/fileController.ts index 6baec35..1535974 100644 --- a/Services/server/src/controllers/fileController.ts +++ b/Services/server/src/controllers/fileController.ts @@ -23,6 +23,7 @@ import { StorageFile } from "../interfaces/storage-fs"; import HttpError from "../interfaces/http-error"; import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio"; +import { getInstance } from "../lib/websocket"; const DEFAULT_BUCKET = process.env.MINIO_BUCKET; const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX; @@ -200,6 +201,9 @@ export class FileController extends Controller { refresh: "wait_for", }); + const io = getInstance(); + io?.emit("FileUploadRequest", metadata); + return { ...body, createdAt: metadata.createdAt, @@ -288,6 +292,10 @@ export class FileController extends Controller { if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) { const { _index: index, _id: id } = search.hits.hits[0]; + const meta = { + updatedAt: new Date().toISOString(), + updatedBy: request.user.preferred_username ?? "n/a", + }; await esClient .update({ index, @@ -297,12 +305,24 @@ export class FileController extends Controller { pathname: destination, path: basePath, fileName: replaceIllegalChars(file), - updatedAt: new Date().toISOString(), - updatedBy: request.user.preferred_username ?? "n/a", + ...meta, }, refresh: "wait_for", }) .then(() => minioClient.removeObject(DEFAULT_BUCKET!, pathname)); + + const io = getInstance(); + io?.emit("FileUpdateMove", { + from: search.hits.hits[0]._source, + to: { + ...search.hits.hits[0]._source, + ...metadata, + pathname: destination, + path: basePath, + fileName: replaceIllegalChars(file), + ...meta, + }, + }); } else { await minioClient.removeObject(DEFAULT_BUCKET!, pathname); } @@ -317,16 +337,25 @@ export class FileController extends Controller { if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) { const { _index: index, _id: id } = search.hits.hits[0]; + const meta = { + updatedAt: new Date().toISOString(), + updatedBy: request.user.preferred_username ?? "n/a", + }; await esClient.update({ index, id, - doc: { - ...metadata, - updatedAt: new Date().toISOString(), - updatedBy: request.user.preferred_username ?? "n/a", - }, + doc: { ...metadata, ...meta }, refresh: "wait_for", }); + + const updated: StorageFile = { + ...search.hits.hits[0]._source, + ...metadata, + ...meta, + }; + + const io = getInstance(); + io?.emit("FileUpdate", updated); } } diff --git a/Services/server/src/controllers/subFolderFileController.ts b/Services/server/src/controllers/subFolderFileController.ts index 717a87e..b66aca3 100644 --- a/Services/server/src/controllers/subFolderFileController.ts +++ b/Services/server/src/controllers/subFolderFileController.ts @@ -23,6 +23,7 @@ import { StorageFile } from "../interfaces/storage-fs"; import HttpError from "../interfaces/http-error"; import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio"; +import { getInstance } from "../lib/websocket"; const DEFAULT_BUCKET = process.env.MINIO_BUCKET; const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX; @@ -206,6 +207,9 @@ export class SubFolderFileController extends Controller { refresh: "wait_for", }); + const io = getInstance(); + io?.emit("FileUploadRequest", metadata); + return { ...body, createdAt: metadata.createdAt, @@ -292,7 +296,10 @@ export class SubFolderFileController extends Controller { query: { match: { pathname } }, }) .catch((e) => console.error(e)); - + const meta = { + updatedAt: new Date().toISOString(), + updatedBy: request.user.preferred_username ?? "n/a", + }; if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) { const { _index: index, _id: id } = search.hits.hits[0]; await esClient @@ -304,12 +311,24 @@ export class SubFolderFileController extends Controller { pathname: destination, path: basePath, fileName: replaceIllegalChars(file), - updatedAt: new Date().toISOString(), - updatedBy: request.user.preferred_username ?? "n/a", + ...meta, }, refresh: "wait_for", }) .then(() => minioClient.removeObject(DEFAULT_BUCKET!, pathname)); + + const io = getInstance(); + io?.emit("FileUpdateMove", { + from: search.hits.hits[0]._source, + to: { + ...search.hits.hits[0]._source, + ...metadata, + pathname: destination, + path: basePath, + fileName: replaceIllegalChars(file), + ...meta, + }, + }); } else { await minioClient.removeObject(DEFAULT_BUCKET!, pathname); } @@ -324,6 +343,10 @@ export class SubFolderFileController extends Controller { if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) { const { _index: index, _id: id } = search.hits.hits[0]; + const meta = { + updatedAt: new Date().toISOString(), + updatedBy: request.user.preferred_username ?? "n/a", + }; await esClient.update({ index, id, @@ -334,6 +357,14 @@ export class SubFolderFileController extends Controller { }, refresh: "wait_for", }); + const updated: StorageFile = { + ...search.hits.hits[0]._source, + ...metadata, + ...meta, + }; + + const io = getInstance(); + io?.emit("FileUpdate", updated); } }