-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdf-able.ts
144 lines (120 loc) · 3.82 KB
/
pdf-able.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
/**
* 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 module page of the pdf-able.
*
* @module internal/files/pdf
*/
import Pdf from './pdf-file';
import BaseEntity from '../base-entity';
import User from '../user/user';
import { PdfService, RouteParams, UnstoredPdfService } from '../../service/pdf/pdf-service';
import { hashJSON } from '../../helpers/hash';
type Constructor<T = {}> = new (...args: any[]) => T;
export interface IPdfAble<S extends Pdf = Pdf> extends BaseEntity {
pdf?: S,
getPdfParamHash(): Promise<string>,
createPdf(): Promise<S>
getOwner(): Promise<User>
}
/**
* PdfAble is a Mixin that allows entities to be converted to Pdf.
* @param Base
* @constructor
*/
export function PdfAble<TBase extends Constructor<BaseEntity>>(Base: TBase) {
abstract class PdfAbleClass extends Base {
/**
* The id of the Pdf file.
*/
abstract pdfId?: number;
/**
* The Pdf file.
*/
abstract pdf?: Pdf;
/**
* The service that creates the Pdf file.
*/
abstract pdfService: PdfService<Pdf, this, RouteParams>;
/**
* Get the owner of the Pdf file.
* Needed for the File.createdBy field.
*/
abstract getOwner(): Promise<User>;
/**
* Create the Pdf file.
*/
async createPdf(): Promise<Pdf> {
return this.pdfService.createPdf(this as any);
}
/**
* Get the hash of the parameters of the Pdf file.
*/
async getPdfParamHash(): Promise<string> {
return hashJSON(await this.pdfService.getParameters(this as any));
}
/**
* Get the Pdf file.
* If the Pdf file is not current, create it.
* @param force If true, always create the Pdf file.
*/
public async getOrCreatePdf(force: boolean = false): Promise<Pdf> {
if (this.pdf && !force) {
// check if pdf is current.
if (await this.validatePdfHash()) return this.pdf;
}
return Promise.resolve(await this.createPdf());
}
/**
* Validates if the Pdf matches the stored hash.
*/
async validatePdfHash(): Promise<boolean> {
if (!this.pdf) return false;
const hash = await this.getPdfParamHash();
return hash === this.pdf.hash;
}
}
return PdfAbleClass;
}
export interface IUnstoredPdfAble {
createPdf(): Promise<Buffer>;
}
export function UnstoredPdfAble<TBase extends Constructor>(Base: TBase) {
abstract class UnstoredPdfAbleClass extends Base implements IUnstoredPdfAble {
/**
* The service that creates the Pdf buffer.
*/
abstract pdfService: UnstoredPdfService<UnstoredPdfAbleClass, RouteParams>;
/**
* Create the Pdf buffer.
* This method generates the PDF and returns it as a Buffer.
*/
async createPdf(): Promise<Buffer> {
return this.pdfService.createPdf(this as UnstoredPdfAbleClass);
}
async createTex(): Promise<Buffer> {
return this.pdfService.createTex(this as UnstoredPdfAbleClass);
}
async getPdfParamHash(): Promise<string> {
return hashJSON(await this.pdfService.getParameters(this as UnstoredPdfAbleClass));
}
}
return UnstoredPdfAbleClass;
}