refactor: helper function
This commit is contained in:
parent
4dee7bfe71
commit
3394ff0caa
1 changed files with 10 additions and 36 deletions
|
|
@ -15,7 +15,6 @@ import {
|
||||||
import { Branch, Prisma, Status, User, UserType } from "@prisma/client";
|
import { Branch, Prisma, Status, User, UserType } from "@prisma/client";
|
||||||
|
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import minio, { deleteFolder } 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";
|
||||||
|
|
@ -29,7 +28,7 @@ import {
|
||||||
removeUserRoles,
|
removeUserRoles,
|
||||||
} from "../services/keycloak";
|
} from "../services/keycloak";
|
||||||
import { isSystem } from "../utils/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 { filterStatus } from "../services/prisma";
|
||||||
import {
|
import {
|
||||||
branchRelationPermInclude,
|
branchRelationPermInclude,
|
||||||
|
|
@ -43,7 +42,6 @@ if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
|
||||||
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager"];
|
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager"];
|
||||||
|
|
||||||
function globalAllow(user: RequestWithUser["user"]) {
|
function globalAllow(user: RequestWithUser["user"]) {
|
||||||
|
|
@ -647,8 +645,8 @@ export class UserController extends Controller {
|
||||||
throw new HttpError(HttpStatus.FORBIDDEN, "User is in used.", "userInUsed");
|
throw new HttpError(HttpStatus.FORBIDDEN, "User is in used.", "userInUsed");
|
||||||
}
|
}
|
||||||
|
|
||||||
await deleteFolder(MINIO_BUCKET, fileLocation.user.profile(userId));
|
await deleteFolder(fileLocation.user.profile(userId));
|
||||||
await deleteFolder(MINIO_BUCKET, fileLocation.user.attachment(userId));
|
await deleteFolder(fileLocation.user.attachment(userId));
|
||||||
await deleteUser(userId);
|
await deleteUser(userId);
|
||||||
|
|
||||||
return await prisma.user.delete({
|
return await prisma.user.delete({
|
||||||
|
|
@ -708,35 +706,21 @@ export class UserProfileController extends Controller {
|
||||||
|
|
||||||
@Get("{name}")
|
@Get("{name}")
|
||||||
async getImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
async getImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
||||||
return req.res?.redirect(
|
return req.res?.redirect(await getFile(fileLocation.user.profile(userId, name)));
|
||||||
await minio.presignedGetObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
fileLocation.user.profile(userId, name),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{name}")
|
@Put("{name}")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
async putImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
async putImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
||||||
await getUserCheckPerm(req.user, userId);
|
await getUserCheckPerm(req.user, userId);
|
||||||
return req.res?.redirect(
|
return req.res?.redirect(await setFile(fileLocation.user.profile(userId, name)));
|
||||||
await minio.presignedPutObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
fileLocation.user.profile(userId, name),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("{name}")
|
@Delete("{name}")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
async deleteImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
async deleteImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
||||||
await getUserCheckPerm(req.user, userId);
|
await getUserCheckPerm(req.user, userId);
|
||||||
await minio.removeObject(MINIO_BUCKET, fileLocation.user.profile(userId, name), {
|
await deleteFile(fileLocation.user.profile(userId, name));
|
||||||
forceDelete: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -759,11 +743,7 @@ export class UserAttachmentController extends Controller {
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
list.map(async (v) => ({
|
list.map(async (v) => ({
|
||||||
name: v,
|
name: v,
|
||||||
url: await minio.presignedGetObject(
|
url: await getFile(fileLocation.user.attachment(userId, v)),
|
||||||
MINIO_BUCKET,
|
|
||||||
fileLocation.user.attachment(userId, v),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -779,12 +759,8 @@ export class UserAttachmentController extends Controller {
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
payload.file.map(async (v) => ({
|
payload.file.map(async (v) => ({
|
||||||
name: v,
|
name: v,
|
||||||
url: await minio.presignedGetObject(MINIO_BUCKET, fileLocation.user.attachment(userId, v)),
|
url: await getFile(fileLocation.user.attachment(userId, v)),
|
||||||
uploadUrl: await minio.presignedPutObject(
|
uploadUrl: await setFile(fileLocation.user.attachment(userId, v), 12 * 60 * 60),
|
||||||
MINIO_BUCKET,
|
|
||||||
fileLocation.user.attachment(userId, v),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -798,9 +774,7 @@ export class UserAttachmentController extends Controller {
|
||||||
await getUserCheckPerm(req.user, userId);
|
await getUserCheckPerm(req.user, userId);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
payload.file.map(async (v) => {
|
payload.file.map(async (v) => {
|
||||||
await minio.removeObject(MINIO_BUCKET, fileLocation.user.attachment(userId, v), {
|
await deleteFile(fileLocation.user.attachment(userId, v));
|
||||||
forceDelete: true,
|
|
||||||
});
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue