Skip to content

Commit 58a58b0

Browse files
committed
feat: add uploadFromBuffer method to Upload plugin for backend file uploads
1 parent 8ded8ec commit 58a58b0

File tree

1 file changed

+43
-0
lines changed
  • adminforth/documentation/docs/tutorial/08-Plugins

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,49 @@ new UploadPlugin({
305305
> adminServeBaseUrl defines the public path prefix. If your AdminForth base URL is /admin, files will be accessible under /admin/static/source/<key>.
306306
307307
308+
## API
309+
310+
### uploadFromBuffer
311+
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.
313+
314+
You can code it as custom logic or you can simply reuse Upload plugin for this purpose as well.
315+
316+
This method uploads a file from a Node.js `Buffer`, automatically creates a record in the corresponding resource, and returns both the stored file path and a preview URL.
317+
318+
```ts title="./some-backend-service.ts"
319+
import { admin } from './admin'; // your AdminForth instance
320+
321+
...
322+
plugins: [
323+
new UploadPlugin({
324+
id: 'my_reports_plugin', // unique identifier for your plugin instance
325+
....
326+
})
327+
]
328+
...
329+
330+
const plugin = admin.getPluginById('my_reports_plugin');
331+
332+
const { path, previewUrl } = await plugin.uploadFromBuffer({
333+
filename: 'report.pdf',
334+
contentType: 'application/pdf',
335+
buffer, // Node.js Buffer with file content
336+
adminUser, // current admin user or system user
337+
recordAttributes: {
338+
title: 'Generated report',
339+
listed: false,
340+
},
341+
});
342+
```
343+
344+
- `uploadFromBuffer` uses the configured storage adapter (S3, local, etc.) to store the file.
345+
- 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.
346+
- It returns an object `{ path, previewUrl }`, where `previewUrl` is the same URL that would be used for previews inside AdminForth.
347+
348+
> ⚠️ 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).
349+
350+
308351
309352
## Image generation
310353

0 commit comments

Comments
 (0)