Skip to content

Commit fbbfc5e

Browse files
committed
feat: rename uploadFromBuffer method and add uploadFromBufferToExistingRecord method
1 parent ec74795 commit fbbfc5e

File tree

1 file changed

+28
-4
lines changed
  • adminforth/documentation/docs/tutorial/08-Plugins

1 file changed

+28
-4
lines changed

adminforth/documentation/docs/tutorial/08-Plugins/05-upload.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ new UploadPlugin({
307307
308308
## API
309309
310-
### uploadFromBuffer
310+
### uploadFromBufferToNewRecord
311311
312-
In some cases you may want to upload a file directly from your backend (for example, a file generated by a background job or received from a webhook) without going through the browser. For this, the Upload plugin exposes the `uploadFromBuffer` method.
312+
In some cases you may want to upload a file directly from your backend (for example, a file generated by a background job or received from a webhook) without going through the browser. For this, the Upload plugin exposes the `uploadFromBufferToNewRecord` method.
313313
314314
You can code it as custom logic or you can simply reuse Upload plugin for this purpose as well.
315315
@@ -329,7 +329,7 @@ plugins: [
329329

330330
const plugin = admin.getPluginById('my_reports_plugin');
331331

332-
const { path, previewUrl } = await plugin.uploadFromBuffer({
332+
const { path, previewUrl } = await plugin.uploadFromBufferToNewRecord({
333333
filename: 'report.pdf',
334334
contentType: 'application/pdf',
335335
buffer, // Node.js Buffer with file content
@@ -341,13 +341,37 @@ const { path, previewUrl } = await plugin.uploadFromBuffer({
341341
});
342342
```
343343
344-
- `uploadFromBuffer` uses the configured storage adapter (S3, local, etc.) to store the file.
344+
- `uploadFromBufferToNewRecord` uses the configured storage adapter (S3, local, etc.) to store the file.
345345
- It automatically creates a new record in the resource and stores the file path into the column defined by `pathColumnName`, together with any extra `recordAttributes` you pass.
346346
- It returns an object `{ path, previewUrl }`, where `previewUrl` is the same URL that would be used for previews inside AdminForth.
347347
348348
> ⚠️ It is not recommended to upload large files from the backend using `uploadFromBuffer`, because the entire file must go through your server memory and network. For large uploads you should prefer frontend presigned uploads directly to storage. You can find an example of presigned upload flow using upload plugin in the Rich editor plugin source code (Rich editor actually uses Upload plugin to upload images in edited content).
349349
350350
351+
### uploadFromBufferToExistingRecord
352+
353+
If you already have a record and just want to replace the file referenced in its `pathColumnName` field, you can use the `uploadFromBufferToExistingRecord` method. It uploads a file from a Node.js `Buffer`, updates the existing record, and returns the new file path and preview URL.
354+
355+
```ts title="./some-backend-service.ts"
356+
const plugin = admin.getPluginById('my_reports_plugin');
357+
358+
const { path, previewUrl } = await plugin.uploadFromBufferToExistingRecord({
359+
recordId: existingRecordId, // primary key of the record to update
360+
filename: 'report.pdf',
361+
contentType: 'application/pdf',
362+
buffer, // Node.js Buffer with file content
363+
adminUser, // current admin user or system user
364+
extra: {}, // optional extra meta for your hooks / audit
365+
});
366+
```
367+
368+
- Uses the same storage adapter and validation rules as `uploadFromBufferToNewRecord` (file extension whitelist, `maxFileSize`, `filePath` callback, etc.).
369+
- Does not create a new record – it only updates the existing one identified by `recordId`, replacing the value in `pathColumnName` with the new storage path.
370+
- If the generated `filePath` would be the same as the current value in the record, it throws an error to help you avoid CDN/browser caching issues. To force a refresh, make sure your `filePath` callback produces a different key (for example, include a timestamp or random UUID).
371+
372+
> ⚠️ The same recommendation about large files applies here: avoid using `uploadFromBufferToExistingRecord` for very large uploads; prefer a presigned upload flow from the frontend instead.
373+
374+
351375
352376
## Image generation
353377

0 commit comments

Comments
 (0)