feat: handle exists file
This commit is contained in:
parent
8fdee84b97
commit
478c89ea92
5 changed files with 266 additions and 99 deletions
|
|
@ -15,20 +15,21 @@ import minioClient from "../storage";
|
||||||
import HttpStatusCode from "../interfaces/http-status";
|
import HttpStatusCode from "../interfaces/http-status";
|
||||||
import { pathExist } from "../utils/minio";
|
import { pathExist } from "../utils/minio";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
|
import { EhrFile } from "../interfaces/ehr-fs";
|
||||||
|
|
||||||
@Route("/cabinet")
|
@Route("/cabinet")
|
||||||
export class FileController extends Controller {
|
export class FileController extends Controller {
|
||||||
@Post("/{cabinetName}/drawer/{drawerName}/folder/{folderName}")
|
@Post("/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file")
|
||||||
@Tags("File")
|
@Tags("File")
|
||||||
@Security("bearerAuth")
|
@Security("bearerAuth")
|
||||||
@SuccessResponse(HttpStatusCode.CREATED)
|
@SuccessResponse(HttpStatusCode.CREATED)
|
||||||
public async uploadFile(
|
public async uploadFile(
|
||||||
@Request() request: any,
|
@Request() request: { user: { preferred_username: string } },
|
||||||
@UploadedFile() file: Express.Multer.File,
|
@UploadedFile() file: Express.Multer.File,
|
||||||
@FormField() title: string,
|
@FormField() title: string,
|
||||||
@FormField() description: string,
|
@FormField() description: string,
|
||||||
@FormField() keywords: string,
|
@FormField() keyword: string,
|
||||||
@FormField() categories: string,
|
@FormField() category: string,
|
||||||
@Path() cabinetName: string,
|
@Path() cabinetName: string,
|
||||||
@Path() drawerName: string,
|
@Path() drawerName: string,
|
||||||
@Path() folderName: string,
|
@Path() folderName: string,
|
||||||
|
|
@ -49,21 +50,58 @@ export class FileController extends Controller {
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
createdBy: request.user.preferred_username,
|
createdBy: request.user.preferred_username,
|
||||||
})
|
})
|
||||||
.catch(() => new Error("Object storage error occured."));
|
.catch((e) => console.error(e));
|
||||||
|
|
||||||
if (info) {
|
if (!info) throw new Error("Object storage error occured.");
|
||||||
|
|
||||||
|
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||||
|
index: "ehr-api-client",
|
||||||
|
query: {
|
||||||
|
match: {
|
||||||
|
pathname: pathname,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const exist = search.hits.hits.find((v) => v._source?.pathname === pathname);
|
||||||
|
|
||||||
|
const metadata: Partial<EhrFile> = {
|
||||||
|
pathname,
|
||||||
|
fileName: filename,
|
||||||
|
fileSize: file.size,
|
||||||
|
fileType: file.mimetype,
|
||||||
|
title: title,
|
||||||
|
description: description,
|
||||||
|
category: category.split(","),
|
||||||
|
keyword: keyword.split(","),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!exist) {
|
||||||
await esClient.index({
|
await esClient.index({
|
||||||
pipeline: "attachment",
|
pipeline: "attachment",
|
||||||
index: "ehr-api-client",
|
index: "ehr-api-client",
|
||||||
document: {
|
document: {
|
||||||
data: Buffer.from(file.buffer).toString("base64"),
|
data: Buffer.from(file.buffer).toString("base64"),
|
||||||
path: pathname,
|
createdAt: new Date().toISOString(),
|
||||||
title,
|
createdBy: request.user.preferred_username,
|
||||||
description,
|
updatedAt: new Date().toISOString(),
|
||||||
keywords,
|
updatedBy: request.user.preferred_username,
|
||||||
categories,
|
...metadata,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await esClient.delete({ index: exist._index, id: exist._id });
|
||||||
|
await esClient.index({
|
||||||
|
pipeline: "attachment",
|
||||||
|
index: "ehr-api-client",
|
||||||
|
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,
|
||||||
},
|
},
|
||||||
op_type: "index",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,11 @@ export interface EhrFile {
|
||||||
pathname: string;
|
pathname: string;
|
||||||
|
|
||||||
fileName: string;
|
fileName: string;
|
||||||
fileSize: string;
|
fileSize: number;
|
||||||
fileType: string;
|
fileType: string;
|
||||||
|
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
category: string[];
|
category: string[];
|
||||||
keyword: string[];
|
keyword: string[];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatusCode from "../interfaces/http-status";
|
import HttpStatusCode from "../interfaces/http-status";
|
||||||
|
import { ValidateError } from "tsoa";
|
||||||
|
|
||||||
function errorHandler(error: Error, _req: Request, res: Response, _next: NextFunction) {
|
function errorHandler(error: Error, _req: Request, res: Response, _next: NextFunction) {
|
||||||
if (error instanceof HttpError) {
|
if (error instanceof HttpError) {
|
||||||
|
|
@ -10,6 +11,16 @@ function errorHandler(error: Error, _req: Request, res: Response, _next: NextFun
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error instanceof ValidateError) {
|
||||||
|
return res.status(error.status).json({
|
||||||
|
status: HttpStatusCode.UNPROCESSABLE_ENTITY,
|
||||||
|
message: "Validation error(s).",
|
||||||
|
detail: error.fields,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR).json({
|
return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR).json({
|
||||||
status: HttpStatusCode.INTERNAL_SERVER_ERROR,
|
status: HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||||
message: error.message,
|
message: error.message,
|
||||||
|
|
|
||||||
|
|
@ -255,7 +255,8 @@ export function RegisterRoutes(app: Router) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
|
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
|
||||||
app.post('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName',
|
app.post('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName/file',
|
||||||
|
authenticateMiddleware([{"bearerAuth":[]}]),
|
||||||
upload.single('file'),
|
upload.single('file'),
|
||||||
...(fetchMiddlewares<RequestHandler>(FileController)),
|
...(fetchMiddlewares<RequestHandler>(FileController)),
|
||||||
...(fetchMiddlewares<RequestHandler>(FileController.prototype.uploadFile)),
|
...(fetchMiddlewares<RequestHandler>(FileController.prototype.uploadFile)),
|
||||||
|
|
@ -266,8 +267,8 @@ export function RegisterRoutes(app: Router) {
|
||||||
file: {"in":"formData","name":"file","required":true,"dataType":"file"},
|
file: {"in":"formData","name":"file","required":true,"dataType":"file"},
|
||||||
title: {"in":"formData","name":"title","required":true,"dataType":"string"},
|
title: {"in":"formData","name":"title","required":true,"dataType":"string"},
|
||||||
description: {"in":"formData","name":"description","required":true,"dataType":"string"},
|
description: {"in":"formData","name":"description","required":true,"dataType":"string"},
|
||||||
keywords: {"in":"formData","name":"keywords","required":true,"dataType":"string"},
|
keyword: {"in":"formData","name":"keyword","required":true,"dataType":"string"},
|
||||||
categories: {"in":"formData","name":"categories","required":true,"dataType":"string"},
|
category: {"in":"formData","name":"category","required":true,"dataType":"string"},
|
||||||
cabinetName: {"in":"path","name":"cabinetName","required":true,"dataType":"string"},
|
cabinetName: {"in":"path","name":"cabinetName","required":true,"dataType":"string"},
|
||||||
drawerName: {"in":"path","name":"drawerName","required":true,"dataType":"string"},
|
drawerName: {"in":"path","name":"drawerName","required":true,"dataType":"string"},
|
||||||
folderName: {"in":"path","name":"folderName","required":true,"dataType":"string"},
|
folderName: {"in":"path","name":"folderName","required":true,"dataType":"string"},
|
||||||
|
|
@ -283,7 +284,34 @@ export function RegisterRoutes(app: Router) {
|
||||||
|
|
||||||
|
|
||||||
const promise = controller.uploadFile.apply(controller, validatedArgs as any);
|
const promise = controller.uploadFile.apply(controller, validatedArgs as any);
|
||||||
promiseHandler(controller, promise, response, 204, next);
|
promiseHandler(controller, promise, response, 201, next);
|
||||||
|
} catch (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
|
||||||
|
app.get('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName/file',
|
||||||
|
...(fetchMiddlewares<RequestHandler>(FileController)),
|
||||||
|
...(fetchMiddlewares<RequestHandler>(FileController.prototype.getFile)),
|
||||||
|
|
||||||
|
function FileController_getFile(request: any, response: any, next: any) {
|
||||||
|
const args = {
|
||||||
|
cabinetName: {"in":"path","name":"cabinetName","required":true,"dataType":"string"},
|
||||||
|
drawerName: {"in":"path","name":"drawerName","required":true,"dataType":"string"},
|
||||||
|
folderName: {"in":"path","name":"folderName","required":true,"dataType":"string"},
|
||||||
|
};
|
||||||
|
|
||||||
|
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
|
||||||
|
|
||||||
|
let validatedArgs: any[] = [];
|
||||||
|
try {
|
||||||
|
validatedArgs = getValidatedArgs(args, request, response);
|
||||||
|
|
||||||
|
const controller = new FileController();
|
||||||
|
|
||||||
|
|
||||||
|
const promise = controller.getFile.apply(controller, validatedArgs as any);
|
||||||
|
promiseHandler(controller, promise, response, 200, next);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -375,18 +375,22 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}": {
|
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file": {
|
||||||
"post": {
|
"post": {
|
||||||
"operationId": "UploadFile",
|
"operationId": "UploadFile",
|
||||||
"responses": {
|
"responses": {
|
||||||
"204": {
|
"201": {
|
||||||
"description": ""
|
"description": ""
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
"File"
|
"File"
|
||||||
],
|
],
|
||||||
"security": [],
|
"security": [
|
||||||
|
{
|
||||||
|
"bearerAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"in": "path",
|
"in": "path",
|
||||||
|
|
@ -430,10 +434,10 @@
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"keywords": {
|
"keyword": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"categories": {
|
"category": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -441,99 +445,69 @@
|
||||||
"file",
|
"file",
|
||||||
"title",
|
"title",
|
||||||
"description",
|
"description",
|
||||||
"keywords",
|
"keyword",
|
||||||
"categories"
|
"category"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"put": {
|
"get": {
|
||||||
"operationId": "EditFolder",
|
"operationId": "GetFile",
|
||||||
"responses": {
|
"responses": {
|
||||||
"204": {
|
"200": {
|
||||||
"description": "",
|
"description": "",
|
||||||
"content": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
"schema": {}
|
"schema": {
|
||||||
|
"items": {
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"category": {
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"keyword": {
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"pathname": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"type",
|
||||||
|
"category",
|
||||||
|
"keyword",
|
||||||
|
"description",
|
||||||
|
"title",
|
||||||
|
"pathname"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
"Folder"
|
"File"
|
||||||
],
|
|
||||||
"security": [
|
|
||||||
{
|
|
||||||
"bearerAuth": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"in": "query",
|
|
||||||
"name": "cabinetName",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"in": "query",
|
|
||||||
"name": "drawerName",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"in": "query",
|
|
||||||
"name": "folderName",
|
|
||||||
"required": true,
|
|
||||||
"schema": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"requestBody": {
|
|
||||||
"required": true,
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"properties": {
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"name"
|
|
||||||
],
|
|
||||||
"type": "object"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"delete": {
|
|
||||||
"operationId": "DeleteFolder",
|
|
||||||
"responses": {
|
|
||||||
"204": {
|
|
||||||
"description": "",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tags": [
|
|
||||||
"Folder"
|
|
||||||
],
|
|
||||||
"security": [
|
|
||||||
{
|
|
||||||
"bearerAuth": []
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
|
"security": [],
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"in": "path",
|
"in": "path",
|
||||||
|
|
@ -656,6 +630,120 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}": {
|
||||||
|
"put": {
|
||||||
|
"operationId": "EditFolder",
|
||||||
|
"responses": {
|
||||||
|
"204": {
|
||||||
|
"description": "",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"Folder"
|
||||||
|
],
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"bearerAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"in": "query",
|
||||||
|
"name": "cabinetName",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"in": "query",
|
||||||
|
"name": "drawerName",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"in": "query",
|
||||||
|
"name": "folderName",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"name"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"operationId": "DeleteFolder",
|
||||||
|
"responses": {
|
||||||
|
"204": {
|
||||||
|
"description": "",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"Folder"
|
||||||
|
],
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"bearerAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"in": "path",
|
||||||
|
"name": "cabinetName",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"in": "path",
|
||||||
|
"name": "drawerName",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"in": "path",
|
||||||
|
"name": "folderName",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/subfolder": {
|
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/subfolder": {
|
||||||
"get": {
|
"get": {
|
||||||
"operationId": "ListFolder",
|
"operationId": "ListFolder",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue