refactor: is path exist function

This commit is contained in:
Methapon2001 2023-11-27 16:07:32 +07:00
parent ca5be0042d
commit 1559226837
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
8 changed files with 96 additions and 44 deletions

View file

@ -12,12 +12,13 @@ import {
Tags,
Request,
Response,
Example,
} from "tsoa";
import minioClient from "../minio";
import esClient from "../elasticsearch";
import { copyCond, listFolder, listItem, replaceIllegalChars } from "../utils/minio";
import { copyCond, listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
import HttpStatusCode from "../interfaces/http-status";
import { StorageFile, StorageFolder } from "../interfaces/storage-fs";
@ -31,6 +32,9 @@ if (!DEFAULT_INDEX) throw Error("Default ElasticSearch index must be specified."
@Route("/cabinet/{cabinetName}/drawer")
export class DrawerController extends Controller {
/**
* @example cabinetName "ตู้เอกสาร 1"
*/
@Get("/")
@Tags("ลิ้นชัก")
@Security("bearerAuth")
@ -39,6 +43,20 @@ export class DrawerController extends Controller {
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการลิ้นชักได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
@Example([
{
path: "ตู้เอกสาร 1/ลิ้นชัก 1/",
name: "ลิ้นชัก 1",
createdAt: "2021-07-20T12:33:13.018Z",
createdBy: "admin",
},
{
path: "ตู้เอกสาร 1/ลิ้นชัก 2/",
name: "ลิ้นชัก 2",
createdAt: "2022-01-23T16:05:02.114Z",
createdBy: "admin",
},
])
public async listDrawer(@Path() cabinetName: string): Promise<StorageFolder[]> {
const list = await listFolder(DEFAULT_BUCKET!, `${cabinetName}/`).catch((e) =>
console.error(`Error List Folder: ${e}`),
@ -48,6 +66,9 @@ export class DrawerController extends Controller {
return list;
}
/**
* @example cabinetName "ตู้เอกสาร 1"
*/
@Post("/")
@Tags("ลิ้นชัก")
@Security("bearerAuth", ["admin"])
@ -57,18 +78,17 @@ export class DrawerController extends Controller {
public async createDrawer(
@Request() request: { user: { preferred_username: string } },
@Path() cabinetName: string,
@Body() body: { name: string },
@Body()
body: {
/**
* @example "ลิ้นชัก 1"
*/
name: string;
},
) {
const basePath = `${cabinetName}/`;
if (
!Boolean(
await minioClient.statObject(DEFAULT_BUCKET!, `${basePath}.keep`).catch((e) => {
if (e.code === "NotFound") return false;
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
}),
)
) {
if (!(await pathExist(DEFAULT_BUCKET!, basePath))) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก");
}
@ -84,6 +104,10 @@ export class DrawerController extends Controller {
return this.setStatus(HttpStatusCode.CREATED);
}
/**
* @example cabinetName "ตู้เอกสาร 1"
* @example drawerName "ลิ้นชัก 1"
*/
@Put("/{drawerName}")
@Tags("ลิ้นชัก")
@Security("bearerAuth", ["admin"])
@ -92,7 +116,13 @@ export class DrawerController extends Controller {
public async editDrawer(
@Path() cabinetName: string,
@Path() drawerName: string,
@Body() body: { name: string },
@Body()
body: {
/**
* @example "ลิ้นชักใหม่"
*/
name: string;
},
): Promise<void> {
const path = `${cabinetName}/${drawerName}/`;
const list = await listItem(DEFAULT_BUCKET!, path, true);
@ -113,7 +143,9 @@ export class DrawerController extends Controller {
return await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
}
const search = await esClient.search<StorageFile & { attachment: Record<string, string> }>({
const search = await esClient.search<
StorageFile & { attachment: Record<string, string> }
>({
index: DEFAULT_INDEX!,
query: { match: { pathname: current.name } },
});
@ -140,6 +172,10 @@ export class DrawerController extends Controller {
return this.setStatus(HttpStatusCode.NO_CONTENT);
}
/**
* @example cabinetName "ตู้เอกสาร 1"
* @example drawerName "ลิ้นชัก 1"
*/
@Delete("/{drawerName}")
@Tags("ลิ้นชัก")
@Security("bearerAuth", ["admin"])

View file

@ -84,9 +84,10 @@ export class FileController extends Controller {
@Path() drawerName: string,
@Path() folderName: string,
) {
const pathname = `${cabinetName}/${drawerName}/${folderName}/${body.file}`;
const basePath = `${cabinetName}/${drawerName}/${folderName}/`;
const pathname = `${basePath}${body.file}`;
if (!(await pathExist(`${cabinetName}/${drawerName}/${folderName}/`))) {
if (!(await pathExist(DEFAULT_BUCKET!, basePath))) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ตำแหน่งที่ระบุไม่พบ กรุณาเตรียมตำแหน่งที่ต้องการก่อนดำเนินการ",

View file

@ -17,7 +17,7 @@ import {
import minioClient from "../minio";
import esClient from "../elasticsearch";
import { copyCond, listFolder, listItem, replaceIllegalChars } from "../utils/minio";
import { copyCond, listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
import HttpStatusCode from "../interfaces/http-status";
import { StorageFile, StorageFolder } from "../interfaces/storage-fs";
@ -64,15 +64,8 @@ export class FolderController extends Controller {
) {
const basePath = `${cabinetName}/${drawerName}/`;
if (
!Boolean(
await minioClient.statObject(DEFAULT_BUCKET!, `${basePath}.keep`).catch((e) => {
if (e.code === "NotFound") return false;
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
}),
)
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างแฟ้ม");
if (!(await pathExist(DEFAULT_BUCKET!, basePath))) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก");
}
const created = await minioClient
@ -117,7 +110,9 @@ export class FolderController extends Controller {
return await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
}
const search = await esClient.search<StorageFile & { attachment: Record<string, string> }>({
const search = await esClient.search<
StorageFile & { attachment: Record<string, string> }
>({
index: DEFAULT_INDEX!,
query: { match: { pathname: current.name } },
});

View file

@ -17,7 +17,7 @@ import {
import minioClient from "../minio";
import esClient from "../elasticsearch";
import { copyCond, listFolder, listItem, replaceIllegalChars } from "../utils/minio";
import { copyCond, listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
import HttpStatusCode from "../interfaces/http-status";
import { StorageFile, StorageFolder } from "../interfaces/storage-fs";
@ -67,15 +67,8 @@ export class SubFolderController extends Controller {
) {
const basePath = `${cabinetName}/${drawerName}/${folderName}/`;
if (
!Boolean(
await minioClient.statObject(DEFAULT_BUCKET!, `${basePath}.keep`).catch((e) => {
if (e.code === "NotFound") return false;
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
}),
)
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแฟ้ม");
if (!(await pathExist(DEFAULT_BUCKET!, basePath))) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก");
}
const created = await minioClient
@ -121,7 +114,9 @@ export class SubFolderController extends Controller {
return await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
}
const search = await esClient.search<StorageFile & { attachment: Record<string, string> }>({
const search = await esClient.search<
StorageFile & { attachment: Record<string, string> }
>({
index: DEFAULT_INDEX!,
query: { match: { pathname: current.name } },
});

View file

@ -88,9 +88,10 @@ export class SubFolderFileController extends Controller {
@Path() folderName: string,
@Path() subFolderName: string,
) {
const pathname = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${body.file}`;
const basePath = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/`;
const pathname = `${basePath}${body.file}`;
if (!(await pathExist(`${cabinetName}/${drawerName}/${folderName}/${subFolderName}`))) {
if (!(await pathExist(DEFAULT_BUCKET!, basePath))) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ตำแหน่งที่ระบุไม่พบ กรุณาเตรียมตำแหน่งที่ต้องการก่อนดำเนินการ",

View file

@ -13,6 +13,8 @@ import { FolderController } from './controllers/folderController';
// 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
import { SearchController } from './controllers/searchController';
// 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
import { StorageController } from './controllers/storageController';
// 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
import { SubFolderController } from './controllers/subFolderController';
// 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
import { SubFolderFileController } from './controllers/subFolderFileController';

View file

@ -381,6 +381,24 @@
"$ref": "#/components/schemas/StorageFolder"
},
"type": "array"
},
"examples": {
"Example 1": {
"value": [
{
"path": "ตู้เอกสาร 1/ลิ้นชัก 1/",
"name": "ลิ้นชัก 1",
"createdAt": "2021-07-20T12:33:13.018Z",
"createdBy": "admin"
},
{
"path": "ตู้เอกสาร 1/ลิ้นชัก 2/",
"name": "ลิ้นชัก 2",
"createdAt": "2022-01-23T16:05:02.114Z",
"createdBy": "admin"
}
]
}
}
}
}
@ -450,7 +468,8 @@
"schema": {
"properties": {
"name": {
"type": "string"
"type": "string",
"example": "ลิ้นชัก 1"
}
},
"required": [
@ -500,7 +519,8 @@
"required": true,
"schema": {
"type": "string"
}
},
"example": "ลิ้นชัก 1"
}
],
"requestBody": {
@ -510,7 +530,8 @@
"schema": {
"properties": {
"name": {
"type": "string"
"type": "string",
"example": "ลิ้นชักใหม่"
}
},
"required": [
@ -555,7 +576,8 @@
"required": true,
"schema": {
"type": "string"
}
},
"example": "ลิ้นชัก 1"
}
]
}

View file

@ -15,9 +15,9 @@ export function replaceIllegalChars(path: string, replace = "-") {
/**
* Check if folder really exist by using ".keep" object.
*/
export async function pathExist(path: string): Promise<boolean> {
export async function pathExist(bucket: string, path: string): Promise<boolean> {
return Boolean(
await minioClient.statObject("ehr", `${path.replace(/^\/|\/$/g, "")}/.keep`).catch((e) => {
await minioClient.statObject(bucket, `${path.replace(/^\/|\/$/g, "")}/.keep`).catch((e) => {
if (e.code === "NotFound") return false;
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
}),