Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,26 @@ customization: {
]
}

```
```

## Avatars

If you want your user to have custom avatar you can use avatarUrl:

```ts title='./index.ts'

auth: {

...

avatarUrl: async (adminUser)=> {
return `https://${process.env.STORAGE_PROVIDER_PATH}/${adminUser.dbUser.avatar_path}`
},

...

}

```

This syntax can be use to get unique avatar for each user of hardcode avatar, but it makes more sense to use it with [upload plugin](https://adminforth.dev/docs/tutorial/Plugins/upload/#using-plugin-for-uploading-avatar)
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export default {
"exe",
"webp",
],
maxFileSize: 1024 * 1024 * 20, // 5MB
maxFileSize: 1024 * 1024 * 20, // 20MB


filePath: ({ originalFilename, originalExtension, contentType }) =>
Expand Down
153 changes: 152 additions & 1 deletion adminforth/documentation/docs/tutorial/07-Plugins/05-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,155 @@ You can set the maximum width for the preview image in the `./resources/apartmen
...

});
```
```

## Using plugin for uploading avatar

Let's say, that you want to use upload plugin for uploading avatar for each user.
To do this add avatar column to the user resource:


```ts title="./schema.prisma"
model adminuser {
id String @id
email String @unique
password_hash String
role String
created_at DateTime
//diff-add
avatar ?String
}
```

Then make migration:

```bash
npm run makemigration -- --name add-user-avatar-field ; npm run migrate:local
```

Add this column to the users resource:

```ts title="./resources/adminuser.ts"

...

columns: [

...

//diff-add
{
//diff-add
name: "avatar",
//diff-add
type: AdminForthDataTypes.STRING,
//diff-add
showIn: ["show", "edit", "create" ],
//diff-add
},

...

]

...

plugins: [

...

//diff-add
new UploadPlugin({
//diff-add
pathColumnName: "avatar",
//diff-add
storageAdapter: new AdminForthAdapterS3Storage({
//diff-add
bucket: process.env.AWS_BUCKET_NAME,
//diff-add
region: process.env.AWS_REGION,
//diff-add
accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
//diff-add
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
//diff-add
}),
//diff-add
allowedFileExtensions: [
//diff-add
"jpg",
//diff-add
"jpeg",
//diff-add
"png",
//diff-add
"gif",
//diff-add
"webm",
//diff-add
"exe",
//diff-add
"webp",
//diff-add
],
//diff-add
maxFileSize: 1024 * 1024 * 20, // 20MB
//diff-add
filePath: ({ originalFilename, originalExtension, contentType, record }) => {
//diff-add
return `aparts/${new Date().getFullYear()}/${originalFilename}.${originalExtension}`
//diff-add
},
//diff-add
preview: {
//diff-add
maxWidth: "200px",
//diff-add
},
//diff-add
}),

...

]

```

And finally add this callback:

```ts title="./index.ts"

auth: {

...
//diff-add
avatarUrl: async (adminUser)=>{
//diff-add
const plugin = admin.getPluginsByClassName('UploadPlugin').find(p => p.pluginOptions.pathColumnName === 'avatar') as any;
//diff-add
if (!plugin) {
//diff-add
throw new Error('Upload plugin for avatar not found');
//diff-add
}
//diff-add
if (adminUser.dbUser.avatar === null || adminUser.dbUser.avatar === undefined || adminUser.dbUser.avatar === '') {
//diff-add
return '';
//diff-add
}
//diff-add
const imageUrl = await plugin.getFileDownloadUrl(adminUser.dbUser.avatar || '', 3600);
//diff-add
return imageUrl;
//diff-add
},


...

}

```

And now you can easily update avatar for each user
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default {
"exe",
"webp",
],
maxFileSize: 1024 * 1024 * 20, // 5MB
maxFileSize: 1024 * 1024 * 20, // 20MB


filePath: ({ originalFilename, originalExtension, contentType }) =>
Expand Down