From 374644a691326b09908a1eb8160fdb25ce18cadc Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:37:56 +0700 Subject: [PATCH] feat: only get url if object exists --- src/controllers/service/service-controller.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/controllers/service/service-controller.ts b/src/controllers/service/service-controller.ts index b05120f..ff8f49c 100644 --- a/src/controllers/service/service-controller.ts +++ b/src/controllers/service/service-controller.ts @@ -15,7 +15,7 @@ import { import { Prisma, Status } from "@prisma/client"; import prisma from "../../db"; -import minio from "../../services/minio"; +import minio, { presignedGetObjectIfExist } from "../../services/minio"; import { RequestWithUser } from "../../interfaces/user"; import HttpError from "../../interfaces/http-error"; import HttpStatus from "../../interfaces/http-status"; @@ -83,7 +83,11 @@ export class ServiceController extends Controller { result: await Promise.all( result.map(async (v) => ({ ...v, - imageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(v.id), 12 * 60 * 60), + imageUrl: await presignedGetObjectIfExist( + MINIO_BUCKET, + imageLocation(v.id), + 12 * 60 * 60, + ), })), ), page, @@ -119,7 +123,7 @@ export class ServiceController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "data_not_found"); return Object.assign(record, { - imageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id), 60 * 60), + imageUrl: await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(record.id), 60 * 60), }); } @@ -222,7 +226,7 @@ export class ServiceController extends Controller { this.setStatus(HttpStatus.CREATED); return Object.assign(record, { - imageUrl: await minio.presignedGetObject( + imageUrl: await presignedGetObjectIfExist( MINIO_BUCKET, imageLocation(record.id), 12 * 60 * 60, @@ -250,12 +254,12 @@ export class ServiceController extends Controller { where: { id: serviceId }, }); return Object.assign(record, { - profileImageUrl: await minio.presignedGetObject( + imageUrl: await presignedGetObjectIfExist( MINIO_BUCKET, imageLocation(record.id), 12 * 60 * 60, ), - profileImageUploadUrl: await minio.presignedPutObject( + imageUploadUrl: await minio.presignedPutObject( MINIO_BUCKET, imageLocation(record.id), 12 * 60 * 60,