refactor: rabbitmq file upload process
This commit is contained in:
parent
732a4b3989
commit
2c4d3846f1
4 changed files with 398 additions and 427 deletions
|
|
@ -1,24 +1,33 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
FormField,
|
||||
Get,
|
||||
Patch,
|
||||
Path,
|
||||
Post,
|
||||
Request,
|
||||
Response,
|
||||
Route,
|
||||
Security,
|
||||
SuccessResponse,
|
||||
Tags,
|
||||
UploadedFile,
|
||||
} from "tsoa";
|
||||
|
||||
import esClient from "../elasticsearch";
|
||||
import minioClient from "../storage";
|
||||
import minioClient from "../minio";
|
||||
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { pathExist } from "../utils/minio";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { EhrFile } from "../interfaces/ehr-fs";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
|
||||
import { copyCond, pathExist } from "../utils/minio";
|
||||
|
||||
const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
|
||||
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
|
||||
|
||||
if (!DEFAULT_BUCKET) throw Error("Default MinIO bucket must be specified.");
|
||||
if (!DEFAULT_INDEX) throw Error("Default ElasticSearch index must be specified.");
|
||||
|
||||
@Route(
|
||||
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/subfolder/{subFolderName}/file",
|
||||
|
|
@ -26,109 +35,98 @@ import { EhrFile } from "../interfaces/ehr-fs";
|
|||
export class SubFolderFileController extends Controller {
|
||||
@Post("/")
|
||||
@Tags("SubFolder File")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.CREATED)
|
||||
@Security("bearerAuth", ["admin"])
|
||||
@Response(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ตำแหน่งที่ระบุไม่พบ กรุณาเตรียมตำแหน่งที่ต้องการก่อนดำเนินการ",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
|
||||
public async uploadFile(
|
||||
@Request() request: { user: { preferred_username: string } },
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@FormField() title: string,
|
||||
@FormField() description: string,
|
||||
@FormField() keyword: string,
|
||||
@FormField() category: string,
|
||||
@Body()
|
||||
body: {
|
||||
file: string;
|
||||
title: string;
|
||||
description: string;
|
||||
category: string;
|
||||
keyword: string;
|
||||
},
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
@Path() folderName: string,
|
||||
@Path() subFolderName: string,
|
||||
) {
|
||||
const filename = Buffer.from(file.originalname, "latin1").toString("utf-8");
|
||||
const pathname = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${filename}`;
|
||||
const pathname = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${body.file}`;
|
||||
|
||||
if (!(await pathExist(`${cabinetName}/${drawerName}/${folderName}/${subFolderName}`))) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.PRECONDITION_FAILED,
|
||||
"Cabinet, drawer, folder or subfolder cannot be found.",
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ตำแหน่งที่ระบุไม่พบ กรุณาเตรียมตำแหน่งที่ต้องการก่อนดำเนินการ",
|
||||
);
|
||||
}
|
||||
|
||||
const info = await minioClient
|
||||
.putObject("ehr", pathname, file.buffer, file.size, {
|
||||
"Content-Type": file.mimetype,
|
||||
createdAt: new Date().toISOString(),
|
||||
createdBy: request.user.preferred_username,
|
||||
const result = await esClient
|
||||
.search<EhrFile & { attachment?: Record<string, unknown> }>({
|
||||
index: DEFAULT_INDEX!,
|
||||
query: { match: { pathname } },
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
|
||||
if (!info) throw new Error("Object storage error occured.");
|
||||
// pathname is unique and should not have multiple record with same path
|
||||
if (result && result.hits.hits.length > 0 && result.hits.hits[0]._source) {
|
||||
await esClient
|
||||
.delete({
|
||||
index: DEFAULT_INDEX!,
|
||||
id: result.hits.hits[0]._id,
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
}
|
||||
|
||||
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
query: {
|
||||
match: {
|
||||
pathname: pathname,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const exist = search.hits.hits.find((v) => v._source?.pathname === pathname);
|
||||
const rec = result ? result.hits.hits[0]._source : false;
|
||||
|
||||
const metadata: Partial<EhrFile> = {
|
||||
pathname,
|
||||
fileName: filename,
|
||||
fileSize: file.size,
|
||||
fileType: file.mimetype,
|
||||
title: title,
|
||||
description: description,
|
||||
category: category.split(","),
|
||||
keyword: keyword.split(","),
|
||||
fileName: body.file,
|
||||
fileSize: 0,
|
||||
fileType: "",
|
||||
title: body.title,
|
||||
description: body.description,
|
||||
category: body.category.split(","),
|
||||
keyword: body.keyword.split(","),
|
||||
upload: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
createdBy: rec ? rec.createdBy : "n/a",
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username ?? "n/a",
|
||||
};
|
||||
|
||||
if (!exist) {
|
||||
await esClient.index({
|
||||
pipeline: "attachment",
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
document: {
|
||||
data: Buffer.from(file.buffer).toString("base64"),
|
||||
createdAt: new Date().toISOString(),
|
||||
createdBy: request.user.preferred_username,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username,
|
||||
...metadata,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await esClient.delete({ index: exist._index, id: exist._id });
|
||||
await esClient.index({
|
||||
pipeline: "attachment",
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
document: {
|
||||
data: Buffer.from(file.buffer).toString("base64"),
|
||||
createdAt: exist._source?.createdAt,
|
||||
createdBy: exist._source?.createdBy,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username,
|
||||
...metadata,
|
||||
},
|
||||
});
|
||||
}
|
||||
await esClient.index({
|
||||
index: DEFAULT_INDEX!,
|
||||
document: metadata,
|
||||
});
|
||||
|
||||
return this.setStatus(HttpStatusCode.CREATED);
|
||||
return {
|
||||
...body,
|
||||
createdAt: metadata.createdAt,
|
||||
createdBy: metadata.createdBy,
|
||||
updatedAt: metadata.updatedAt,
|
||||
updatedBy: metadata.updatedBy,
|
||||
upload: await minioClient.presignedPutObject(DEFAULT_BUCKET!, pathname),
|
||||
};
|
||||
}
|
||||
|
||||
@Get("/")
|
||||
@Tags("SubFolder File")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
public async getFile(
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
@Path() folderName: string,
|
||||
@Path() subFolderName: string,
|
||||
) {
|
||||
const search = await esClient.search<
|
||||
EhrFile & {
|
||||
attachment: Record<string, string>;
|
||||
}
|
||||
>({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
): Promise<EhrFile[]> {
|
||||
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: DEFAULT_INDEX!,
|
||||
query: {
|
||||
prefix: {
|
||||
pathname: `${cabinetName}/${drawerName}/${folderName}/${subFolderName}`,
|
||||
|
|
@ -136,24 +134,23 @@ export class SubFolderFileController extends Controller {
|
|||
},
|
||||
});
|
||||
|
||||
// Use flatMap for return type only. Filter does not change type after filter out undefined or null
|
||||
const records = search.hits.hits
|
||||
.map((v) => {
|
||||
if (!v._source) return;
|
||||
|
||||
const { attachment, ...rest } = v._source;
|
||||
|
||||
return rest;
|
||||
if (v._source) {
|
||||
const { attachment, ...rest } = v._source;
|
||||
return rest satisfies EhrFile;
|
||||
}
|
||||
})
|
||||
.flatMap((v) => (v ? [v] : []));
|
||||
.filter((v: EhrFile | undefined): v is EhrFile => !!v);
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
@Patch("/{fileName}")
|
||||
@Tags("SubFolder File")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
@Security("bearerAuth", ["admin"])
|
||||
@Response(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างแฟ้ม")
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
public async updateFile(
|
||||
@Request() request: { user: { preferred_username: string } },
|
||||
@Path() cabinetName: string,
|
||||
|
|
@ -161,92 +158,98 @@ export class SubFolderFileController extends Controller {
|
|||
@Path() folderName: string,
|
||||
@Path() subFolderName: string,
|
||||
@Path() fileName: string,
|
||||
@UploadedFile() file?: Express.Multer.File,
|
||||
@FormField() title?: string,
|
||||
@FormField() description?: string,
|
||||
@FormField() keyword?: string,
|
||||
@FormField() category?: string,
|
||||
@Body()
|
||||
body: {
|
||||
file?: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
category?: string;
|
||||
keyword?: string;
|
||||
},
|
||||
) {
|
||||
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
query: {
|
||||
match: {
|
||||
pathname: `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
||||
},
|
||||
},
|
||||
});
|
||||
const basePath = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/`;
|
||||
const pathname = `${basePath}${fileName}`;
|
||||
|
||||
if (search && search.hits.hits.length === 0) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found");
|
||||
if (
|
||||
!Boolean(
|
||||
await minioClient.statObject(DEFAULT_BUCKET!, `${pathname}`).catch((e) => {
|
||||
if (e.code === "NotFound") return false;
|
||||
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
|
||||
}),
|
||||
)
|
||||
) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบไฟล์");
|
||||
}
|
||||
|
||||
const data = search.hits.hits[0];
|
||||
// assume user will probably replace file by re-upload but maybe just rename
|
||||
if (body.file) {
|
||||
const destination = `${basePath}${body.file}`;
|
||||
const source = `/${DEFAULT_BUCKET}/${basePath}${fileName}`;
|
||||
const copy = await minioClient.copyObject(DEFAULT_BUCKET!, destination, source, copyCond);
|
||||
|
||||
if (!file) {
|
||||
const esResult = await esClient
|
||||
.update({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
id: data._id,
|
||||
doc: {
|
||||
title,
|
||||
description,
|
||||
keyword: keyword?.split(","),
|
||||
category: category?.split(","),
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username,
|
||||
},
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
if (copy) {
|
||||
const search = await esClient
|
||||
.search<EhrFile & { attachment?: Record<string, unknown> }>({
|
||||
index: DEFAULT_INDEX!,
|
||||
query: { match: { pathname } },
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
|
||||
if (!esResult) throw new Error("An error occured, cannot perform this action.");
|
||||
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
||||
const { _index: index, _id: id } = search.hits.hits[0];
|
||||
await esClient
|
||||
.update({
|
||||
index,
|
||||
id,
|
||||
doc: {
|
||||
pathname: destination,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username ?? "n/a",
|
||||
},
|
||||
})
|
||||
.then(() => minioClient.removeObject(DEFAULT_BUCKET!, pathname));
|
||||
} else {
|
||||
await minioClient.removeObject(DEFAULT_BUCKET!, pathname);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const filename = Buffer.from(file.originalname, "latin1").toString("utf-8");
|
||||
const pathname = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${filename}`;
|
||||
|
||||
await minioClient.removeObject(
|
||||
"ehr",
|
||||
`${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
||||
);
|
||||
|
||||
const info = await minioClient
|
||||
.putObject("ehr", pathname, file.buffer, file.size, {
|
||||
"Content-Type": file.mimetype,
|
||||
createdAt: new Date().toISOString(),
|
||||
createdBy: request.user.preferred_username,
|
||||
const search = await esClient
|
||||
.search<EhrFile & { attachment?: Record<string, unknown> }>({
|
||||
index: DEFAULT_INDEX!,
|
||||
query: { match: { pathname } },
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
|
||||
if (!info) throw new Error("Object storage error occured.");
|
||||
|
||||
await esClient.delete({ index: data._index, id: data._id });
|
||||
await esClient.index({
|
||||
pipeline: "attachment",
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
document: {
|
||||
data: Buffer.from(file.buffer).toString("base64"),
|
||||
pathname,
|
||||
fileName: filename,
|
||||
fileSize: file.size,
|
||||
fileType: file.mimetype,
|
||||
title: title,
|
||||
description: description,
|
||||
category: category?.split(","),
|
||||
keyword: keyword?.split(","),
|
||||
createdAt: data._source?.createdAt,
|
||||
createdBy: data._source?.createdBy,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username,
|
||||
},
|
||||
});
|
||||
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
||||
const { _index: index, _id: id } = search.hits.hits[0];
|
||||
await esClient.update({
|
||||
index,
|
||||
id,
|
||||
doc: {
|
||||
...body,
|
||||
keyword: body.keyword?.split(","),
|
||||
category: body.category?.split(","),
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: request.user.preferred_username ?? "n/a",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
return body.file
|
||||
? {
|
||||
upload: await minioClient.presignedPutObject(
|
||||
DEFAULT_BUCKET!,
|
||||
`${basePath}${body.file ?? fileName}`,
|
||||
),
|
||||
}
|
||||
: this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Delete("/{fileName}")
|
||||
@Tags("SubFolder File")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
@Security("bearerAuth", ["admin"])
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
public async deleteFile(
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
|
|
@ -254,42 +257,15 @@ export class SubFolderFileController extends Controller {
|
|||
@Path() subFolderName: string,
|
||||
@Path() fileName: string,
|
||||
) {
|
||||
const search = await esClient.search<
|
||||
EhrFile & {
|
||||
attachment: Record<string, string>;
|
||||
}
|
||||
>({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
query: {
|
||||
match: {
|
||||
pathname: `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (search && search.hits.hits.length === 0) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found");
|
||||
}
|
||||
|
||||
const esResult = await esClient
|
||||
.delete({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
id: search.hits.hits[0]._id,
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
|
||||
if (!esResult) throw new Error("An error occured, cannot perform this action.");
|
||||
|
||||
await minioClient.removeObject(
|
||||
"ehr",
|
||||
`${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
||||
DEFAULT_BUCKET!,
|
||||
`${cabinetName}/${drawerName}/${folderName}/${fileName}/${subFolderName}/`,
|
||||
);
|
||||
|
||||
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Get("/{fileName}")
|
||||
@Tags("File")
|
||||
@Tags("Download")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
public async downloadFile(
|
||||
@Path() cabinetName: string,
|
||||
|
|
@ -299,7 +275,7 @@ export class SubFolderFileController extends Controller {
|
|||
@Path() fileName: string,
|
||||
) {
|
||||
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? 'ehr-index',
|
||||
index: DEFAULT_INDEX!,
|
||||
query: {
|
||||
match: {
|
||||
pathname: `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
||||
|
|
@ -311,19 +287,13 @@ export class SubFolderFileController extends Controller {
|
|||
throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found");
|
||||
}
|
||||
|
||||
const data = search.hits.hits[0]._source;
|
||||
|
||||
if (!data) {
|
||||
throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "Found data but no info.");
|
||||
}
|
||||
|
||||
const { attachment, ...rest } = data;
|
||||
const { attachment, ...rest } = search.hits.hits[0]._source!;
|
||||
|
||||
return {
|
||||
...rest,
|
||||
download: await minioClient.presignedGetObject(
|
||||
"ehr",
|
||||
`${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
||||
DEFAULT_BUCKET!,
|
||||
`${cabinetName}/${drawerName}/${folderName}/${fileName}`,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue