Skip to content

Commit ac4d145

Browse files
committed
docs: add info preComputeUrl
1 parent a556c69 commit ac4d145

13 files changed

+140
-26
lines changed

docs/.vitepress/config.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default defineConfig({
1414
// https://vitepress.dev/reference/default-theme-config
1515
nav: [
1616
{ text: 'Home', link: '/' },
17-
{ text: 'Guide', link: '/guide/introduction' },
17+
{ text: 'Guide', link: '/guide/essentials/introduction' },
1818
{
1919
text: 'Other docs',
2020
items: [
@@ -36,15 +36,15 @@ export default defineConfig({
3636
items: [
3737
{
3838
text: 'Introduction',
39-
link: '/guide/introduction'
39+
link: '/guide/essentials/introduction'
4040
},
4141
{
4242
text: 'Installation',
43-
link: '/guide/installation'
43+
link: '/guide/essentials/installation'
4444
},
4545
{
4646
text: 'Configuration',
47-
link: '/guide/configuration'
47+
link: '/guide/essentials/configuration'
4848
}
4949
]
5050
},
@@ -53,19 +53,19 @@ export default defineConfig({
5353
items: [
5454
{
5555
text: 'Migration Setup',
56-
link: '/guide/migration-setup'
56+
link: '/guide/basic_usage/migration-setup'
5757
},
5858
{
5959
text: 'Model Setup',
60-
link: '/guide/model-setup'
60+
link: '/guide/basic_usage/model-setup'
6161
},
6262
{
6363
text: 'Controller Setup',
64-
link: '/guide/controller-setup'
64+
link: '/guide/basic_usage/controller-setup'
6565
},
6666
{
6767
text: 'View Setup',
68-
link: '/guide/view-setup'
68+
link: '/guide/basic_usage/view-setup'
6969
}
7070
]
7171
},
@@ -85,9 +85,13 @@ export default defineConfig({
8585
{
8686
text: 'Advanced Usage',
8787
items: [
88+
{
89+
text: 'PrecompileUrl',
90+
link: '/guide/advanced_usage/pre-compile-on-demand'
91+
},
8892
{
8993
text: 'Custom converter',
90-
link: '/guide/custom-converter'
94+
link: '/guide/advanced_usage/custom-converter'
9195
},
9296
]
9397
}

docs/changelog.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Changelog
22

3+
## 2.2.0
4+
5+
* add config preComputeUrl
6+
* fix data serialize
7+
38
## 2.1.0
49

510
* you may set the ffmpeg and ffprobe binary paths manually
6-
* add the ability to disable meta
7-
* add the ability to disable rename
11+
* add config to disable meta
12+
* add config to disable rename
813

914
## 2.0.2
1015

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Pre compute on demand
2+
3+
We recommend not enabling the preComputeUrl option when you need the URL for just one or two queries and not within the rest of your application.
4+
5+
For those couple of queries, you can manually compute the URLs within the controller. Here's a small helper method that you can drop on the model directly.
6+
7+
```ts
8+
import type { Attachment } from '@jrmc/adonis-attachment/types/attachment'
9+
import { attachment, Attachmentable, attachmentManager } from '@jrmc/adonis-attachment'
10+
11+
class User extends compose(BaseModel, Attachmentable) {
12+
static async preComputeUrls(models: User | User[]) {
13+
if (Array.isArray(models)) {
14+
await Promise.all(models.map((model) => this.preComputeUrls(model)))
15+
return
16+
}
17+
18+
// compute url for original file
19+
await attachmentManager.computeUrl(models.avatar)
20+
21+
// compute url for thumbnail variant
22+
const thumb = models.avatar.getVariant('thumbnail')
23+
await attachmentManager.computeUrl(thumb)
24+
25+
// compute url for medium variant with expiration time option
26+
const medium = models.avatar.getVariant('medium')
27+
await attachmentManager.computeUrl(medium, {
28+
expiresIn: '30 mins',
29+
})
30+
}
31+
32+
@attachment({
33+
variants: ['thumbnail', 'medium', 'large']
34+
})
35+
declare avatar: Attachment
36+
}
37+
```
38+
39+
computeUrl method create automatically creates a signed or unsigned url depending on Drive's configuration.
40+
41+
it's possible to pass specific options to the signed url.
42+
options params accepts `expiresIn`, `contentType` et `contentDisposition`.
43+
44+
[More informations](https://flydrive.dev/docs/disk_api#getsignedurl)
45+
46+
---
47+
48+
And now use it as follows.
49+
50+
```ts
51+
const users = await User.all()
52+
await User.preComputeUrls(users)
53+
54+
return users
55+
```
56+
57+
Or for a single user
58+
59+
```ts
60+
const user = await User.findOrFail(1)
61+
await User.preComputeUrls(user)
62+
63+
return user
64+
```
File renamed without changes.
File renamed without changes.

docs/guide/model-setup.md docs/guide/basic_usage/model-setup.md

+12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ class User extends BaseModel {
5151
}
5252
```
5353

54+
## Specifying preComputeUrl
55+
56+
You can enabled pre compute the URLs after SELECT queries, default is false
57+
58+
```ts
59+
class User extends BaseModel {
60+
@attachment({ preComputeUrl: true }) // [!code highlight]
61+
declare avatar: Attachment
62+
}
63+
```
64+
65+
5466
## Specifying meta
5567

5668
You can disabled meta generation, default is true

docs/guide/view-setup.md docs/guide/basic_usage/view-setup.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,28 @@ await user.avatar.getSignedUrl('thumbnail')
3030

3131
getSignedUrl options params accepts `expiresIn`, `contentType` et `contentDisposition`. [More informations](https://flydrive.dev/docs/disk_api#getsignedurl)
3232

33+
### If preComputeUrl is enabled
3334

34-
## By serialize
35+
```edge
36+
<img src="{{ user.avatar.url }}" loading="lazy" alt="" />
37+
<img src="{{ user.avatar.getVariant('thumbnail').url }}" loading="lazy" alt="" />
38+
```
3539

36-
```ts
37-
await user.avatar.toJSON()
40+
41+
## URLs for Inertia template
42+
43+
::: code-group
44+
```js
45+
<img src={user.avatar.thumbnail.url} loading="lazy" alt="" />
3846
```
3947

4048
```vue
4149
<img :src="user.avatar.thumbnail.url" loading="lazy" alt="" />
42-
<img :src="user.avatar.thumbnail.signedUrl" loading="lazy" alt="" />
4350
```
4451

52+
```svelte
53+
<img src={user.avatar.thumbnail.url} loading="lazy" alt="" />
54+
```
55+
:::
56+
57+
preComputeUrl is required.

docs/guide/configuration.md docs/guide/essentials/configuration.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default defineConfig({
1818
})
1919
```
2020

21-
### converters
21+
## converters
2222

2323
|OPTIONS: | DESCRIPTIONS: |
2424
| -------- | ------------------------ |
@@ -28,13 +28,26 @@ export default defineConfig({
2828

2929
---
3030

31-
### meta (optional, default true)
31+
## preComputeUrl (optional, default false)
32+
33+
enable the preComputeUrl flag to pre compute the URLs after SELECT queries.
34+
35+
```typescript
36+
export default defineConfig({
37+
preComputeUrl: true, // [!code focus]
38+
converters: [
39+
// ...
40+
]
41+
})
42+
```
43+
44+
## meta (optional, default true)
3245

3346
you can set the default meta generation or not
3447

3548
```typescript
3649
export default defineConfig({
37-
meta: false, // [!code focus],
50+
meta: false, // [!code focus]
3851
converters: [
3952
// ...
4053
]
@@ -43,13 +56,13 @@ export default defineConfig({
4356

4457
---
4558

46-
### rename (optional, default true)
59+
## rename (optional, default true)
4760

4861
You can define by default if the files are renamed or not.
4962

5063
```typescript
5164
export default defineConfig({
52-
rename: false, // [!code focus],
65+
rename: false, // [!code focus]
5366
converters: [
5467
// ...
5568
]
@@ -58,7 +71,7 @@ export default defineConfig({
5871

5972
---
6073

61-
### bin (optional)
74+
## bin (optional)
6275

6376
You may set the ffmpeg and ffprobe binary paths manually:
6477

File renamed without changes.

docs/guide/introduction.md docs/guide/essentials/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ The `adonis-attachment` package was designed to simplify file upload management.
44

55
The creation of variants is handled through Converters.
66

7-
<!--@include: ./partials/table-converter.md-->
7+
<!--@include: ../partials/table-converter.md-->
88

99
Project sample : [adonis-starter-kit](https://github.com/batosai/adonis-starter-kit)

docs/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ hero:
99
actions:
1010
- theme: brand
1111
text: Getting started
12-
link: /guide/introduction
12+
link: /guide/essentials/introduction
1313
- theme: alt
1414
text: View on GitHub
1515
link: https://github.com/batosai/adonis-attachment
@@ -19,7 +19,7 @@ features:
1919
details: Simplifying upload management.
2020
- title: Convert
2121
details: Optimise, convert, crop image, thumbnail video etc.
22-
- title: Custom
23-
details: Go further and create your own converter.
22+
- title: Edge & inertia compliant
23+
details: Go to attachment in your favorite frontend
2424
---
2525

docs/structure-data-json.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Meta data list (if available):
1212
- orientation
1313
- mime type
1414
- gps
15+
- url
1516

1617
Sample struture JSON in database
1718

@@ -42,6 +43,7 @@ Sample struture JSON in database
4243
"mimeType":"image/jpeg",
4344
"path":"uploads/avatars/xjfpa4tuxh66p6s3hrgl0pzn.jpg",
4445
"originalName":"aco_bot.jpg",
46+
"url": "/uploads/avatars/[...]",
4547
"variants":[
4648
{
4749
"key":"thumbnail",
@@ -62,7 +64,8 @@ Sample struture JSON in database
6264
}
6365
},
6466
"mimeType":"image/webp",
65-
"path":"uploads/avatars/variants/xjfpa4tuxh66p6s3hrgl0pzn.jpg/ajtugq7224qp9moqyi216vur.webp"
67+
"path":"uploads/avatars/variants/xjfpa4tuxh66p6s3hrgl0pzn.jpg/ajtugq7224qp9moqyi216vur.webp",
68+
"url": "/uploads/avatars/variants/[...]",
6669
}
6770
]
6871
}

0 commit comments

Comments
 (0)