feat: image endpoint (branch endpoint)
This commit is contained in:
parent
1b2d06e707
commit
8cd393151e
1 changed files with 83 additions and 1 deletions
|
|
@ -18,7 +18,7 @@ import prisma from "../db";
|
|||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { RequestWithUser } from "../interfaces/user";
|
||||
import minio from "../services/minio";
|
||||
import minio, { presignedGetObjectIfExist } from "../services/minio";
|
||||
|
||||
if (!process.env.MINIO_BUCKET) {
|
||||
throw Error("Require MinIO bucket.");
|
||||
|
|
@ -533,4 +533,86 @@ export class BranchController extends Controller {
|
|||
where: { id: branchId },
|
||||
});
|
||||
}
|
||||
|
||||
@Get("{branchId}/line-image")
|
||||
async getLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const url = await presignedGetObjectIfExist(MINIO_BUCKET, lineImageLoc(branchId), 60 * 60);
|
||||
|
||||
if (!url) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
||||
}
|
||||
|
||||
return req.res?.redirect(url);
|
||||
}
|
||||
|
||||
@Put("{branchId}/line-image")
|
||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
||||
async setLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const record = await prisma.branch.findUnique({
|
||||
include: {
|
||||
user: { where: { userId: req.user.sub } },
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||
}
|
||||
|
||||
if (
|
||||
!["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) &&
|
||||
!record?.user.find((v) => v.userId === req.user.sub)
|
||||
) {
|
||||
throw new HttpError(
|
||||
HttpStatus.FORBIDDEN,
|
||||
"You do not have permission to perform this action.",
|
||||
"noPermission",
|
||||
);
|
||||
}
|
||||
|
||||
return req.res?.redirect(
|
||||
await minio.presignedPutObject(MINIO_BUCKET, lineImageLoc(record.id), 12 * 60 * 60),
|
||||
);
|
||||
}
|
||||
|
||||
@Get("{branchId}/branch-image")
|
||||
async getBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const url = await presignedGetObjectIfExist(MINIO_BUCKET, branchImageLoc(branchId), 60 * 60);
|
||||
|
||||
if (!url) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
||||
}
|
||||
|
||||
return req.res?.redirect(url);
|
||||
}
|
||||
|
||||
@Put("{branchId}/branch-image")
|
||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
||||
async setBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||
const record = await prisma.branch.findUnique({
|
||||
include: {
|
||||
user: { where: { userId: req.user.sub } },
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||
}
|
||||
|
||||
if (
|
||||
!["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) &&
|
||||
!record?.user.find((v) => v.userId === req.user.sub)
|
||||
) {
|
||||
throw new HttpError(
|
||||
HttpStatus.FORBIDDEN,
|
||||
"You do not have permission to perform this action.",
|
||||
"noPermission",
|
||||
);
|
||||
}
|
||||
|
||||
return req.res?.redirect(
|
||||
await minio.presignedPutObject(MINIO_BUCKET, branchImageLoc(record.id), 12 * 60 * 60),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue