feat: file event emit
This commit is contained in:
parent
7c9579bb54
commit
171aaf144c
2 changed files with 70 additions and 10 deletions
|
|
@ -23,6 +23,7 @@ import { StorageFile } from "../interfaces/storage-fs";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
|
|
||||||
import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio";
|
import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio";
|
||||||
|
import { getInstance } from "../lib/websocket";
|
||||||
|
|
||||||
const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
|
const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
|
||||||
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
|
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
|
||||||
|
|
@ -200,6 +201,9 @@ export class FileController extends Controller {
|
||||||
refresh: "wait_for",
|
refresh: "wait_for",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const io = getInstance();
|
||||||
|
io?.emit("FileUploadRequest", metadata);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...body,
|
...body,
|
||||||
createdAt: metadata.createdAt,
|
createdAt: metadata.createdAt,
|
||||||
|
|
@ -288,6 +292,10 @@ export class FileController extends Controller {
|
||||||
|
|
||||||
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
||||||
const { _index: index, _id: id } = search.hits.hits[0];
|
const { _index: index, _id: id } = search.hits.hits[0];
|
||||||
|
const meta = {
|
||||||
|
updatedAt: new Date().toISOString(),
|
||||||
|
updatedBy: request.user.preferred_username ?? "n/a",
|
||||||
|
};
|
||||||
await esClient
|
await esClient
|
||||||
.update({
|
.update({
|
||||||
index,
|
index,
|
||||||
|
|
@ -297,12 +305,24 @@ export class FileController extends Controller {
|
||||||
pathname: destination,
|
pathname: destination,
|
||||||
path: basePath,
|
path: basePath,
|
||||||
fileName: replaceIllegalChars(file),
|
fileName: replaceIllegalChars(file),
|
||||||
updatedAt: new Date().toISOString(),
|
...meta,
|
||||||
updatedBy: request.user.preferred_username ?? "n/a",
|
|
||||||
},
|
},
|
||||||
refresh: "wait_for",
|
refresh: "wait_for",
|
||||||
})
|
})
|
||||||
.then(() => minioClient.removeObject(DEFAULT_BUCKET!, pathname));
|
.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 {
|
} else {
|
||||||
await minioClient.removeObject(DEFAULT_BUCKET!, pathname);
|
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) {
|
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
||||||
const { _index: index, _id: id } = search.hits.hits[0];
|
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({
|
await esClient.update({
|
||||||
index,
|
index,
|
||||||
id,
|
id,
|
||||||
doc: {
|
doc: { ...metadata, ...meta },
|
||||||
...metadata,
|
|
||||||
updatedAt: new Date().toISOString(),
|
|
||||||
updatedBy: request.user.preferred_username ?? "n/a",
|
|
||||||
},
|
|
||||||
refresh: "wait_for",
|
refresh: "wait_for",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const updated: StorageFile = {
|
||||||
|
...search.hits.hits[0]._source,
|
||||||
|
...metadata,
|
||||||
|
...meta,
|
||||||
|
};
|
||||||
|
|
||||||
|
const io = getInstance();
|
||||||
|
io?.emit("FileUpdate", updated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import { StorageFile } from "../interfaces/storage-fs";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
|
|
||||||
import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio";
|
import { copyCond, pathExist, replaceIllegalChars } from "../utils/minio";
|
||||||
|
import { getInstance } from "../lib/websocket";
|
||||||
|
|
||||||
const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
|
const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
|
||||||
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
|
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
|
||||||
|
|
@ -206,6 +207,9 @@ export class SubFolderFileController extends Controller {
|
||||||
refresh: "wait_for",
|
refresh: "wait_for",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const io = getInstance();
|
||||||
|
io?.emit("FileUploadRequest", metadata);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...body,
|
...body,
|
||||||
createdAt: metadata.createdAt,
|
createdAt: metadata.createdAt,
|
||||||
|
|
@ -292,7 +296,10 @@ export class SubFolderFileController extends Controller {
|
||||||
query: { match: { pathname } },
|
query: { match: { pathname } },
|
||||||
})
|
})
|
||||||
.catch((e) => console.error(e));
|
.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) {
|
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
||||||
const { _index: index, _id: id } = search.hits.hits[0];
|
const { _index: index, _id: id } = search.hits.hits[0];
|
||||||
await esClient
|
await esClient
|
||||||
|
|
@ -304,12 +311,24 @@ export class SubFolderFileController extends Controller {
|
||||||
pathname: destination,
|
pathname: destination,
|
||||||
path: basePath,
|
path: basePath,
|
||||||
fileName: replaceIllegalChars(file),
|
fileName: replaceIllegalChars(file),
|
||||||
updatedAt: new Date().toISOString(),
|
...meta,
|
||||||
updatedBy: request.user.preferred_username ?? "n/a",
|
|
||||||
},
|
},
|
||||||
refresh: "wait_for",
|
refresh: "wait_for",
|
||||||
})
|
})
|
||||||
.then(() => minioClient.removeObject(DEFAULT_BUCKET!, pathname));
|
.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 {
|
} else {
|
||||||
await minioClient.removeObject(DEFAULT_BUCKET!, pathname);
|
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) {
|
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
||||||
const { _index: index, _id: id } = search.hits.hits[0];
|
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({
|
await esClient.update({
|
||||||
index,
|
index,
|
||||||
id,
|
id,
|
||||||
|
|
@ -334,6 +357,14 @@ export class SubFolderFileController extends Controller {
|
||||||
},
|
},
|
||||||
refresh: "wait_for",
|
refresh: "wait_for",
|
||||||
});
|
});
|
||||||
|
const updated: StorageFile = {
|
||||||
|
...search.hits.hits[0]._source,
|
||||||
|
...metadata,
|
||||||
|
...meta,
|
||||||
|
};
|
||||||
|
|
||||||
|
const io = getInstance();
|
||||||
|
io?.emit("FileUpdate", updated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue