feat: add signature endpoints
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 6s

This commit is contained in:
Methapon2001 2025-03-26 15:17:50 +07:00
parent 1091b93805
commit 4c47b0b7d1
2 changed files with 47 additions and 0 deletions

View file

@ -37,6 +37,7 @@ import {
getPresigned,
listFile,
setFile,
uploadFile,
} from "../utils/minio";
import { filterStatus } from "../services/prisma";
import {
@ -878,3 +879,42 @@ export class UserAttachmentController extends Controller {
);
}
}
@Route("api/v1/user/{userId}/signature")
@Security("keycloak")
export class UserSignatureController extends Controller {
#checkPermission(req: RequestWithUser, userId: string) {
if (req.user.sub !== userId) {
throw new HttpError(
HttpStatus.FORBIDDEN,
"You do not have permission to perform this action.",
"noPermission",
);
}
}
@Get()
async getSignature(@Request() req: RequestWithUser, @Path() userId: string) {
this.#checkPermission(req, userId);
return req.res?.redirect(await getFile(fileLocation.user.signature(userId)));
}
@Put()
async setSignature(
@Request() req: RequestWithUser,
@Path() userId: string,
@Body() signature?: { data: string },
) {
this.#checkPermission(req, userId);
const base64 = signature?.data;
if (base64) {
const buffer = Buffer.from(base64.replace(/^data:image\/\w+;base64,/, ""), "base64");
const mime = "image/" + base64.split(";")[0].split("/")[1];
await uploadFile(fileLocation.user.signature(userId), buffer, mime);
} else {
return await setFile(fileLocation.user.signature(userId));
}
}
}

View file

@ -35,6 +35,12 @@ export async function setFile(path: string, exp = 6 * 60 * 60) {
return await minio.presignedPutObject(MINIO_BUCKET, path, exp);
}
export async function uploadFile(path: string, buffer: Buffer, contentType?: string) {
await minio.putObject(MINIO_BUCKET, path, buffer, Buffer.byteLength(buffer), {
["Content-Type"]: contentType,
});
}
export async function deleteFile(path: string) {
await minio.removeObject(MINIO_BUCKET, path, { forceDelete: true });
}
@ -70,6 +76,7 @@ export const fileLocation = {
`${ROOT}/user/profile-image-${userId}/${name || ""}`,
attachment: (userId: string, name?: string) =>
`${ROOT}/user/attachment-${userId}/${name || ""}`,
signature: (userId: string) => `${ROOT}/user/signature-${userId}`,
},
customer: {
img: (customerId: string, name?: string) => `${ROOT}/customer/img-${customerId}/${name || ""}`,