@adminforth/upload 2.10.0 → 2.11.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/build.log CHANGED
@@ -11,5 +11,5 @@ custom/preview.vue
11
11
  custom/tsconfig.json
12
12
  custom/uploader.vue
13
13
 
14
- sent 53,885 bytes received 134 bytes 108,038.00 bytes/sec
14
+ sent 53,878 bytes received 134 bytes 108,024.00 bytes/sec
15
15
  total size is 53,396 speedup is 0.99
package/dist/index.js CHANGED
@@ -482,8 +482,11 @@ export default class UploadPlugin extends AdminForthPlugin {
482
482
  }),
483
483
  });
484
484
  }
485
+ /*
486
+ * Uploads a file from a buffer, creates a record in the resource, and returns the file path and preview URL.
487
+ */
485
488
  uploadFromBuffer(_a) {
486
- return __awaiter(this, arguments, void 0, function* ({ filename, contentType, buffer, adminUser, extra, }) {
489
+ return __awaiter(this, arguments, void 0, function* ({ filename, contentType, buffer, adminUser, extra, recordAttributes, }) {
487
490
  var _b;
488
491
  if (!filename || !contentType || !buffer) {
489
492
  throw new Error('filename, contentType and buffer are required');
@@ -547,19 +550,19 @@ export default class UploadPlugin extends AdminForthPlugin {
547
550
  }
548
551
  throw new Error(`Upload failed with status ${resp.status}: ${bodyText}`);
549
552
  }
550
- yield this.options.storageAdapter.markKeyForNotDeletation(filePath);
553
+ yield this.markKeyForNotDeletion(filePath);
551
554
  if (!this.resourceConfig) {
552
555
  throw new Error('resourceConfig is not initialized yet');
553
556
  }
554
557
  const { error: createError } = yield this.adminforth.createResourceRecord({
555
558
  resource: this.resourceConfig,
556
- record: { [this.options.pathColumnName]: filePath },
559
+ record: Object.assign(Object.assign({}, (recordAttributes !== null && recordAttributes !== void 0 ? recordAttributes : {})), { [this.options.pathColumnName]: filePath }),
557
560
  adminUser,
558
561
  extra,
559
562
  });
560
563
  if (createError) {
561
564
  try {
562
- yield this.options.storageAdapter.markKeyForDeletation(filePath);
565
+ yield this.markKeyForDeletion(filePath);
563
566
  }
564
567
  catch (e) {
565
568
  // best-effort cleanup, ignore error
package/index.ts CHANGED
@@ -1,5 +1,5 @@
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,7 +642,7 @@ 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');
@@ -652,14 +650,14 @@ export default class UploadPlugin extends AdminForthPlugin {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/upload",
3
- "version": "2.10.0",
3
+ "version": "2.11.0",
4
4
  "description": "Plugin for uploading files for adminforth",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AdminUser, ImageGenerationAdapter, StorageAdapter } from "adminforth";
1
+ import { AdminUser, ImageGenerationAdapter, StorageAdapter, HttpExtra } from "adminforth";
2
2
 
3
3
  export type PluginOptions = {
4
4
 
@@ -154,4 +154,49 @@ export type PluginOptions = {
154
154
  * For now only S3 adapter is supported.
155
155
  */
156
156
  storageAdapter: StorageAdapter,
157
- }
157
+ }
158
+
159
+ /**
160
+ * Parameters for the UploadPlugin.uploadFromBuffer API.
161
+ * Used to upload a binary file buffer, create a record in the resource,
162
+ * and return the stored path and preview URL.
163
+ */
164
+ export type UploadFromBufferParams = {
165
+ /**
166
+ * Original file name including extension, used to derive storage path
167
+ * and validate the file extension. Example: "photo.png".
168
+ */
169
+ filename: string;
170
+
171
+ /**
172
+ * MIME type of the uploaded file. Will be used as Content-Type
173
+ * when uploading to the storage adapter. Example: "image/png".
174
+ */
175
+ contentType: string;
176
+
177
+ /**
178
+ * Binary contents of the file. Can be a Node.js Buffer, Uint8Array,
179
+ * or ArrayBuffer. The plugin uploads this directly without converting
180
+ * to base64.
181
+ */
182
+ buffer: Buffer | Uint8Array | ArrayBuffer;
183
+
184
+ /**
185
+ * Authenticated admin user on whose behalf the record is created.
186
+ * Passed through to AdminForth createResourceRecord hooks.
187
+ */
188
+ adminUser: AdminUser;
189
+
190
+ /**
191
+ * Optional HTTP context (headers, IP, etc.) forwarded to AdminForth
192
+ * createResourceRecord hooks. Needed to execute hooks on creation resource record for the upload.
193
+ */
194
+ extra?: HttpExtra;
195
+
196
+ /**
197
+ * Optional additional attributes to set on the created record
198
+ * together with the path column managed by the plugin.
199
+ * Values here do NOT affect the generated storage path.
200
+ */
201
+ recordAttributes?: Record<string, any>;
202
+ };