feat: image endpoint get or upload

This commit is contained in:
Methapon2001 2024-07-31 11:43:07 +07:00
parent 88559480b7
commit b2c31774f2

View file

@ -658,6 +658,49 @@ export class UserController extends Controller {
where: { id: userId },
});
}
@Get("{userId}/image")
async getUserImageByUserId(@Request() req: RequestWithUser, @Path() userId: string) {
const url = await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(userId), 60 * 60);
if (!url) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
}
return req.res?.redirect(url);
}
@Put("{userId}/image")
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
async setUserImageByUserId(@Request() req: RequestWithUser, @Path() userId: string) {
const record = await prisma.user.findFirst({
include: {
branch: { where: { userId: req.user.sub } },
},
where: {
id: userId,
},
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
}
if (
!["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) &&
!record.branch.some((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, imageLocation(userId), 12 * 60 * 60),
);
}
}
function attachmentLocation(uid: string) {