feat: file event emit

This commit is contained in:
Methapon2001 2023-12-10 10:16:01 +07:00
parent 7c9579bb54
commit 171aaf144c
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
2 changed files with 70 additions and 10 deletions

View file

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

View file

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