From 1b47c58f12e17490c108d31161ff004892e1254f Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:30:48 +0700 Subject: [PATCH 1/4] fix: missing trailing slash --- Services/server/src/controllers/folderController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Services/server/src/controllers/folderController.ts b/Services/server/src/controllers/folderController.ts index 06bd264..e8aafdc 100644 --- a/Services/server/src/controllers/folderController.ts +++ b/Services/server/src/controllers/folderController.ts @@ -185,7 +185,7 @@ export class FolderController extends Controller { const io = getInstance(); io?.emit("EditFolder", { - from: `${cabinetName}/${drawerName}/${folderName}`, + from: `${cabinetName}/${drawerName}/${folderName}/`, to: `${cabinetName}/${drawerName}/${replaceIllegalChars(body.name)}/`, }); From de2dca7e4df98dd00b857e0a571f2151aa308728 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:37:00 +0700 Subject: [PATCH 2/4] feat: socket.io emit more data --- .../server/src/controllers/cabinetController.ts | 16 +++++++++++----- .../server/src/controllers/drawerController.ts | 16 +++++++++++----- .../server/src/controllers/folderController.ts | 16 +++++++++++----- .../src/controllers/subFolderController.ts | 15 ++++++++++----- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Services/server/src/controllers/cabinetController.ts b/Services/server/src/controllers/cabinetController.ts index 8d20f3f..c9b541c 100644 --- a/Services/server/src/controllers/cabinetController.ts +++ b/Services/server/src/controllers/cabinetController.ts @@ -78,17 +78,23 @@ export class CabinetController extends Controller { name: string; }, ) { + const meta = { + createdAt: new Date().toISOString(), + createdBy: request.user.preferred_username, + }; + const created = await minioClient - .putObject(DEFAULT_BUCKET!, `${replaceIllegalChars(body.name)}/.keep`, "", 0, { - createdAt: new Date().toISOString(), - createdBy: request.user.preferred_username, - }) + .putObject(DEFAULT_BUCKET!, `${replaceIllegalChars(body.name)}/.keep`, "", 0, meta) .catch((e) => console.error(e)); if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์"); const io = getInstance(); - io?.emit("CreateFolder", { pathname: `${replaceIllegalChars(body.name)}/` }); + io?.emit("CreateFolder", { + pathname: `${replaceIllegalChars(body.name)}/`, + name: replaceIllegalChars(body.name), + ...meta, + }); return this.setStatus(HttpStatusCode.CREATED); } diff --git a/Services/server/src/controllers/drawerController.ts b/Services/server/src/controllers/drawerController.ts index 1311697..a761fd5 100644 --- a/Services/server/src/controllers/drawerController.ts +++ b/Services/server/src/controllers/drawerController.ts @@ -93,17 +93,23 @@ export class DrawerController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก"); } + const meta = { + createdAt: new Date().toISOString(), + createdBy: request.user.preferred_username, + }; + const created = await minioClient - .putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, { - createdAt: new Date().toISOString(), - createdBy: request.user.preferred_username, - }) + .putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, meta) .catch((e) => console.error(e)); if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์"); const io = getInstance(); - io?.emit("CreateFolder", { pathname: `${basePath}${replaceIllegalChars(body.name)}/` }); + io?.emit("CreateFolder", { + pathname: `${basePath}${replaceIllegalChars(body.name)}/`, + name: replaceIllegalChars(body.name), + ...meta, + }); return this.setStatus(HttpStatusCode.CREATED); } diff --git a/Services/server/src/controllers/folderController.ts b/Services/server/src/controllers/folderController.ts index e8aafdc..358cb23 100644 --- a/Services/server/src/controllers/folderController.ts +++ b/Services/server/src/controllers/folderController.ts @@ -98,17 +98,23 @@ export class FolderController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก"); } + const meta = { + createdAt: new Date().toISOString(), + createdBy: request.user.preferred_username, + }; + const created = await minioClient - .putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, { - createdAt: new Date().toISOString(), - createdBy: request.user.preferred_username, - }) + .putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, meta) .catch((e) => console.error(e)); if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์"); const io = getInstance(); - io?.emit("CreateFolder", { pathname: `${basePath}${replaceIllegalChars(body.name)}/` }); + io?.emit("CreateFolder", { + pathname: `${basePath}${replaceIllegalChars(body.name)}/`, + name: replaceIllegalChars(body.name), + ...meta, + }); return this.setStatus(HttpStatusCode.CREATED); } diff --git a/Services/server/src/controllers/subFolderController.ts b/Services/server/src/controllers/subFolderController.ts index 1454aa6..8e02751 100644 --- a/Services/server/src/controllers/subFolderController.ts +++ b/Services/server/src/controllers/subFolderController.ts @@ -96,18 +96,23 @@ export class SubFolderController extends Controller { if (!(await pathExist(DEFAULT_BUCKET!, basePath))) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก"); } + const meta = { + createdAt: new Date().toISOString(), + createdBy: request.user.preferred_username, + }; const created = await minioClient - .putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, { - createdAt: new Date().toISOString(), - createdBy: request.user.preferred_username, - }) + .putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, meta) .catch((e) => console.error(e)); if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์"); const io = getInstance(); - io?.emit("CreateFolder", { pathname: `${basePath}${replaceIllegalChars(body.name)}/` }); + io?.emit("CreateFolder", { + pathname: `${basePath}${replaceIllegalChars(body.name)}/`, + name: replaceIllegalChars(body.name), + ...meta, + }); return this.setStatus(HttpStatusCode.CREATED); } From 7c9579bb548dc10c5505699e49e0836bda3a78a8 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Sun, 10 Dec 2023 10:07:49 +0700 Subject: [PATCH 3/4] refactor: change event name --- Services/server/src/rabbitmq/handler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Services/server/src/rabbitmq/handler.ts b/Services/server/src/rabbitmq/handler.ts index 568596c..9d8b5c0 100644 --- a/Services/server/src/rabbitmq/handler.ts +++ b/Services/server/src/rabbitmq/handler.ts @@ -144,7 +144,7 @@ async function handleNotFoundRecord( if (!result) return false; const io = getInstance(); - io?.emit("FileUpdate", metadata); + io?.emit("FileUpload", metadata); return true; } @@ -170,7 +170,7 @@ async function handleFoundRecord( if (!result) return false; const io = getInstance(); - io?.emit("FileUpdate", metadata); + io?.emit("FileUpload", metadata); return true; } 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 4/4] 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); } }