Skip to content

Commit 48f6dd2

Browse files
committed
fix: add getKeyAsDataURL into storage adapter
1 parent 18d0f44 commit 48f6dd2

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,59 @@ generation: {
358358
}
359359
```
360360
361+
`attachFiles` function can return an array of URLs to images which will be used as input for image generation.
362+
URLs can be absolute HTTP URLs (should be public in this case) or data-URLs (`data:image/png;base64,<base64 content of image>`).
363+
364+
For example you can use `getKeyAsDataURL` function from any storage adapter to get image as data-URL:
365+
366+
```ts title="./apartments.ts"
367+
368+
import { StorageAdapter } from 'adminforth'
369+
370+
let sourceAdapter: StorageAdapter = null;
371+
372+
columns: [
373+
...
374+
{
375+
name: 'source_image',
376+
},
377+
{
378+
name: 'destination_image',
379+
}
380+
],
381+
plugins: [
382+
...
383+
new UploadPlugin({
384+
pathColumnName: 'apartment_source',
385+
storageAdapter: (sourcesAdapter = new AdminForthStorageAdapterLocalFilesystem({
386+
fileSystemFolder: "./db/uploads",
387+
mode: "public",
388+
signingSecret: process.env.ADMINFORTH_SECRET, // secret used to generate presigned URLs
389+
}), sourcesAdapter),
390+
}),
391+
new UploadPlugin({
392+
pathColumnName: 'apartment_image',
393+
storageAdapter: new AdminForthAdapterLocalFilesystem({
394+
fileSystemFolder: "./db/uploads",
395+
mode: "public",
396+
signingSecret: process.env.ADMINFORTH_SECRET, // secret used to generate presigned URLs
397+
}),
398+
generation: {
399+
attachFiles: ({ record }) => {
400+
// get picture stored in apartment_source column as data-URL
401+
return [sourceAdapter.getKeyAsDataURL(record.apartment_source)];
402+
},
403+
generationPrompt: "Remove text from the image",
404+
countToGenerate: 3,
405+
outputSize: '1024x1024',
406+
}
407+
})
408+
]
409+
```
410+
411+
With thus setup you can upload image to `apartment_source` column, save entity and then generate new image by clicking on `Generate` button in the `apartment_image` to remove any text from the image.
412+
413+
361414
### Rate limits
362415
363416
You can set rate limits for image generation per IP address:

adminforth/servers/express.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class ExpressServer implements IExpressHttpServer {
118118
}
119119
}
120120
this.expressApp.get(`${slashedPrefix}assets/*`, handler);
121+
process.env.HEAVY_DEBUG && console.log('®️ Registering SPA serve handler', `${slashedPrefix}assets/*`);
121122
this.expressApp.get(`${prefix}*`, handler);
122123

123124

adminforth/types/Adapters.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ export interface StorageAdapter {
201201
* If adapter is configured to publically, this method should return true.
202202
*/
203203
objectCanBeAccesedPublicly(): Promise<boolean>;
204+
205+
/**
206+
* This method should return the key as a data URL (base64 encoded string).
207+
* @param key - The key of the file to be converted to a data URL
208+
* @returns A promise that resolves to a string containing the data URL
209+
*/
210+
getKeyAsDataURL(key: string): Promise<string>;
204211
}
205212

206213

dev-demo/resources/apartments.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import AdminForthAdapterS3Storage from "../../adapters/adminforth-storage-adapte
1818
import AdminForthAdapterLocal from "../../adapters/adminforth-storage-adapter-local/index.js";
1919
import AdminForthStorageAdapterLocalFilesystem from "../../adapters/adminforth-storage-adapter-local/index.js";
2020
import AdminForth from "../../adminforth";
21+
import { StorageAdapter } from "../../adminforth";
2122

2223

2324
const demoChecker = async ({ record, adminUser, resource }) => {
@@ -40,6 +41,8 @@ declare global {
4041
}
4142
}
4243

44+
let sourcesAdapter: StorageAdapter;
45+
4346
export default {
4447
dataSource: "maindb",
4548
table: "apartments",
@@ -319,10 +322,14 @@ export default {
319322
openAiApiKey: process.env.OPENAI_API_KEY as string,
320323
}),
321324

322-
// attachFiles: ({ record, adminUser }: { record: any; adminUser: AdminUser }) => {
323-
// // attach apartment source image to generation, image should be public
324-
// return [`https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${record.apartment_source}`];
325-
// },
325+
attachFiles: async ({ record, adminUser }: { record: any; adminUser: AdminUser }) => {
326+
// attach apartment source image to generation, image should be public
327+
return [
328+
await sourcesAdapter.getKeyAsDataURL(record.apartment_source)
329+
];
330+
331+
// return [`https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${record.apartment_source}`];
332+
},
326333
generationPrompt: "Add a 10 kittyies to the appartment look, it should be foto-realistic, they should be different colors, sitting all around the appartment",
327334
countToGenerate: 1,
328335
outputSize: '1024x1024',
@@ -341,19 +348,21 @@ export default {
341348
new UploadPlugin({
342349
pathColumnName: "apartment_source",
343350

344-
// storageAdapter: new AdminForthAdapterS3Storage({
345-
// region: "eu-central-1",
346-
// bucket: "tmpbucket-adminforth",
347-
// accessKeyId: process.env.AWS_ACCESS_KEY_ID,
348-
// secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
349-
// s3ACL: 'public-read', // ACL which will be set to uploaded file
350-
// }),
351+
storageAdapter: (sourcesAdapter = new AdminForthAdapterS3Storage({
352+
region: "eu-central-1",
353+
bucket: "tmpbucket-adminforth",
354+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
355+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
356+
s3ACL: 'public-read', // ACL which will be set to uploaded file
357+
}), sourcesAdapter),
351358

352-
storageAdapter: new AdminForthStorageAdapterLocalFilesystem({
353-
fileSystemFolder: "./db/uploads",
354-
mode: "public", // public if all files should be accessible from the web, private only if could be accesed by temporary presigned links
355-
signingSecret: process.env.ADMINFORTH_SECRET, // secret used to generate presigned URLs
356-
}),
359+
// storageAdapter: (sourcesAdapter = new AdminForthStorageAdapterLocalFilesystem({
360+
// fileSystemFolder: "./db/uploads", // folder where files will be stored on disk
361+
// adminServeBaseUrl: "static/source", // the adapter not only stores files, but also serves them for HTTP requests
362+
// // this optional path allows to set the base URL for the files. Should be unique for each adapter if set.
363+
// mode: "public", // public if all files should be accessible from the web, private only if could be accesed by temporary presigned links
364+
// signingSecret: process.env.ADMINFORTH_SECRET, // secret used to generate presigned URLs
365+
// }), sourcesAdapter),
357366

358367
allowedFileExtensions: [
359368
"jpg",

0 commit comments

Comments
 (0)