feat: branch image multi

This commit is contained in:
Methapon Metanipat 2024-09-09 14:02:17 +07:00
parent 102f0216f8
commit 9e745ee81c
2 changed files with 60 additions and 1 deletions

View file

@ -20,7 +20,7 @@ import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
import minio from "../services/minio";
import { isSystem } from "../utils/keycloak";
import { deleteFile, fileLocation } from "../utils/minio";
import { deleteFile, fileLocation, listFile } from "../utils/minio";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -837,4 +837,62 @@ export class BranchController extends Controller {
await permissionCheck(req.user, branchId);
await deleteFile(fileLocation.branch.bank(branchId, bankId));
}
@Get("{branchId}/image")
@Security("keycloak")
async listImage(@Path() branchId: string) {
const record = await prisma.branch.findFirst({
where: { id: branchId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
}
return await listFile(fileLocation.user.profile(branchId));
}
@Get("{branchId}/image/{name}")
async getImageByName(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() name: string,
) {
return req.res?.redirect(
await minio.presignedGetObject(
MINIO_BUCKET,
fileLocation.branch.img(branchId, name),
12 * 60 * 60,
),
);
}
@Put("{branchId}/image/{name}")
@Security("keycloak")
async putImageByName(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() name: string,
) {
await permissionCheck(req.user, branchId);
return req.res?.redirect(
await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.branch.img(branchId, name),
12 * 60 * 60,
),
);
}
@Delete("{branchId}/image/{name}")
async deleteImageByName(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() name: string,
) {
await permissionCheck(req.user, branchId);
await minio.removeObject(MINIO_BUCKET, fileLocation.branch.img(branchId, name), {
forceDelete: true,
});
}
}

View file

@ -44,6 +44,7 @@ export const fileLocation = {
image: (branchId: string) => `branch/branch-img-${branchId}`,
map: (branchId: string) => `branch/map-img-${branchId}`,
bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`,
img: (branchId: string, name?: string) => `user/img-${branchId}/${name || ""}`,
},
user: {
profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,