1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
11 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.SizeValidator = void 0;
|
15 |
|
16 | const bytes_1 = __importDefault(require("bytes"));
|
17 |
|
18 |
|
19 |
|
20 | class SizeValidator {
|
21 | constructor(file) {
|
22 | this.file = file;
|
23 | this.bytesLimit = 0;
|
24 | this.validated = false;
|
25 | }
|
26 | |
27 |
|
28 |
|
29 | get maxLimit() {
|
30 | return this.maximumAllowedLimit;
|
31 | }
|
32 | set maxLimit(limit) {
|
33 | if (this.maximumAllowedLimit !== undefined) {
|
34 | throw new Error('Cannot reset sizeLimit after file has been validated');
|
35 | }
|
36 | this.validated = false;
|
37 | this.maximumAllowedLimit = limit;
|
38 | if (this.maximumAllowedLimit) {
|
39 | this.bytesLimit =
|
40 | typeof this.maximumAllowedLimit === 'string'
|
41 | ? (0, bytes_1.default)(this.maximumAllowedLimit)
|
42 | : this.maximumAllowedLimit;
|
43 | }
|
44 | }
|
45 | |
46 |
|
47 |
|
48 | reportError() {
|
49 | this.file.errors.push({
|
50 | fieldName: this.file.fieldName,
|
51 | clientName: this.file.clientName,
|
52 | message: `File size should be less than ${(0, bytes_1.default)(this.bytesLimit)}`,
|
53 | type: 'size',
|
54 | });
|
55 | }
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 | validateWhenGettingStreamed() {
|
62 | if (this.file.size > this.bytesLimit) {
|
63 | this.validated = true;
|
64 | this.reportError();
|
65 | }
|
66 | }
|
67 | |
68 |
|
69 |
|
70 |
|
71 | validateAfterConsumed() {
|
72 | this.validated = true;
|
73 | if (this.file.size > this.bytesLimit) {
|
74 | this.reportError();
|
75 | }
|
76 | }
|
77 | |
78 |
|
79 |
|
80 | validate() {
|
81 | if (this.validated) {
|
82 | return;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 |
|
88 | if (this.maximumAllowedLimit === undefined) {
|
89 | this.validated = true;
|
90 | return;
|
91 | }
|
92 | if (this.file.state === 'streaming') {
|
93 | this.validateWhenGettingStreamed();
|
94 | return;
|
95 | }
|
96 | if (this.file.state === 'consumed') {
|
97 | this.validateAfterConsumed();
|
98 | return;
|
99 | }
|
100 | }
|
101 | }
|
102 | exports.SizeValidator = SizeValidator;
|