-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdf-service.ts
145 lines (119 loc) · 4.32 KB
/
pdf-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the page of pdf-service.
*
* @module internal/pdf
*/
import Pdf from '../../entity/file/pdf-file';
import {
Client, FileResponse,
FileSettings,
IPayoutRouteParams,
Language,
ReturnFileType,
} from 'pdf-generator-client';
import { EntityManager } from 'typeorm';
import FileService from '../file-service';
import { PdfError } from '../../errors';
import { IPdfAble, IUnstoredPdfAble } from '../../entity/file/pdf-able';
import WithManager from '../../database/with-manager';
interface IRouteParams {
params: any;
settings: FileSettings;
}
export declare class RouteParams implements IRouteParams {
params: any;
settings: FileSettings;
constructor(data?: IPayoutRouteParams);
static fromJS(data: any): IRouteParams;
toJSON(data?: any): any;
}
export abstract class BasePdfService<T, R extends RouteParams> extends WithManager {
public client: Client;
abstract routeConstructor: new (data: IRouteParams) => R;
stationary = 'BAC';
static getClient(url: string) {
return new Client(url, { fetch });
}
constructor(manager?: EntityManager) {
super(manager);
const PDF_GEN_URL = process.env.PDF_GEN_URL ?? 'http://localhost:3001/pdf';
this.client = BasePdfService.getClient(PDF_GEN_URL);
}
protected getFileSettings(fileType = ReturnFileType.PDF): FileSettings {
return new FileSettings({
createdAt: new Date(),
fileType,
language: Language.ENGLISH,
name: '',
stationery: this.stationary,
});
}
public abstract getParameters(entity: T): Promise<any>;
public abstract generator(routeParams: R): Promise<FileResponse>;
public async getRouteParams(entity: T, fileType = ReturnFileType.PDF): Promise<R> {
const params = await this.getParameters(entity);
const settings = this.getFileSettings(fileType);
return new this.routeConstructor({ params, settings });
}
public async createTex(entity: T): Promise<Buffer> {
const routeParams = await this.getRouteParams(entity, ReturnFileType.TEX);
try {
const res = await this.generator(routeParams);
const blob = res.data;
return Buffer.from(await blob.arrayBuffer());
} catch (res: any) {
throw new PdfError(`Pdf generation failed: ${res.message}`);
}
}
}
export abstract class PdfService<S extends Pdf, T extends IPdfAble<S>, R extends RouteParams> extends BasePdfService<T, R> {
fileService: FileService;
abstract pdfConstructor: new () => S;
constructor(fileLocation: string, manager?: EntityManager) {
super(manager);
this.fileService = new FileService(fileLocation);
}
public async createPdf(entity: T): Promise<S> {
const routeParams = await this.getRouteParams(entity, ReturnFileType.PDF);
const user = await entity.getOwner();
try {
const res = await this.generator(routeParams);
const blob = res.data;
const buffer = Buffer.from(await blob.arrayBuffer());
return await this.fileService.uploadPdf<T, S>(entity, this.pdfConstructor, buffer, user);
} catch (res: any) {
throw new PdfError(`Pdf generation failed: ${res.message}`);
}
}
}
export abstract class UnstoredPdfService<T extends IUnstoredPdfAble, R extends RouteParams> extends BasePdfService<T, R> {
public async createPdf(entity: T): Promise<Buffer> {
const routeParams = await this.getRouteParams(entity);
try {
const res = await this.generator(routeParams);
const blob = res.data;
return Buffer.from(await blob.arrayBuffer());
} catch (res: any) {
throw new PdfError(`Pdf generation failed: ${res.message}`);
}
}
}