@flusys/nestjs-storage 1.1.0-beta → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +153 -8
- package/cjs/config/index.js +0 -1
- package/cjs/config/storage.constants.js +0 -17
- package/cjs/controllers/file-manager.controller.js +44 -1
- package/cjs/controllers/folder.controller.js +44 -1
- package/cjs/controllers/storage-config.controller.js +44 -1
- package/cjs/controllers/upload.controller.js +18 -29
- package/cjs/docs/storage-swagger.config.js +24 -136
- package/cjs/dtos/file-manager.dto.js +71 -35
- package/cjs/dtos/folder.dto.js +15 -9
- package/cjs/dtos/storage-config.dto.js +5 -86
- package/cjs/dtos/upload.dto.js +24 -17
- package/cjs/entities/file-manager-with-company.entity.js +3 -4
- package/cjs/entities/file-manager.entity.js +71 -3
- package/cjs/entities/folder-with-company.entity.js +3 -4
- package/cjs/entities/folder.entity.js +19 -3
- package/cjs/entities/index.js +9 -10
- package/cjs/entities/storage-config-with-company.entity.js +3 -4
- package/cjs/entities/storage-config.entity.js +73 -3
- package/cjs/interfaces/index.js +0 -1
- package/cjs/middlewares/file-serve.middleware.js +113 -100
- package/cjs/modules/storage.module.js +82 -136
- package/cjs/providers/azure-provider.optional.js +10 -38
- package/cjs/providers/local-provider.js +38 -31
- package/cjs/providers/s3-provider.optional.js +19 -40
- package/cjs/providers/storage-factory.service.js +54 -99
- package/cjs/providers/storage-provider.registry.js +8 -18
- package/cjs/services/file-manager.service.js +238 -323
- package/cjs/services/folder.service.js +8 -11
- package/cjs/services/index.js +1 -0
- package/cjs/{config → services}/storage-config.service.js +32 -76
- package/cjs/services/storage-datasource.provider.js +16 -26
- package/cjs/services/storage-provider-config.service.js +15 -37
- package/cjs/services/upload.service.js +72 -88
- package/cjs/utils/file-validator.util.js +458 -0
- package/cjs/utils/image-compressor.util.js +3 -8
- package/config/index.d.ts +0 -1
- package/config/storage.constants.d.ts +0 -8
- package/controllers/upload.controller.d.ts +3 -6
- package/dtos/file-manager.dto.d.ts +13 -7
- package/dtos/folder.dto.d.ts +5 -5
- package/dtos/storage-config.dto.d.ts +9 -16
- package/entities/file-manager-with-company.entity.d.ts +2 -2
- package/entities/file-manager.entity.d.ts +11 -2
- package/entities/folder-with-company.entity.d.ts +2 -2
- package/entities/folder.entity.d.ts +4 -2
- package/entities/index.d.ts +3 -4
- package/entities/storage-config-with-company.entity.d.ts +2 -2
- package/entities/storage-config.entity.d.ts +7 -2
- package/fesm/config/index.js +0 -1
- package/fesm/config/storage.constants.js +0 -8
- package/fesm/controllers/file-manager.controller.js +45 -2
- package/fesm/controllers/folder.controller.js +45 -2
- package/fesm/controllers/storage-config.controller.js +45 -2
- package/fesm/controllers/upload.controller.js +19 -30
- package/fesm/docs/storage-swagger.config.js +27 -142
- package/fesm/dtos/file-manager.dto.js +72 -36
- package/fesm/dtos/folder.dto.js +16 -10
- package/fesm/dtos/storage-config.dto.js +9 -96
- package/fesm/dtos/upload.dto.js +25 -19
- package/fesm/entities/file-manager-with-company.entity.js +3 -4
- package/fesm/entities/file-manager.entity.js +72 -4
- package/fesm/entities/folder-with-company.entity.js +3 -4
- package/fesm/entities/folder.entity.js +20 -4
- package/fesm/entities/index.js +5 -13
- package/fesm/entities/storage-config-with-company.entity.js +3 -4
- package/fesm/entities/storage-config.entity.js +74 -4
- package/fesm/interfaces/index.js +0 -1
- package/fesm/interfaces/storage-config.interface.js +1 -3
- package/fesm/middlewares/file-serve.middleware.js +114 -101
- package/fesm/modules/storage.module.js +83 -136
- package/fesm/providers/azure-provider.optional.js +14 -45
- package/fesm/providers/local-provider.js +38 -31
- package/fesm/providers/s3-provider.optional.js +23 -47
- package/fesm/providers/storage-factory.service.js +52 -97
- package/fesm/providers/storage-provider.registry.js +10 -20
- package/fesm/services/file-manager.service.js +237 -322
- package/fesm/services/folder.service.js +6 -9
- package/fesm/services/index.js +1 -0
- package/fesm/{config → services}/storage-config.service.js +32 -76
- package/fesm/services/storage-datasource.provider.js +16 -26
- package/fesm/services/storage-provider-config.service.js +13 -35
- package/fesm/services/upload.service.js +71 -87
- package/fesm/utils/file-validator.util.js +451 -0
- package/fesm/utils/image-compressor.util.js +3 -8
- package/interfaces/file-manager.interface.d.ts +7 -4
- package/interfaces/index.d.ts +0 -1
- package/interfaces/storage-config.interface.d.ts +1 -22
- package/interfaces/storage-module-options.interface.d.ts +0 -5
- package/middlewares/file-serve.middleware.d.ts +9 -1
- package/modules/storage.module.d.ts +1 -2
- package/package.json +6 -6
- package/providers/azure-provider.optional.d.ts +8 -6
- package/providers/local-provider.d.ts +2 -7
- package/providers/s3-provider.optional.d.ts +9 -7
- package/providers/storage-factory.service.d.ts +8 -9
- package/providers/storage-provider.registry.d.ts +4 -4
- package/services/file-manager.service.d.ts +23 -16
- package/services/folder.service.d.ts +4 -4
- package/services/index.d.ts +1 -0
- package/services/storage-config.service.d.ts +24 -0
- package/services/storage-datasource.provider.d.ts +3 -4
- package/services/storage-provider-config.service.d.ts +5 -6
- package/services/upload.service.d.ts +5 -5
- package/utils/file-validator.util.d.ts +19 -0
- package/cjs/entities/file-manager-base.entity.js +0 -115
- package/cjs/entities/folder-base.entity.js +0 -55
- package/cjs/entities/storage-config-base.entity.js +0 -93
- package/cjs/interfaces/file-upload-response.interface.js +0 -4
- package/config/storage-config.service.d.ts +0 -22
- package/entities/file-manager-base.entity.d.ts +0 -13
- package/entities/folder-base.entity.d.ts +0 -5
- package/entities/storage-config-base.entity.d.ts +0 -9
- package/fesm/entities/file-manager-base.entity.js +0 -108
- package/fesm/entities/folder-base.entity.js +0 -48
- package/fesm/entities/storage-config-base.entity.js +0 -83
- package/fesm/interfaces/file-upload-response.interface.js +0 -1
- package/interfaces/file-upload-response.interface.d.ts +0 -6
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "FileValidator", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return FileValidator;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _common = require("@nestjs/common");
|
|
12
|
+
function _define_property(obj, key, value) {
|
|
13
|
+
if (key in obj) {
|
|
14
|
+
Object.defineProperty(obj, key, {
|
|
15
|
+
value: value,
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true
|
|
19
|
+
});
|
|
20
|
+
} else {
|
|
21
|
+
obj[key] = value;
|
|
22
|
+
}
|
|
23
|
+
return obj;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Magic byte signatures for common file types.
|
|
27
|
+
* Each entry maps a hex signature pattern to its MIME type.
|
|
28
|
+
*/ const MAGIC_BYTES = [
|
|
29
|
+
// Images
|
|
30
|
+
{
|
|
31
|
+
signature: [
|
|
32
|
+
0xff,
|
|
33
|
+
0xd8,
|
|
34
|
+
0xff
|
|
35
|
+
],
|
|
36
|
+
offset: 0,
|
|
37
|
+
mimeType: 'image/jpeg'
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
signature: [
|
|
41
|
+
0x89,
|
|
42
|
+
0x50,
|
|
43
|
+
0x4e,
|
|
44
|
+
0x47,
|
|
45
|
+
0x0d,
|
|
46
|
+
0x0a,
|
|
47
|
+
0x1a,
|
|
48
|
+
0x0a
|
|
49
|
+
],
|
|
50
|
+
offset: 0,
|
|
51
|
+
mimeType: 'image/png'
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
signature: [
|
|
55
|
+
0x47,
|
|
56
|
+
0x49,
|
|
57
|
+
0x46,
|
|
58
|
+
0x38,
|
|
59
|
+
0x37,
|
|
60
|
+
0x61
|
|
61
|
+
],
|
|
62
|
+
offset: 0,
|
|
63
|
+
mimeType: 'image/gif'
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
signature: [
|
|
67
|
+
0x47,
|
|
68
|
+
0x49,
|
|
69
|
+
0x46,
|
|
70
|
+
0x38,
|
|
71
|
+
0x39,
|
|
72
|
+
0x61
|
|
73
|
+
],
|
|
74
|
+
offset: 0,
|
|
75
|
+
mimeType: 'image/gif'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
signature: [
|
|
79
|
+
0x42,
|
|
80
|
+
0x4d
|
|
81
|
+
],
|
|
82
|
+
offset: 0,
|
|
83
|
+
mimeType: 'image/bmp'
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
signature: [
|
|
87
|
+
0x52,
|
|
88
|
+
0x49,
|
|
89
|
+
0x46,
|
|
90
|
+
0x46
|
|
91
|
+
],
|
|
92
|
+
offset: 0,
|
|
93
|
+
mimeType: 'image/webp'
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
signature: [
|
|
97
|
+
0x00,
|
|
98
|
+
0x00,
|
|
99
|
+
0x01,
|
|
100
|
+
0x00
|
|
101
|
+
],
|
|
102
|
+
offset: 0,
|
|
103
|
+
mimeType: 'image/x-icon'
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
signature: [
|
|
107
|
+
0x00,
|
|
108
|
+
0x00,
|
|
109
|
+
0x02,
|
|
110
|
+
0x00
|
|
111
|
+
],
|
|
112
|
+
offset: 0,
|
|
113
|
+
mimeType: 'image/x-icon'
|
|
114
|
+
},
|
|
115
|
+
// Documents
|
|
116
|
+
{
|
|
117
|
+
signature: [
|
|
118
|
+
0x25,
|
|
119
|
+
0x50,
|
|
120
|
+
0x44,
|
|
121
|
+
0x46
|
|
122
|
+
],
|
|
123
|
+
offset: 0,
|
|
124
|
+
mimeType: 'application/pdf'
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
signature: [
|
|
128
|
+
0x50,
|
|
129
|
+
0x4b,
|
|
130
|
+
0x03,
|
|
131
|
+
0x04
|
|
132
|
+
],
|
|
133
|
+
offset: 0,
|
|
134
|
+
mimeType: 'application/zip'
|
|
135
|
+
},
|
|
136
|
+
// Audio
|
|
137
|
+
{
|
|
138
|
+
signature: [
|
|
139
|
+
0x49,
|
|
140
|
+
0x44,
|
|
141
|
+
0x33
|
|
142
|
+
],
|
|
143
|
+
offset: 0,
|
|
144
|
+
mimeType: 'audio/mpeg'
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
signature: [
|
|
148
|
+
0xff,
|
|
149
|
+
0xfb
|
|
150
|
+
],
|
|
151
|
+
offset: 0,
|
|
152
|
+
mimeType: 'audio/mpeg'
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
signature: [
|
|
156
|
+
0xff,
|
|
157
|
+
0xfa
|
|
158
|
+
],
|
|
159
|
+
offset: 0,
|
|
160
|
+
mimeType: 'audio/mpeg'
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
signature: [
|
|
164
|
+
0x4f,
|
|
165
|
+
0x67,
|
|
166
|
+
0x67,
|
|
167
|
+
0x53
|
|
168
|
+
],
|
|
169
|
+
offset: 0,
|
|
170
|
+
mimeType: 'audio/ogg'
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
signature: [
|
|
174
|
+
0x66,
|
|
175
|
+
0x4c,
|
|
176
|
+
0x61,
|
|
177
|
+
0x43
|
|
178
|
+
],
|
|
179
|
+
offset: 0,
|
|
180
|
+
mimeType: 'audio/flac'
|
|
181
|
+
},
|
|
182
|
+
// Video
|
|
183
|
+
{
|
|
184
|
+
signature: [
|
|
185
|
+
0x00,
|
|
186
|
+
0x00,
|
|
187
|
+
0x00,
|
|
188
|
+
0x1c,
|
|
189
|
+
0x66,
|
|
190
|
+
0x74,
|
|
191
|
+
0x79,
|
|
192
|
+
0x70
|
|
193
|
+
],
|
|
194
|
+
offset: 0,
|
|
195
|
+
mimeType: 'video/mp4'
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
signature: [
|
|
199
|
+
0x00,
|
|
200
|
+
0x00,
|
|
201
|
+
0x00,
|
|
202
|
+
0x20,
|
|
203
|
+
0x66,
|
|
204
|
+
0x74,
|
|
205
|
+
0x79,
|
|
206
|
+
0x70
|
|
207
|
+
],
|
|
208
|
+
offset: 0,
|
|
209
|
+
mimeType: 'video/mp4'
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
signature: [
|
|
213
|
+
0x1a,
|
|
214
|
+
0x45,
|
|
215
|
+
0xdf,
|
|
216
|
+
0xa3
|
|
217
|
+
],
|
|
218
|
+
offset: 0,
|
|
219
|
+
mimeType: 'video/webm'
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
signature: [
|
|
223
|
+
0x52,
|
|
224
|
+
0x49,
|
|
225
|
+
0x46,
|
|
226
|
+
0x46
|
|
227
|
+
],
|
|
228
|
+
offset: 0,
|
|
229
|
+
mimeType: 'video/avi'
|
|
230
|
+
},
|
|
231
|
+
// Archives
|
|
232
|
+
{
|
|
233
|
+
signature: [
|
|
234
|
+
0x1f,
|
|
235
|
+
0x8b
|
|
236
|
+
],
|
|
237
|
+
offset: 0,
|
|
238
|
+
mimeType: 'application/gzip'
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
signature: [
|
|
242
|
+
0x37,
|
|
243
|
+
0x7a,
|
|
244
|
+
0xbc,
|
|
245
|
+
0xaf,
|
|
246
|
+
0x27,
|
|
247
|
+
0x1c
|
|
248
|
+
],
|
|
249
|
+
offset: 0,
|
|
250
|
+
mimeType: 'application/x-7z-compressed'
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
signature: [
|
|
254
|
+
0x52,
|
|
255
|
+
0x61,
|
|
256
|
+
0x72,
|
|
257
|
+
0x21,
|
|
258
|
+
0x1a,
|
|
259
|
+
0x07
|
|
260
|
+
],
|
|
261
|
+
offset: 0,
|
|
262
|
+
mimeType: 'application/x-rar-compressed'
|
|
263
|
+
}
|
|
264
|
+
];
|
|
265
|
+
/**
|
|
266
|
+
* MIME type aliases - types that are equivalent.
|
|
267
|
+
*/ const MIME_ALIASES = {
|
|
268
|
+
'image/jpeg': [
|
|
269
|
+
'image/jpg'
|
|
270
|
+
],
|
|
271
|
+
'image/jpg': [
|
|
272
|
+
'image/jpeg'
|
|
273
|
+
],
|
|
274
|
+
'video/mp4': [
|
|
275
|
+
'video/quicktime'
|
|
276
|
+
],
|
|
277
|
+
'application/zip': [
|
|
278
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
279
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
280
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
|
281
|
+
]
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* File types that are text-based and don't have magic bytes.
|
|
285
|
+
* SECURITY NOTE: Dangerous types (HTML, JS, SVG) are excluded as they can contain scripts.
|
|
286
|
+
* These types require explicit allowlisting and additional content scanning.
|
|
287
|
+
*/ const TEXT_BASED_TYPES = [
|
|
288
|
+
'text/plain',
|
|
289
|
+
'text/csv',
|
|
290
|
+
'text/markdown',
|
|
291
|
+
'application/json',
|
|
292
|
+
'application/xml',
|
|
293
|
+
'application/typescript',
|
|
294
|
+
'text/css'
|
|
295
|
+
];
|
|
296
|
+
/**
|
|
297
|
+
* Dangerous text-based types that can execute scripts.
|
|
298
|
+
* These bypass magic-bytes validation but require explicit allowlisting.
|
|
299
|
+
*/ const DANGEROUS_TEXT_TYPES = [
|
|
300
|
+
'text/html',
|
|
301
|
+
'application/javascript',
|
|
302
|
+
'text/javascript',
|
|
303
|
+
'image/svg+xml',
|
|
304
|
+
'application/xhtml+xml'
|
|
305
|
+
];
|
|
306
|
+
/**
|
|
307
|
+
* ZIP-based format prefixes that are valid when detected as application/zip.
|
|
308
|
+
*/ const ZIP_VARIANT_PREFIXES = [
|
|
309
|
+
'application/vnd.openxmlformats-officedocument',
|
|
310
|
+
'application/x-zip',
|
|
311
|
+
'application/x-compressed'
|
|
312
|
+
];
|
|
313
|
+
let FileValidator = class FileValidator {
|
|
314
|
+
// Result Helpers
|
|
315
|
+
static failureResult(message, detectedType, declaredType) {
|
|
316
|
+
return {
|
|
317
|
+
valid: false,
|
|
318
|
+
detectedType,
|
|
319
|
+
declaredType,
|
|
320
|
+
message
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
static successResult(detectedType, declaredType) {
|
|
324
|
+
return {
|
|
325
|
+
valid: true,
|
|
326
|
+
detectedType,
|
|
327
|
+
declaredType
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Detect file type from buffer using magic bytes.
|
|
332
|
+
* @param buffer - File buffer to analyze
|
|
333
|
+
* @returns Detected MIME type or null if unknown
|
|
334
|
+
*/ static detectFileType(buffer) {
|
|
335
|
+
for (const { signature, offset, mimeType } of MAGIC_BYTES){
|
|
336
|
+
if (buffer.length < offset + signature.length) continue;
|
|
337
|
+
let matches = true;
|
|
338
|
+
for(let i = 0; i < signature.length; i++){
|
|
339
|
+
if (buffer[offset + i] !== signature[i]) {
|
|
340
|
+
matches = false;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (matches) {
|
|
345
|
+
return mimeType;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Check if a MIME type is text-based (doesn't have magic bytes).
|
|
352
|
+
*/ static isTextBasedType(mimeType) {
|
|
353
|
+
return TEXT_BASED_TYPES.some((t)=>mimeType.startsWith(t) || mimeType === t);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Check if a MIME type is a dangerous text type that can execute scripts.
|
|
357
|
+
*/ static isDangerousTextType(mimeType) {
|
|
358
|
+
return DANGEROUS_TEXT_TYPES.some((t)=>mimeType === t);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Check if two MIME types are compatible (exact match or aliases).
|
|
362
|
+
*/ static mimeTypesMatch(detected, declared) {
|
|
363
|
+
// Exact match
|
|
364
|
+
if (detected === declared) return true;
|
|
365
|
+
// Check aliases
|
|
366
|
+
const aliases = MIME_ALIASES[detected];
|
|
367
|
+
if (aliases?.includes(declared)) return true;
|
|
368
|
+
// Check reverse aliases
|
|
369
|
+
const reverseAliases = MIME_ALIASES[declared];
|
|
370
|
+
if (reverseAliases?.includes(detected)) return true;
|
|
371
|
+
// Check if both are in same category (e.g., both images)
|
|
372
|
+
const detectedCategory = detected.split('/')[0];
|
|
373
|
+
const declaredCategory = declared.split('/')[0];
|
|
374
|
+
// For ZIP-based formats, allow any ZIP-detected file if declared is a ZIP variant
|
|
375
|
+
if (detected === 'application/zip' && ZIP_VARIANT_PREFIXES.some((v)=>declared.startsWith(v))) {
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
return detectedCategory === declaredCategory;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Check if a MIME type is in the allowed list.
|
|
382
|
+
*/ static isTypeAllowed(mimeType, allowedTypes) {
|
|
383
|
+
// Wildcard allows all
|
|
384
|
+
if (allowedTypes.includes('*/*')) return true;
|
|
385
|
+
return allowedTypes.some((allowed)=>{
|
|
386
|
+
// Category wildcard (e.g., "image/*")
|
|
387
|
+
if (allowed.endsWith('/*')) {
|
|
388
|
+
const category = allowed.slice(0, -2);
|
|
389
|
+
return mimeType.startsWith(category);
|
|
390
|
+
}
|
|
391
|
+
return allowed === mimeType;
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Validate file content matches declared MIME type using magic bytes.
|
|
396
|
+
* @param buffer - File buffer
|
|
397
|
+
* @param declaredMimeType - MIME type declared by client
|
|
398
|
+
* @param allowedTypes - List of allowed MIME types/patterns
|
|
399
|
+
* @returns Validation result
|
|
400
|
+
*/ static validateFileContent(buffer, declaredMimeType, allowedTypes = [
|
|
401
|
+
'*/*'
|
|
402
|
+
]) {
|
|
403
|
+
try {
|
|
404
|
+
const detectedType = this.detectFileType(buffer);
|
|
405
|
+
// No magic bytes detected - handle text-based types
|
|
406
|
+
if (!detectedType) {
|
|
407
|
+
return this.validateUndetectedType(declaredMimeType, allowedTypes);
|
|
408
|
+
}
|
|
409
|
+
// Verify detected type matches declared type
|
|
410
|
+
if (!this.mimeTypesMatch(detectedType, declaredMimeType)) {
|
|
411
|
+
this.logger.warn(`MIME type mismatch: declared=${declaredMimeType}, detected=${detectedType}`);
|
|
412
|
+
return this.failureResult(`File content does not match declared type. Detected: ${detectedType}, Declared: ${declaredMimeType}`, detectedType, declaredMimeType);
|
|
413
|
+
}
|
|
414
|
+
// Verify type is in allowed list
|
|
415
|
+
if (!this.isTypeAllowed(detectedType, allowedTypes)) {
|
|
416
|
+
return this.failureResult(`File type "${detectedType}" is not allowed`, detectedType, declaredMimeType);
|
|
417
|
+
}
|
|
418
|
+
return this.successResult(detectedType, declaredMimeType);
|
|
419
|
+
} catch (error) {
|
|
420
|
+
this.logger.error('File validation error:', error);
|
|
421
|
+
return this.failureResult('File validation failed');
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Handle validation for files without detectable magic bytes.
|
|
426
|
+
*/ static validateUndetectedType(declaredMimeType, allowedTypes) {
|
|
427
|
+
// Check for dangerous text types first (HTML, JS, SVG)
|
|
428
|
+
if (this.isDangerousTextType(declaredMimeType)) {
|
|
429
|
+
const explicitlyAllowed = allowedTypes.some((t)=>t === declaredMimeType && t !== '*/*' && !t.endsWith('/*'));
|
|
430
|
+
if (!explicitlyAllowed) {
|
|
431
|
+
this.logger.warn(`Blocked dangerous file type: ${declaredMimeType} - requires explicit allowlisting`);
|
|
432
|
+
return this.failureResult(`File type "${declaredMimeType}" is potentially dangerous and not explicitly allowed`, declaredMimeType, declaredMimeType);
|
|
433
|
+
}
|
|
434
|
+
this.logger.warn(`Allowing explicitly permitted dangerous file type: ${declaredMimeType}`);
|
|
435
|
+
}
|
|
436
|
+
// Safe text-based files don't have magic bytes, trust declared type
|
|
437
|
+
if (this.isTextBasedType(declaredMimeType)) {
|
|
438
|
+
const isAllowed = this.isTypeAllowed(declaredMimeType, allowedTypes);
|
|
439
|
+
return isAllowed ? this.successResult(declaredMimeType, declaredMimeType) : this.failureResult(`File type "${declaredMimeType}" is not allowed`, declaredMimeType, declaredMimeType);
|
|
440
|
+
}
|
|
441
|
+
// Binary files without recognized signatures - be cautious
|
|
442
|
+
this.logger.warn(`Unable to detect file type for declared type: ${declaredMimeType}`);
|
|
443
|
+
return this.failureResult('Unable to verify file type. File may be corrupted or unsupported.', undefined, declaredMimeType);
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Sanitize filename to prevent path traversal and special character issues.
|
|
447
|
+
* @param filename - Original filename
|
|
448
|
+
* @returns Sanitized filename
|
|
449
|
+
*/ static sanitizeFilename(filename) {
|
|
450
|
+
return filename// Remove path components (prevent traversal)
|
|
451
|
+
.replace(/^.*[\\\/]/, '')// Remove null bytes
|
|
452
|
+
.replace(/\0/g, '')// Replace multiple dots with single
|
|
453
|
+
.replace(/\.{2,}/g, '.')// Remove special characters except allowed ones
|
|
454
|
+
.replace(/[^a-zA-Z0-9._-]/g, '_')// Limit length
|
|
455
|
+
.substring(0, 255);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
_define_property(FileValidator, "logger", new _common.Logger(FileValidator.name));
|
|
@@ -147,17 +147,12 @@ let ImageCompressor = class ImageCompressor {
|
|
|
147
147
|
break;
|
|
148
148
|
}
|
|
149
149
|
try {
|
|
150
|
-
|
|
151
|
-
const { data, info } = await image.toBuffer({
|
|
152
|
-
resolveWithObject: true
|
|
153
|
-
});
|
|
150
|
+
const compressedBuffer = await image.toBuffer();
|
|
154
151
|
return {
|
|
155
|
-
buffer:
|
|
152
|
+
buffer: compressedBuffer,
|
|
156
153
|
format: `image/${targetFormat}`
|
|
157
154
|
};
|
|
158
|
-
} catch
|
|
159
|
-
// Fallback to original if processing fails
|
|
160
|
-
console.warn(`Image processing failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
155
|
+
} catch {
|
|
161
156
|
return {
|
|
162
157
|
buffer,
|
|
163
158
|
format: mimetype
|
package/config/index.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
1
|
export declare const STORAGE_MODULE_OPTIONS = "STORAGE_MODULE_OPTIONS";
|
|
2
|
-
export declare const STORAGE_CONFIG_SERVICE = "STORAGE_CONFIG_SERVICE";
|
|
3
|
-
export declare const STORAGE_DATA_SOURCE_PROVIDER = "STORAGE_DATA_SOURCE_PROVIDER";
|
|
4
2
|
export declare const DEFAULT_MAX_FILE_SIZE: number;
|
|
5
3
|
export declare const DEFAULT_ALLOWED_FILE_TYPES: string[];
|
|
6
|
-
export declare const FILE_VALIDATION_MESSAGES: {
|
|
7
|
-
readonly FILE_TOO_LARGE: "File size exceeds the maximum allowed size";
|
|
8
|
-
readonly INVALID_FILE_TYPE: "File type is not allowed";
|
|
9
|
-
readonly NO_FILE_PROVIDED: "No file was provided";
|
|
10
|
-
readonly UPLOAD_FAILED: "File upload failed";
|
|
11
|
-
};
|
|
@@ -2,13 +2,10 @@ import { SingleResponseDto } from '@flusys/nestjs-shared/dtos';
|
|
|
2
2
|
import { ILoggedUserInfo } from '@flusys/nestjs-shared/interfaces';
|
|
3
3
|
import { DeleteMultipleFileDto, DeleteSingleFileDto, FileUploadResponsePayloadDto, UploadOptionsDto } from '../dtos';
|
|
4
4
|
import { UploadService } from '../services/upload.service';
|
|
5
|
-
import { StorageConfigService } from '../config';
|
|
6
|
-
import { StorageFactoryService } from '../providers/storage-factory.service';
|
|
7
5
|
export declare class UploadController {
|
|
8
|
-
private uploadService;
|
|
9
|
-
|
|
10
|
-
private
|
|
11
|
-
constructor(uploadService: UploadService, storageConfigService: StorageConfigService, storageFactoryService: StorageFactoryService);
|
|
6
|
+
private readonly uploadService;
|
|
7
|
+
constructor(uploadService: UploadService);
|
|
8
|
+
private toFileUploadResponse;
|
|
12
9
|
uploadSingleFile(file: Express.Multer.File, options: UploadOptionsDto, user: ILoggedUserInfo): Promise<SingleResponseDto<FileUploadResponsePayloadDto>>;
|
|
13
10
|
uploadMultipleFiles(files: Express.Multer.File[], options: UploadOptionsDto, user: ILoggedUserInfo): Promise<SingleResponseDto<FileUploadResponsePayloadDto[]>>;
|
|
14
11
|
deleteSingleFile(dto: DeleteSingleFileDto, user: ILoggedUserInfo): Promise<SingleResponseDto<boolean>>;
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export declare class CreateFileManagerDto implements Record<string, unknown> {
|
|
3
|
-
[key: string]: unknown;
|
|
1
|
+
export declare class CreateFileManagerDto {
|
|
4
2
|
name: string;
|
|
5
3
|
key: string;
|
|
6
4
|
size: string;
|
|
@@ -8,14 +6,22 @@ export declare class CreateFileManagerDto implements Record<string, unknown> {
|
|
|
8
6
|
isPrivate: boolean;
|
|
9
7
|
folderId?: string;
|
|
10
8
|
storageConfigId?: string;
|
|
11
|
-
location?:
|
|
9
|
+
location?: string;
|
|
12
10
|
}
|
|
13
11
|
declare const UpdateFileManagerDto_base: import("@nestjs/common").Type<Partial<CreateFileManagerDto>>;
|
|
14
|
-
export declare class UpdateFileManagerDto extends UpdateFileManagerDto_base
|
|
15
|
-
[key: string]: unknown;
|
|
12
|
+
export declare class UpdateFileManagerDto extends UpdateFileManagerDto_base {
|
|
16
13
|
id: string;
|
|
17
14
|
}
|
|
18
|
-
export declare class FileManagerResponseDto
|
|
15
|
+
export declare class FileManagerResponseDto {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
key: string;
|
|
19
|
+
size: string;
|
|
20
|
+
contentType: string;
|
|
21
|
+
isPrivate: boolean;
|
|
22
|
+
folderId?: string;
|
|
23
|
+
storageConfigId?: string;
|
|
24
|
+
location?: string;
|
|
19
25
|
providerName?: string;
|
|
20
26
|
}
|
|
21
27
|
export declare class GetFilesRequestDto {
|
package/dtos/folder.dto.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export declare class CreateFolderDto
|
|
2
|
-
[key: string]: unknown;
|
|
1
|
+
export declare class CreateFolderDto {
|
|
3
2
|
name: string;
|
|
4
3
|
}
|
|
5
4
|
declare const UpdateFolderDto_base: import("@nestjs/common").Type<Partial<CreateFolderDto>>;
|
|
6
|
-
export declare class UpdateFolderDto extends UpdateFolderDto_base
|
|
7
|
-
[key: string]: unknown;
|
|
5
|
+
export declare class UpdateFolderDto extends UpdateFolderDto_base {
|
|
8
6
|
id: string;
|
|
9
7
|
}
|
|
10
|
-
export declare class FolderResponseDto
|
|
8
|
+
export declare class FolderResponseDto {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
11
|
slug: string;
|
|
12
12
|
}
|
|
13
13
|
export {};
|
|
@@ -1,27 +1,20 @@
|
|
|
1
1
|
import { IdentityResponseDto } from '@flusys/nestjs-shared/dtos';
|
|
2
|
-
|
|
3
|
-
export declare class CreateStorageConfigDto implements Record<string, unknown> {
|
|
4
|
-
[key: string]: unknown;
|
|
2
|
+
export declare class CreateStorageConfigDto {
|
|
5
3
|
name: string;
|
|
6
|
-
storage:
|
|
7
|
-
config: Record<string,
|
|
4
|
+
storage: string;
|
|
5
|
+
config: Record<string, unknown>;
|
|
8
6
|
isActive?: boolean;
|
|
9
7
|
isDefault?: boolean;
|
|
10
8
|
}
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
declare const UpdateStorageConfigDto_base: import("@nestjs/common").Type<Partial<Omit<CreateStorageConfigDto, never>>>;
|
|
10
|
+
export declare class UpdateStorageConfigDto extends UpdateStorageConfigDto_base {
|
|
13
11
|
id: string;
|
|
14
|
-
name?: string;
|
|
15
|
-
storage?: FileLocationEnum;
|
|
16
|
-
config?: Record<string, any>;
|
|
17
|
-
isActive?: boolean;
|
|
18
|
-
isDefault?: boolean;
|
|
19
12
|
}
|
|
20
|
-
export declare class StorageConfigResponseDto extends IdentityResponseDto
|
|
21
|
-
[key: string]: unknown;
|
|
13
|
+
export declare class StorageConfigResponseDto extends IdentityResponseDto {
|
|
22
14
|
name: string;
|
|
23
|
-
storage:
|
|
24
|
-
config: Record<string,
|
|
15
|
+
storage: string;
|
|
16
|
+
config: Record<string, unknown>;
|
|
25
17
|
isActive: boolean;
|
|
26
18
|
isDefault: boolean;
|
|
27
19
|
}
|
|
20
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FileManager } from './file-manager.entity';
|
|
2
2
|
import { FolderWithCompany } from './folder-with-company.entity';
|
|
3
|
-
export declare class FileManagerWithCompany extends
|
|
3
|
+
export declare class FileManagerWithCompany extends FileManager {
|
|
4
4
|
companyId: string | null;
|
|
5
5
|
folder: FolderWithCompany | null;
|
|
6
6
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Identity } from '@flusys/nestjs-shared/entities';
|
|
2
2
|
import { Folder } from './folder.entity';
|
|
3
|
-
export declare class FileManager extends
|
|
3
|
+
export declare class FileManager extends Identity {
|
|
4
|
+
name: string;
|
|
5
|
+
contentType: string;
|
|
6
|
+
size: string;
|
|
7
|
+
key: string;
|
|
8
|
+
url: string | null;
|
|
9
|
+
location: string;
|
|
10
|
+
storageConfigId: string | null;
|
|
11
|
+
expiresAt: number | null;
|
|
12
|
+
isPrivate: boolean;
|
|
4
13
|
folder: Folder | null;
|
|
5
14
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class FolderWithCompany extends
|
|
1
|
+
import { Folder } from './folder.entity';
|
|
2
|
+
export declare class FolderWithCompany extends Folder {
|
|
3
3
|
companyId: string | null;
|
|
4
4
|
fileManager: any[];
|
|
5
5
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class Folder extends
|
|
1
|
+
import { Identity } from '@flusys/nestjs-shared/entities';
|
|
2
|
+
export declare class Folder extends Identity {
|
|
3
|
+
name: string;
|
|
4
|
+
slug: string;
|
|
3
5
|
fileManager: any[];
|
|
4
6
|
}
|
package/entities/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
export * from './file-manager-base.entity';
|
|
2
|
-
export * from './folder-base.entity';
|
|
3
|
-
export * from './storage-config-base.entity';
|
|
4
1
|
export * from './file-manager.entity';
|
|
5
2
|
export * from './folder.entity';
|
|
6
3
|
export * from './storage-config.entity';
|
|
@@ -15,5 +12,7 @@ import { FolderWithCompany } from './folder-with-company.entity';
|
|
|
15
12
|
import { StorageConfigWithCompany } from './storage-config-with-company.entity';
|
|
16
13
|
export declare const StorageCoreEntities: (typeof Folder | typeof FileManager | typeof StorageConfig)[];
|
|
17
14
|
export declare const StorageCompanyEntities: (typeof FolderWithCompany | typeof FileManagerWithCompany | typeof StorageConfigWithCompany)[];
|
|
18
|
-
export declare const StorageAllEntities: (typeof Folder | typeof FileManager | typeof StorageConfig)[];
|
|
19
15
|
export declare function getStorageEntitiesByConfig(enableCompanyFeature: boolean): any[];
|
|
16
|
+
export { FileManager as FileManagerBase } from './file-manager.entity';
|
|
17
|
+
export { Folder as FolderBase } from './folder.entity';
|
|
18
|
+
export { StorageConfig as StorageConfigBase } from './storage-config.entity';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class StorageConfigWithCompany extends
|
|
1
|
+
import { StorageConfig } from './storage-config.entity';
|
|
2
|
+
export declare class StorageConfigWithCompany extends StorageConfig {
|
|
3
3
|
companyId: string | null;
|
|
4
4
|
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class StorageConfig extends
|
|
1
|
+
import { Identity } from '@flusys/nestjs-shared/entities';
|
|
2
|
+
export declare class StorageConfig extends Identity {
|
|
3
|
+
name: string;
|
|
4
|
+
storage: string;
|
|
5
|
+
config: Record<string, any>;
|
|
6
|
+
isActive: boolean;
|
|
7
|
+
isDefault: boolean;
|
|
3
8
|
}
|