feat: only get url if object exists

This commit is contained in:
Methapon2001 2024-06-12 16:37:56 +07:00
parent 68af0eb821
commit 374644a691

View file

@ -15,7 +15,7 @@ import {
import { Prisma, Status } from "@prisma/client"; import { Prisma, Status } from "@prisma/client";
import prisma from "../../db"; import prisma from "../../db";
import minio from "../../services/minio"; import minio, { presignedGetObjectIfExist } from "../../services/minio";
import { RequestWithUser } from "../../interfaces/user"; import { RequestWithUser } from "../../interfaces/user";
import HttpError from "../../interfaces/http-error"; import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status"; import HttpStatus from "../../interfaces/http-status";
@ -83,7 +83,11 @@ export class ServiceController extends Controller {
result: await Promise.all( result: await Promise.all(
result.map(async (v) => ({ result.map(async (v) => ({
...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, page,
@ -119,7 +123,7 @@ export class ServiceController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "data_not_found"); throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "data_not_found");
return Object.assign(record, { 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); this.setStatus(HttpStatus.CREATED);
return Object.assign(record, { return Object.assign(record, {
imageUrl: await minio.presignedGetObject( imageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET, MINIO_BUCKET,
imageLocation(record.id), imageLocation(record.id),
12 * 60 * 60, 12 * 60 * 60,
@ -250,12 +254,12 @@ export class ServiceController extends Controller {
where: { id: serviceId }, where: { id: serviceId },
}); });
return Object.assign(record, { return Object.assign(record, {
profileImageUrl: await minio.presignedGetObject( imageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET, MINIO_BUCKET,
imageLocation(record.id), imageLocation(record.id),
12 * 60 * 60, 12 * 60 * 60,
), ),
profileImageUploadUrl: await minio.presignedPutObject( imageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET, MINIO_BUCKET,
imageLocation(record.id), imageLocation(record.id),
12 * 60 * 60, 12 * 60 * 60,