feat: upload file and elasticsearch index with metadata
This commit is contained in:
parent
e48dcb9133
commit
8fdee84b97
1 changed files with 63 additions and 15 deletions
|
|
@ -1,24 +1,72 @@
|
||||||
import { Controller, FormField, Post, Route, UploadedFile } from "tsoa";
|
import {
|
||||||
import minioClient from "../storage";
|
Controller,
|
||||||
|
FormField,
|
||||||
|
Path,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
Route,
|
||||||
|
Security,
|
||||||
|
SuccessResponse,
|
||||||
|
Tags,
|
||||||
|
UploadedFile,
|
||||||
|
} from "tsoa";
|
||||||
import esClient from "../elasticsearch";
|
import esClient from "../elasticsearch";
|
||||||
|
import minioClient from "../storage";
|
||||||
|
import HttpStatusCode from "../interfaces/http-status";
|
||||||
|
import { pathExist } from "../utils/minio";
|
||||||
|
import HttpError from "../interfaces/http-error";
|
||||||
|
|
||||||
@Route("/file")
|
@Route("/cabinet")
|
||||||
export class FileController extends Controller {
|
export class FileController extends Controller {
|
||||||
@Post("/")
|
@Post("/{cabinetName}/drawer/{drawerName}/folder/{folderName}")
|
||||||
public async uploadFile(@UploadedFile() file: Express.Multer.File, @FormField() desc: string) {
|
@Tags("File")
|
||||||
|
@Security("bearerAuth")
|
||||||
|
@SuccessResponse(HttpStatusCode.CREATED)
|
||||||
|
public async uploadFile(
|
||||||
|
@Request() request: any,
|
||||||
|
@UploadedFile() file: Express.Multer.File,
|
||||||
|
@FormField() title: string,
|
||||||
|
@FormField() description: string,
|
||||||
|
@FormField() keywords: string,
|
||||||
|
@FormField() categories: string,
|
||||||
|
@Path() cabinetName: string,
|
||||||
|
@Path() drawerName: string,
|
||||||
|
@Path() folderName: string,
|
||||||
|
) {
|
||||||
const filename = Buffer.from(file.originalname, "latin1").toString("utf-8");
|
const filename = Buffer.from(file.originalname, "latin1").toString("utf-8");
|
||||||
|
const pathname = `${cabinetName}/${drawerName}/${folderName}/${filename}`;
|
||||||
|
|
||||||
console.log(
|
if (!(await pathExist(`${cabinetName}/${drawerName}/${folderName}/`))) {
|
||||||
esClient.search({
|
throw new HttpError(
|
||||||
query: {
|
HttpStatusCode.PRECONDITION_FAILED,
|
||||||
match_all: {},
|
"Cabinet, drawer or folder cannot be 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,
|
||||||
|
})
|
||||||
|
.catch(() => new Error("Object storage error occured."));
|
||||||
|
|
||||||
|
if (info) {
|
||||||
|
await esClient.index({
|
||||||
|
pipeline: "attachment",
|
||||||
|
index: "ehr-api-client",
|
||||||
|
document: {
|
||||||
|
data: Buffer.from(file.buffer).toString("base64"),
|
||||||
|
path: pathname,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
keywords,
|
||||||
|
categories,
|
||||||
},
|
},
|
||||||
}),
|
op_type: "index",
|
||||||
);
|
});
|
||||||
|
}
|
||||||
|
|
||||||
minioClient.putObject("ehr", `test_upload_file/${filename}`, file.buffer, file.size, {
|
return this.setStatus(HttpStatusCode.CREATED);
|
||||||
"Content-Type": file.mimetype,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue