Skip to content

Commit a556c69

Browse files
committed
feat: computeUrl method in Manager
1 parent d784c37 commit a556c69

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

src/attachment_manager.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import type { LoggerService } from '@adonisjs/core/types'
9-
import type { DriveService } from '@adonisjs/drive/types'
9+
import type { DriveService, SignedURLOptions } from '@adonisjs/drive/types'
1010
import type { MultipartFile } from '@adonisjs/core/bodyparser'
1111
import type { AttachmentBase, Attachment as AttachmentType } from './types/attachment.js'
1212
import type { ResolvedAttachmentConfig } from './types/config.js'
@@ -83,28 +83,28 @@ export class AttachmentManager {
8383
}
8484
}
8585

86-
async computeUrl(attachment: AttachmentType) {
87-
if (attachment.options?.preComputeUrl === false) {
88-
return
89-
}
90-
86+
async computeUrl(attachment: AttachmentType | AttachmentBase, signedUrlOptions?: SignedURLOptions) {
9187
const disk = attachment.getDisk()
9288
const fileVisibility = await disk.getVisibility(attachment.path!)
9389

9490
if (fileVisibility === 'private') {
95-
attachment.url = await attachment.getSignedUrl()
91+
attachment.url = await attachment.getSignedUrl(signedUrlOptions)
9692
} else {
9793
attachment.url = await attachment.getUrl()
9894
}
95+
}
96+
97+
async preComputeUrl(attachment: AttachmentType) {
98+
if (attachment.options?.preComputeUrl === false) {
99+
return
100+
}
99101

100-
if (attachment.variants) {
102+
await this.computeUrl(attachment)
103+
104+
if (attachment instanceof Attachment && attachment.variants) {
101105
for (const key in attachment.variants) {
102106
if (Object.prototype.hasOwnProperty.call(attachment.variants, key)) {
103-
if (fileVisibility === 'private') {
104-
attachment.variants[key].url = await attachment.getSignedUrl(attachment.variants[key].key)
105-
} else {
106-
attachment.variants[key].url = await attachment.getUrl(attachment.variants[key].key)
107-
}
107+
await this.computeUrl(attachment.variants[key])
108108
}
109109
}
110110
}

src/attachments/attachment.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ export class Attachment extends AttachmentBase implements AttachmentInterface {
107107
this.path = path.join(this.folder, this.name)
108108
}
109109

110+
if (this.variants) {
111+
this.variants.forEach((v) => {
112+
v.setOptions({
113+
...this.options,
114+
variants: []
115+
})
116+
})
117+
}
118+
110119
return this
111120
}
112121

@@ -130,16 +139,28 @@ export class Attachment extends AttachmentBase implements AttachmentInterface {
130139
meta: this.meta,
131140
}
132141

142+
if (this.variants) {
143+
this.variants!.map(async (v) => {
144+
data[v.key] = {
145+
name: v.name,
146+
extname: v.extname,
147+
mimetype: v.mimeType,
148+
meta: v.meta,
149+
size: v.size,
150+
}
151+
})
152+
}
153+
133154
if (this.url) {
134155
data.url = this.url
156+
}
135157

136-
if (this.variants) {
137-
this.variants!.map(async (v) => {
138-
data[v.key] = {
139-
url: v.url,
140-
}
141-
})
142-
}
158+
if (this.variants) {
159+
this.variants!.map(async (v) => {
160+
if (v.url) {
161+
data[v.key].url = v.url
162+
}
163+
})
143164
}
144165

145166
return data

src/mixins/attachmentable.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { NormalizeConstructor } from '@adonisjs/core/types/helpers'
1010
import type { AttributeOfModelWithAttachment } from '../types/mixin.js'
1111

1212
import { beforeSave, afterSave, beforeDelete, afterFind, afterFetch, afterPaginate } from '@adonisjs/lucid/orm'
13-
import { persistAttachment, commit, rollback, generateVariants, computeUrl } from '../utils/actions.js'
13+
import { persistAttachment, commit, rollback, generateVariants, preComputeUrl } from '../utils/actions.js'
1414
import { clone, getAttachmentAttributeNames } from '../utils/helpers.js'
1515
import { defaultStateAttributeMixin } from '../utils/default_values.js'
1616

@@ -26,7 +26,7 @@ export const Attachmentable = <Model extends NormalizeConstructor<typeof BaseMod
2626

2727
await Promise.all(
2828
attachmentAttributeNames.map((attributeName) => {
29-
return computeUrl(modelInstance, attributeName)
29+
return preComputeUrl(modelInstance, attributeName)
3030
})
3131
)
3232
}

src/utils/actions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ export async function persistAttachment(modelInstance: ModelWithAttachment, attr
8484
}
8585
}
8686

87-
export async function computeUrl(modelInstance: ModelWithAttachment, attributeName: string) {
87+
export async function preComputeUrl(modelInstance: ModelWithAttachment, attributeName: string) {
8888
const attachment = modelInstance.$attributes[attributeName] as Attachment
8989
const options = getOptions(modelInstance, attributeName)
9090

9191
attachment.setOptions(options)
9292

93-
return attachmentManager.computeUrl(attachment)
93+
return attachmentManager.preComputeUrl(attachment)
9494
}
9595

9696
/**

0 commit comments

Comments
 (0)