feat: update file
This commit is contained in:
parent
c95ea595ce
commit
b67a3d1b22
1 changed files with 93 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ import {
|
|||
Delete,
|
||||
FormField,
|
||||
Get,
|
||||
Patch,
|
||||
Path,
|
||||
Post,
|
||||
Request,
|
||||
|
|
@ -168,6 +169,98 @@ export class FileController extends Controller {
|
|||
return records;
|
||||
}
|
||||
|
||||
@Patch("/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file/{fileName}")
|
||||
@Tags("File")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
public async updateFile(
|
||||
@Request() request: { user: { preferred_username: string } },
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
@Path() folderName: string,
|
||||
@Path() fileName: string,
|
||||
@UploadedFile() file?: Express.Multer.File,
|
||||
@FormField() title?: string,
|
||||
@FormField() description?: string,
|
||||
@FormField() keyword?: string,
|
||||
@FormField() category?: string,
|
||||
) {
|
||||
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: "ehr-api-client",
|
||||
query: {
|
||||
match: {
|
||||
pathname: `${cabinetName}/${drawerName}/${folderName}/${fileName}`,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (search && search.hits.hits.length === 0) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found");
|
||||
}
|
||||
|
||||
const data = search.hits.hits[0];
|
||||
|
||||
if (!file) {
|
||||
const esResult = await esClient
|
||||
.update({
|
||||
index: "ehr-api-client",
|
||||
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 (!esResult) throw new Error("An error occured, cannot perform this action.");
|
||||
} else {
|
||||
const filename = Buffer.from(file.originalname, "latin1").toString("utf-8");
|
||||
const pathname = `${cabinetName}/${drawerName}/${folderName}/${filename}`;
|
||||
|
||||
await minioClient.removeObject(
|
||||
"ehr",
|
||||
`${cabinetName}/${drawerName}/${folderName}/${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,
|
||||
})
|
||||
.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: "ehr-api-client",
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Delete("/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file/{fileName}")
|
||||
@Tags("File")
|
||||
@Security("bearerAuth")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue