From 8750c2cf980690c5b6709e50af47a13cc7120acc Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 5 Sep 2024 09:55:45 +0700 Subject: [PATCH] refactor: drop supported presigned url return Now use its endpoint to upload file instead. The server still not loaded by the api as it redirect to presigned url. --- src/controllers/01-branch-controller.ts | 100 ++++++++++-------------- src/utils/minio.ts | 31 ++++++++ 2 files changed, 74 insertions(+), 57 deletions(-) create mode 100644 src/utils/minio.ts diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index a0bbb0e..8679ff1 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -20,6 +20,7 @@ import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; import minio, { presignedGetObjectIfExist } from "../services/minio"; import { isSystem } from "../utils/keycloak"; +import { fileLocation } from "../utils/minio"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -99,18 +100,6 @@ type BranchUpdate = { }[]; }; -function lineImageLoc(id: string) { - return `branch/line-qr-${id}`; -} - -function branchImageLoc(id: string) { - return `branch/branch-img-${id}`; -} - -function mapImageLoc(id: string) { - return `branch/map-img-${id}`; -} - @Route("api/v1/branch") @Tags("Branch") export class BranchController extends Controller { @@ -306,10 +295,7 @@ export class BranchController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound"); } - return Object.assign(record, { - imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)), - qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)), - }); + return record; } @Post() @@ -395,6 +381,7 @@ export class BranchController extends Controller { province: true, district: true, subDistrict: true, + contact: true, createdBy: true, updatedBy: true, }, @@ -430,21 +417,7 @@ export class BranchController extends Controller { this.setStatus(HttpStatus.CREATED); - return Object.assign(record, { - contact: await prisma.branchContact.findMany({ where: { branchId: record.id } }), - imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)), - imageUploadUrl: await minio.presignedPutObject( - MINIO_BUCKET, - branchImageLoc(record.id), - 12 * 60 * 60, - ), - qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)), - qrCodeImageUploadUrl: await minio.presignedPutObject( - MINIO_BUCKET, - lineImageLoc(record.id), - 12 * 60 * 60, - ), - }); + return record; } @Put("{branchId}") @@ -554,20 +527,7 @@ export class BranchController extends Controller { where: { id: branchId }, }); - return Object.assign(record, { - imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)), - imageUploadUrl: await minio.presignedPutObject( - MINIO_BUCKET, - branchImageLoc(record.id), - 12 * 60 * 60, - ), - qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)), - qrCodeImageUploadUrl: await minio.presignedPutObject( - MINIO_BUCKET, - lineImageLoc(record.id), - 12 * 60 * 60, - ), - }); + return record; } @Delete("{branchId}") @@ -626,12 +586,14 @@ export class BranchController extends Controller { }); } - await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), { - forceDelete: true, - }); - await minio.removeObject(MINIO_BUCKET, branchImageLoc(branchId), { - forceDelete: true, - }); + await Promise.all([ + minio.removeObject(MINIO_BUCKET, fileLocation.branch.line(branchId), { + forceDelete: true, + }), + minio.removeObject(MINIO_BUCKET, fileLocation.branch.image(branchId), { + forceDelete: true, + }), + ]); return data; }); @@ -639,7 +601,11 @@ export class BranchController extends Controller { @Get("{branchId}/line-image") async getLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - const url = await presignedGetObjectIfExist(MINIO_BUCKET, lineImageLoc(branchId), 60 * 60); + const url = await presignedGetObjectIfExist( + MINIO_BUCKET, + fileLocation.branch.line(branchId), + 60 * 60, + ); if (!url) { throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); @@ -674,13 +640,21 @@ export class BranchController extends Controller { } return req.res?.redirect( - await minio.presignedPutObject(MINIO_BUCKET, lineImageLoc(record.id), 12 * 60 * 60), + await minio.presignedPutObject( + MINIO_BUCKET, + fileLocation.branch.line(record.id), + 12 * 60 * 60, + ), ); } @Get("{branchId}/branch-image") async getBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - const url = await presignedGetObjectIfExist(MINIO_BUCKET, branchImageLoc(branchId), 60 * 60); + const url = await presignedGetObjectIfExist( + MINIO_BUCKET, + fileLocation.branch.image(branchId), + 60 * 60, + ); if (!url) { throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); @@ -715,13 +689,21 @@ export class BranchController extends Controller { } return req.res?.redirect( - await minio.presignedPutObject(MINIO_BUCKET, branchImageLoc(record.id), 12 * 60 * 60), + await minio.presignedPutObject( + MINIO_BUCKET, + fileLocation.branch.image(record.id), + 12 * 60 * 60, + ), ); } @Get("{branchId}/map-image") async getMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { - const url = await presignedGetObjectIfExist(MINIO_BUCKET, mapImageLoc(branchId), 60 * 60); + const url = await presignedGetObjectIfExist( + MINIO_BUCKET, + fileLocation.branch.image(branchId), + 60 * 60, + ); if (!url) { throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); @@ -756,7 +738,11 @@ export class BranchController extends Controller { } return req.res?.redirect( - await minio.presignedPutObject(MINIO_BUCKET, mapImageLoc(record.id), 12 * 60 * 60), + await minio.presignedPutObject( + MINIO_BUCKET, + fileLocation.branch.map(record.id), + 12 * 60 * 60, + ), ); } } diff --git a/src/utils/minio.ts b/src/utils/minio.ts new file mode 100644 index 0000000..b028016 --- /dev/null +++ b/src/utils/minio.ts @@ -0,0 +1,31 @@ +import minio from "../services/minio"; + +if (!process.env.MINIO_BUCKET) { + throw Error("Require MinIO bucket."); +} + +const MINIO_BUCKET = process.env.MINIO_BUCKET; + +export async function deleteFolder(path: string) { + new Promise((resolve, reject) => { + const item: string[] = []; + + const stream = minio.listObjectsV2(MINIO_BUCKET, path, true); + + stream.on("data", (v) => v && v.name && item.push(v.name)); + stream.on("end", () => resolve(item)); + stream.on("error", () => reject(new Error("MinIO error."))); + }).then((list) => { + list.map(async (v) => { + await minio.removeObject(MINIO_BUCKET, v, { forceDelete: true }); + }); + }); +} + +export const fileLocation = { + branch: { + line: (id: string) => `branch/line-qr-${id}`, + image: (id: string) => `branch/branch-img-${id}`, + map: (id: string) => `branch/map-img-${id}`, + }, +};