|
1 | | -import path from "node:path"; |
2 | | -import { Injectable } from "@nestjs/common"; |
3 | | -import { createCanvas, loadImage, registerFont } from "canvas"; |
| 1 | +import { HttpException, Injectable, InternalServerErrorException, NotFoundException } from "@nestjs/common"; |
| 2 | +import { Response } from "express"; |
| 3 | +import { LoggerService } from "src/core/logger/logger.service"; |
4 | 4 | import { PrismaService } from "src/core/prisma/prisma.service"; |
| 5 | +import { S3Service } from "src/core/s3/s3.service"; |
5 | 6 |
|
6 | 7 | @Injectable() |
7 | 8 | export class StudentCertificateService { |
8 | | - constructor(private readonly prisma: PrismaService) {} |
| 9 | + constructor( |
| 10 | + private readonly prisma: PrismaService, |
| 11 | + private readonly s3: S3Service, |
| 12 | + private readonly logger: LoggerService, |
| 13 | + ) {} |
9 | 14 |
|
10 | | - async certificatePreview() {} |
| 15 | + async getCertificatePreview(res: Response, userId: string): Promise<string> { |
| 16 | + try { |
| 17 | + const application = await this.prisma.studentApplication.findMany({ |
| 18 | + where: { |
| 19 | + std_user_id: userId, |
| 20 | + }, |
| 21 | + }); |
11 | 22 |
|
12 | | - async generateImage(name: string): Promise<Buffer> { |
13 | | - // Load background |
14 | | - const bgPath = path.join(__dirname, "./certificate_template.png"); |
15 | | - const background = await loadImage(bgPath); |
| 23 | + const fileKey = `${application[0].std_application_id}.jpg`; |
16 | 24 |
|
17 | | - // Register custom font |
18 | | - const fontPath = path.join(__dirname, "./Arimo-VariableFont_wght.ttf"); |
19 | | - registerFont(fontPath, { family: "CustomFont" }); |
| 25 | + const fileUrl = await this.s3.signedUrl(fileKey, "certificate"); |
20 | 26 |
|
21 | | - // Create canvas |
22 | | - const canvas = createCanvas(background.width, background.height); |
23 | | - const ctx = canvas.getContext("2d"); |
| 27 | + if (!fileUrl) { |
| 28 | + throw new NotFoundException("Cannot find this certificate in storage server"); |
| 29 | + } |
24 | 30 |
|
25 | | - // Draw background |
26 | | - ctx.drawImage(background, 0, 0); |
| 31 | + return fileUrl; |
| 32 | + } catch (e) { |
| 33 | + this.logger.error(e); |
| 34 | + if (e instanceof HttpException) { |
| 35 | + throw e; |
| 36 | + } |
27 | 37 |
|
28 | | - // Set font |
29 | | - ctx.font = '40px "CustomFont"'; |
30 | | - ctx.fillStyle = "#000000"; |
| 38 | + throw new InternalServerErrorException(e); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async getCertificateFull(res: Response, userId: string) { |
| 43 | + try { |
| 44 | + const application = await this.prisma.studentApplication.findMany({ |
| 45 | + where: { |
| 46 | + std_user_id: userId, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + const fileKey = `${application[0].std_application_id}.pdf`; |
| 51 | + |
| 52 | + const fileUrl = await this.s3.signedUrl(fileKey, "certificate"); |
31 | 53 |
|
32 | | - // Center text |
33 | | - const textWidth = ctx.measureText(name).width; |
34 | | - const x = (canvas.width - textWidth) / 2; |
35 | | - const y = canvas.height / 2; |
| 54 | + if (!fileUrl) { |
| 55 | + throw new NotFoundException("Cannot find this certificate in storage server"); |
| 56 | + } |
36 | 57 |
|
37 | | - // Draw name |
38 | | - ctx.fillText(name, x, y); |
| 58 | + return fileUrl; |
| 59 | + } catch (e) { |
| 60 | + this.logger.error(e); |
| 61 | + if (e instanceof HttpException) { |
| 62 | + throw e; |
| 63 | + } |
39 | 64 |
|
40 | | - // Return image buffer |
41 | | - return canvas.toBuffer("image/png"); |
| 65 | + throw new InternalServerErrorException(e); |
| 66 | + } |
42 | 67 | } |
43 | 68 | } |
0 commit comments