refactor: helper function

This commit is contained in:
Methapon Metanipat 2024-09-13 16:16:14 +07:00
parent 4dee7bfe71
commit 3394ff0caa

View file

@ -15,7 +15,6 @@ import {
import { Branch, Prisma, Status, User, UserType } from "@prisma/client";
import prisma from "../db";
import minio, { deleteFolder } from "../services/minio";
import { RequestWithUser } from "../interfaces/user";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
@ -29,7 +28,7 @@ import {
removeUserRoles,
} from "../services/keycloak";
import { isSystem } from "../utils/keycloak";
import { fileLocation, listFile } from "../utils/minio";
import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio";
import { filterStatus } from "../services/prisma";
import {
branchRelationPermInclude,
@ -43,7 +42,6 @@ if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager"];
function globalAllow(user: RequestWithUser["user"]) {
@ -647,8 +645,8 @@ export class UserController extends Controller {
throw new HttpError(HttpStatus.FORBIDDEN, "User is in used.", "userInUsed");
}
await deleteFolder(MINIO_BUCKET, fileLocation.user.profile(userId));
await deleteFolder(MINIO_BUCKET, fileLocation.user.attachment(userId));
await deleteFolder(fileLocation.user.profile(userId));
await deleteFolder(fileLocation.user.attachment(userId));
await deleteUser(userId);
return await prisma.user.delete({
@ -708,35 +706,21 @@ export class UserProfileController extends Controller {
@Get("{name}")
async getImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
return req.res?.redirect(
await minio.presignedGetObject(
MINIO_BUCKET,
fileLocation.user.profile(userId, name),
12 * 60 * 60,
),
);
return req.res?.redirect(await getFile(fileLocation.user.profile(userId, name)));
}
@Put("{name}")
@Security("keycloak")
async putImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
await getUserCheckPerm(req.user, userId);
return req.res?.redirect(
await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.user.profile(userId, name),
12 * 60 * 60,
),
);
return req.res?.redirect(await setFile(fileLocation.user.profile(userId, name)));
}
@Delete("{name}")
@Security("keycloak")
async deleteImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
await getUserCheckPerm(req.user, userId);
await minio.removeObject(MINIO_BUCKET, fileLocation.user.profile(userId, name), {
forceDelete: true,
});
await deleteFile(fileLocation.user.profile(userId, name));
}
}
@ -759,11 +743,7 @@ export class UserAttachmentController extends Controller {
return await Promise.all(
list.map(async (v) => ({
name: v,
url: await minio.presignedGetObject(
MINIO_BUCKET,
fileLocation.user.attachment(userId, v),
12 * 60 * 60,
),
url: await getFile(fileLocation.user.attachment(userId, v)),
})),
);
}
@ -779,12 +759,8 @@ export class UserAttachmentController extends Controller {
return await Promise.all(
payload.file.map(async (v) => ({
name: v,
url: await minio.presignedGetObject(MINIO_BUCKET, fileLocation.user.attachment(userId, v)),
uploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.user.attachment(userId, v),
12 * 60 * 60,
),
url: await getFile(fileLocation.user.attachment(userId, v)),
uploadUrl: await setFile(fileLocation.user.attachment(userId, v), 12 * 60 * 60),
})),
);
}
@ -798,9 +774,7 @@ export class UserAttachmentController extends Controller {
await getUserCheckPerm(req.user, userId);
await Promise.all(
payload.file.map(async (v) => {
await minio.removeObject(MINIO_BUCKET, fileLocation.user.attachment(userId, v), {
forceDelete: true,
});
await deleteFile(fileLocation.user.attachment(userId, v));
}),
);
}