|
1 | 1 |
|
2 | | -import { PluginOptions } from './types.js'; |
| 2 | +import { PluginOptions, UploadFromBufferParams } from './types.js'; |
3 | 3 | import { AdminForthPlugin, AdminForthResourceColumn, AdminForthResource, Filters, IAdminForth, IHttpServer, suggestIfTypo, RateLimiter, AdminUser, HttpExtra } from "adminforth"; |
4 | 4 | import { Readable } from "stream"; |
5 | 5 | import { randomUUID } from "crypto"; |
@@ -554,19 +554,17 @@ export default class UploadPlugin extends AdminForthPlugin { |
554 | 554 | } |
555 | 555 |
|
556 | 556 |
|
| 557 | + /* |
| 558 | + * Uploads a file from a buffer, creates a record in the resource, and returns the file path and preview URL. |
| 559 | + */ |
557 | 560 | async uploadFromBuffer({ |
558 | 561 | filename, |
559 | 562 | contentType, |
560 | 563 | buffer, |
561 | 564 | adminUser, |
562 | 565 | extra, |
563 | | - }: { |
564 | | - filename: string; |
565 | | - contentType: string; |
566 | | - buffer: Buffer | Uint8Array | ArrayBuffer; |
567 | | - adminUser: AdminUser; |
568 | | - extra?: HttpExtra; |
569 | | - }): Promise<{ path: string; previewUrl: string }> { |
| 566 | + recordAttributes, |
| 567 | + }: UploadFromBufferParams): Promise<{ path: string; previewUrl: string }> { |
570 | 568 | if (!filename || !contentType || !buffer) { |
571 | 569 | throw new Error('filename, contentType and buffer are required'); |
572 | 570 | } |
@@ -644,22 +642,22 @@ export default class UploadPlugin extends AdminForthPlugin { |
644 | 642 | throw new Error(`Upload failed with status ${resp.status}: ${bodyText}`); |
645 | 643 | } |
646 | 644 |
|
647 | | - await this.options.storageAdapter.markKeyForNotDeletation(filePath); |
| 645 | + await this.markKeyForNotDeletion(filePath); |
648 | 646 |
|
649 | 647 | if (!this.resourceConfig) { |
650 | 648 | throw new Error('resourceConfig is not initialized yet'); |
651 | 649 | } |
652 | 650 |
|
653 | 651 | const { error: createError } = await this.adminforth.createResourceRecord({ |
654 | 652 | resource: this.resourceConfig, |
655 | | - record: { [this.options.pathColumnName]: filePath }, |
| 653 | + record: { ...(recordAttributes ?? {}), [this.options.pathColumnName]: filePath }, |
656 | 654 | adminUser, |
657 | 655 | extra, |
658 | 656 | }); |
659 | 657 |
|
660 | 658 | if (createError) { |
661 | 659 | try { |
662 | | - await this.options.storageAdapter.markKeyForDeletation(filePath); |
| 660 | + await this.markKeyForDeletion(filePath); |
663 | 661 | } catch (e) { |
664 | 662 | // best-effort cleanup, ignore error |
665 | 663 | } |
|
0 commit comments