Skip to content

Commit

Permalink
feat: computeUrl method in Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
batosai committed Aug 27, 2024
1 parent d784c37 commit a556c69
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
26 changes: 13 additions & 13 deletions src/attachment_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type { LoggerService } from '@adonisjs/core/types'
import type { DriveService } from '@adonisjs/drive/types'
import type { DriveService, SignedURLOptions } from '@adonisjs/drive/types'
import type { MultipartFile } from '@adonisjs/core/bodyparser'
import type { AttachmentBase, Attachment as AttachmentType } from './types/attachment.js'
import type { ResolvedAttachmentConfig } from './types/config.js'
Expand Down Expand Up @@ -83,28 +83,28 @@ export class AttachmentManager {
}
}

async computeUrl(attachment: AttachmentType) {
if (attachment.options?.preComputeUrl === false) {
return
}

async computeUrl(attachment: AttachmentType | AttachmentBase, signedUrlOptions?: SignedURLOptions) {
const disk = attachment.getDisk()
const fileVisibility = await disk.getVisibility(attachment.path!)

if (fileVisibility === 'private') {
attachment.url = await attachment.getSignedUrl()
attachment.url = await attachment.getSignedUrl(signedUrlOptions)
} else {
attachment.url = await attachment.getUrl()
}
}

async preComputeUrl(attachment: AttachmentType) {
if (attachment.options?.preComputeUrl === false) {
return
}

if (attachment.variants) {
await this.computeUrl(attachment)

if (attachment instanceof Attachment && attachment.variants) {
for (const key in attachment.variants) {
if (Object.prototype.hasOwnProperty.call(attachment.variants, key)) {
if (fileVisibility === 'private') {
attachment.variants[key].url = await attachment.getSignedUrl(attachment.variants[key].key)
} else {
attachment.variants[key].url = await attachment.getUrl(attachment.variants[key].key)
}
await this.computeUrl(attachment.variants[key])
}
}
}
Expand Down
35 changes: 28 additions & 7 deletions src/attachments/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export class Attachment extends AttachmentBase implements AttachmentInterface {
this.path = path.join(this.folder, this.name)
}

if (this.variants) {
this.variants.forEach((v) => {
v.setOptions({
...this.options,
variants: []
})
})
}

return this
}

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

if (this.variants) {
this.variants!.map(async (v) => {
data[v.key] = {
name: v.name,
extname: v.extname,
mimetype: v.mimeType,
meta: v.meta,
size: v.size,
}
})
}

if (this.url) {
data.url = this.url
}

if (this.variants) {
this.variants!.map(async (v) => {
data[v.key] = {
url: v.url,
}
})
}
if (this.variants) {
this.variants!.map(async (v) => {
if (v.url) {
data[v.key].url = v.url
}
})
}

return data
Expand Down
4 changes: 2 additions & 2 deletions src/mixins/attachmentable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { NormalizeConstructor } from '@adonisjs/core/types/helpers'
import type { AttributeOfModelWithAttachment } from '../types/mixin.js'

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

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

await Promise.all(
attachmentAttributeNames.map((attributeName) => {
return computeUrl(modelInstance, attributeName)
return preComputeUrl(modelInstance, attributeName)
})
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ export async function persistAttachment(modelInstance: ModelWithAttachment, attr
}
}

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

attachment.setOptions(options)

return attachmentManager.computeUrl(attachment)
return attachmentManager.preComputeUrl(attachment)
}

/**
Expand Down

0 comments on commit a556c69

Please sign in to comment.