@adminforth/upload 2.7.2 → 2.8.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/dist/index.js +35 -32
- package/index.ts +43 -34
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -29,6 +29,40 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
29
29
|
}
|
|
30
30
|
return this.options.storageAdapter.getDownloadUrl(path, expiresInSeconds);
|
|
31
31
|
});
|
|
32
|
+
this.getFileUploadUrl = (originalFilename, contentType, size, originalExtension, recordPk) => __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
if (this.options.allowedFileExtensions && !this.options.allowedFileExtensions.includes(originalExtension.toLowerCase())) {
|
|
35
|
+
return {
|
|
36
|
+
error: `File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
let record = undefined;
|
|
40
|
+
if (recordPk) {
|
|
41
|
+
// get record by recordPk
|
|
42
|
+
const pkName = (_a = this.resourceConfig.columns.find((column) => column.primaryKey)) === null || _a === void 0 ? void 0 : _a.name;
|
|
43
|
+
record = yield this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ(pkName, recordPk)]);
|
|
44
|
+
}
|
|
45
|
+
const filePath = this.options.filePath({ originalFilename, originalExtension, contentType, record });
|
|
46
|
+
if (filePath.startsWith('/')) {
|
|
47
|
+
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
|
|
48
|
+
}
|
|
49
|
+
const { uploadUrl, uploadExtraParams } = yield this.options.storageAdapter.getUploadSignedUrl(filePath, contentType, 1800);
|
|
50
|
+
let previewUrl;
|
|
51
|
+
if ((_b = this.options.preview) === null || _b === void 0 ? void 0 : _b.previewUrl) {
|
|
52
|
+
previewUrl = this.options.preview.previewUrl({ filePath });
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
previewUrl = yield this.options.storageAdapter.getDownloadUrl(filePath, 1800);
|
|
56
|
+
}
|
|
57
|
+
const tagline = `${ADMINFORTH_NOT_YET_USED_TAG}=true`;
|
|
58
|
+
return {
|
|
59
|
+
uploadUrl,
|
|
60
|
+
tagline,
|
|
61
|
+
filePath,
|
|
62
|
+
uploadExtraParams,
|
|
63
|
+
previewUrl,
|
|
64
|
+
};
|
|
65
|
+
});
|
|
32
66
|
if ((_b = (_a = this.options.generation) === null || _a === void 0 ? void 0 : _a.rateLimit) === null || _b === void 0 ? void 0 : _b.limit) {
|
|
33
67
|
this.rateLimiter = new RateLimiter((_c = this.options.generation.rateLimit) === null || _c === void 0 ? void 0 : _c.limit);
|
|
34
68
|
}
|
|
@@ -319,39 +353,8 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
319
353
|
method: 'POST',
|
|
320
354
|
path: `/plugin/${this.pluginInstanceId}/get_file_upload_url`,
|
|
321
355
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
|
|
322
|
-
var _b, _c;
|
|
323
356
|
const { originalFilename, contentType, size, originalExtension, recordPk } = body;
|
|
324
|
-
|
|
325
|
-
return {
|
|
326
|
-
error: `File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
let record = undefined;
|
|
330
|
-
if (recordPk) {
|
|
331
|
-
// get record by recordPk
|
|
332
|
-
const pkName = (_b = this.resourceConfig.columns.find((column) => column.primaryKey)) === null || _b === void 0 ? void 0 : _b.name;
|
|
333
|
-
record = yield this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ(pkName, recordPk)]);
|
|
334
|
-
}
|
|
335
|
-
const filePath = this.options.filePath({ originalFilename, originalExtension, contentType, record });
|
|
336
|
-
if (filePath.startsWith('/')) {
|
|
337
|
-
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
|
|
338
|
-
}
|
|
339
|
-
const { uploadUrl, uploadExtraParams } = yield this.options.storageAdapter.getUploadSignedUrl(filePath, contentType, 1800);
|
|
340
|
-
let previewUrl;
|
|
341
|
-
if ((_c = this.options.preview) === null || _c === void 0 ? void 0 : _c.previewUrl) {
|
|
342
|
-
previewUrl = this.options.preview.previewUrl({ filePath });
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
previewUrl = yield this.options.storageAdapter.getDownloadUrl(filePath, 1800);
|
|
346
|
-
}
|
|
347
|
-
const tagline = `${ADMINFORTH_NOT_YET_USED_TAG}=true`;
|
|
348
|
-
return {
|
|
349
|
-
uploadUrl,
|
|
350
|
-
tagline,
|
|
351
|
-
filePath,
|
|
352
|
-
uploadExtraParams,
|
|
353
|
-
previewUrl,
|
|
354
|
-
};
|
|
357
|
+
return this.getFileUploadUrl(originalFilename, contentType, size, originalExtension, recordPk);
|
|
355
358
|
})
|
|
356
359
|
});
|
|
357
360
|
// generation: {
|
package/index.ts
CHANGED
|
@@ -23,6 +23,8 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
23
23
|
|
|
24
24
|
getFileDownloadUrl: ((path: string) => Promise<string>);
|
|
25
25
|
|
|
26
|
+
getFileUploadUrl: ( originalFilename, contentType, size, originalExtension, recordPk ) => Promise<{ uploadUrl: string, tagline?: string, filePath?: string, uploadExtraParams?: Record<string, string>, previewUrl?: string, error?: string } | {error: string}>;
|
|
27
|
+
|
|
26
28
|
constructor(options: PluginOptions) {
|
|
27
29
|
super(options, import.meta.url);
|
|
28
30
|
this.options = options;
|
|
@@ -36,6 +38,46 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
36
38
|
}
|
|
37
39
|
return this.options.storageAdapter.getDownloadUrl(path, expiresInSeconds);
|
|
38
40
|
}
|
|
41
|
+
|
|
42
|
+
this.getFileUploadUrl = async ( originalFilename, contentType, size, originalExtension, recordPk ) : Promise<{ uploadUrl: string, tagline?: string, filePath?: string, uploadExtraParams?: Record<string, string>, previewUrl?: string, error?: string } | {error: string}> => {
|
|
43
|
+
|
|
44
|
+
if (this.options.allowedFileExtensions && !this.options.allowedFileExtensions.includes(originalExtension.toLowerCase())) {
|
|
45
|
+
return {
|
|
46
|
+
error: `File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let record = undefined;
|
|
51
|
+
if (recordPk) {
|
|
52
|
+
// get record by recordPk
|
|
53
|
+
const pkName = this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name;
|
|
54
|
+
record = await this.adminforth.resource(this.resourceConfig.resourceId).get(
|
|
55
|
+
[Filters.EQ(pkName, recordPk)]
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const filePath: string = this.options.filePath({ originalFilename, originalExtension, contentType, record });
|
|
60
|
+
if (filePath.startsWith('/')) {
|
|
61
|
+
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
|
|
62
|
+
}
|
|
63
|
+
const { uploadUrl, uploadExtraParams } = await this.options.storageAdapter.getUploadSignedUrl(filePath, contentType, 1800);
|
|
64
|
+
let previewUrl;
|
|
65
|
+
if (this.options.preview?.previewUrl) {
|
|
66
|
+
previewUrl = this.options.preview.previewUrl({ filePath });
|
|
67
|
+
} else {
|
|
68
|
+
previewUrl = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
|
|
69
|
+
}
|
|
70
|
+
const tagline = `${ADMINFORTH_NOT_YET_USED_TAG}=true`;
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
uploadUrl,
|
|
74
|
+
tagline,
|
|
75
|
+
filePath,
|
|
76
|
+
uploadExtraParams,
|
|
77
|
+
previewUrl,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
39
81
|
if (this.options.generation?.rateLimit?.limit) {
|
|
40
82
|
this.rateLimiter = new RateLimiter(this.options.generation.rateLimit?.limit)
|
|
41
83
|
}
|
|
@@ -369,41 +411,8 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
369
411
|
handler: async ({ body }) => {
|
|
370
412
|
const { originalFilename, contentType, size, originalExtension, recordPk } = body;
|
|
371
413
|
|
|
372
|
-
|
|
373
|
-
return {
|
|
374
|
-
error: `File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
let record = undefined;
|
|
379
|
-
if (recordPk) {
|
|
380
|
-
// get record by recordPk
|
|
381
|
-
const pkName = this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name;
|
|
382
|
-
record = await this.adminforth.resource(this.resourceConfig.resourceId).get(
|
|
383
|
-
[Filters.EQ(pkName, recordPk)]
|
|
384
|
-
)
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
const filePath: string = this.options.filePath({ originalFilename, originalExtension, contentType, record });
|
|
388
|
-
if (filePath.startsWith('/')) {
|
|
389
|
-
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
|
|
390
|
-
}
|
|
391
|
-
const { uploadUrl, uploadExtraParams } = await this.options.storageAdapter.getUploadSignedUrl(filePath, contentType, 1800);
|
|
392
|
-
let previewUrl;
|
|
393
|
-
if (this.options.preview?.previewUrl) {
|
|
394
|
-
previewUrl = this.options.preview.previewUrl({ filePath });
|
|
395
|
-
} else {
|
|
396
|
-
previewUrl = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
|
|
397
|
-
}
|
|
398
|
-
const tagline = `${ADMINFORTH_NOT_YET_USED_TAG}=true`;
|
|
414
|
+
return this.getFileUploadUrl( originalFilename, contentType, size, originalExtension, recordPk );
|
|
399
415
|
|
|
400
|
-
return {
|
|
401
|
-
uploadUrl,
|
|
402
|
-
tagline,
|
|
403
|
-
filePath,
|
|
404
|
-
uploadExtraParams,
|
|
405
|
-
previewUrl,
|
|
406
|
-
};
|
|
407
416
|
}
|
|
408
417
|
});
|
|
409
418
|
|