feat: upload and select multiple image

This commit is contained in:
Methapon Metanipat 2024-09-10 09:56:46 +07:00
parent 5a50ab209d
commit ed832148fc
4 changed files with 78 additions and 24 deletions

View file

@ -15,12 +15,13 @@ import {
} from "tsoa";
import { RequestWithUser } from "../interfaces/user";
import prisma from "../db";
import minio, { deleteFolder, presignedGetObjectIfExist } from "../services/minio";
import minio, { deleteFolder } from "../services/minio";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { isSystem } from "../utils/keycloak";
import { branchRelationPermInclude, createPermCheck } from "../services/permission";
import { filterStatus } from "../services/prisma";
import { fileLocation, listFile } from "../utils/minio";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -74,10 +75,6 @@ export type CustomerUpdate = {
birthDate?: Date;
};
function imageLocation(id: string) {
return `customer/${id}/profile-image`;
}
@Route("api/v1/customer")
@Tags("Customer")
export class CustomerController extends Controller {
@ -397,22 +394,46 @@ export class CustomerController extends Controller {
async (data) => await deleteFolder(MINIO_BUCKET, `customer/${customerId}`).then(() => data),
);
}
}
@Get("{customerId}/image")
async getCustomerImageById(@Request() req: RequestWithUser, @Path() customerId: string) {
const url = await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(customerId), 60 * 60);
if (!url) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
@Route("api/v1/customer/{customerId}/image")
@Tags("Customer")
export class CustomerImageController extends Controller {
@Get()
@Security("keycloak")
async listImage(@Path() customerId: string) {
const customer = await prisma.customer.findUnique({
where: { id: customerId },
});
if (!customer) {
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found", "customerNotFound");
}
return req.res?.redirect(url);
return await listFile(fileLocation.customer.img(customerId));
}
@Put("{customerId}/image")
@Security("keycloak", MANAGE_ROLES)
async setCustomerImageById(@Request() req: RequestWithUser, @Path() customerId: string) {
const record = await prisma.customer.findFirst({
@Get("{name}")
async getImage(
@Request() req: RequestWithUser,
@Path() customerId: string,
@Path() name: string,
) {
return req.res?.redirect(
await minio.presignedGetObject(
MINIO_BUCKET,
fileLocation.customer.img(customerId, name),
12 * 60 * 60,
),
);
}
@Put("{name}")
@Security("keycloak")
async putImage(
@Request() req: RequestWithUser,
@Path() customerId: string,
@Path() name: string,
) {
const customer = await prisma.customer.findUnique({
where: { id: customerId },
include: {
registeredBranch: {
@ -420,15 +441,41 @@ export class CustomerController extends Controller {
},
},
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
if (!customer) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
}
await permissionCheck(req.user, record.registeredBranch);
console.log(customer.registeredBranch);
await permissionCheck(req.user, customer.registeredBranch);
return req.res?.redirect(
await minio.presignedPutObject(MINIO_BUCKET, imageLocation(customerId), 12 * 60 * 60),
await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.customer.img(customerId, name),
12 * 60 * 60,
),
);
}
@Delete("{name}")
@Security("keycloak")
async deleteImage(
@Request() req: RequestWithUser,
@Path() customerId: string,
@Path() name: string,
) {
const customer = await prisma.customer.findUnique({
where: { id: customerId },
include: {
registeredBranch: {
include: branchRelationPermInclude(req.user),
},
},
});
if (!customer) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
}
await permissionCheck(req.user, customer.registeredBranch);
await minio.removeObject(MINIO_BUCKET, fileLocation.customer.img(customerId, name), {
forceDelete: true,
});
}
}