Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attachementLite.setOptions should override @attachment options #18

Closed
Obapelumi opened this issue Jun 20, 2022 · 4 comments
Closed

attachementLite.setOptions should override @attachment options #18

Obapelumi opened this issue Jun 20, 2022 · 4 comments
Assignees
Labels
Status: Abandoned Dropped and not into consideration Type: Question Needs clarification

Comments

@Obapelumi
Copy link

Obapelumi commented Jun 20, 2022

Why this feature is required (specific use-cases will be appreciated)?

Sometimes you want to set disk/folder options for an attachment on the fly. For example, here I'm setting the folder option based on key, refId & entity

import { Attachment } from '@ioc:Adonis/Addons/AttachmentLite'
import { AuthContract } from '@ioc:Adonis/Addons/Auth'
import { TransactionClientContract } from '@ioc:Adonis/Lucid/Database'
import Entity from 'App/Models/System/Entity'
import Media from 'App/Models/System/Media'
import StoreMediaValidator from 'App/Validators/System/StoreMediaValidator'
import Env from '@ioc:Adonis/Core/Env'

export default class StoreMedia {
  public static async handle(
    { file, key, refId, entity }: StoreMediaValidator['schema']['props'],
    auth: AuthContract,
    client?: TransactionClientContract
  ) {
    const folder = `${Env.get('NODE_ENV')}/${entity}/${refId}/${key}`
    return await Media.create(
      {
        file: Attachment.fromFile(file).setOptions({ folder }),
        key,
        refId,
        entity: Entity[entity],
        createdBy: auth.user?.id
      },
      { client }
    )
  }
}

In this case the folder defaults to the one specified on the @attachment decorator in my Media model.

Have you tried any other work arounds?

Yes, I tried Attachment.fromFile(file).setOptions({ folder }).save() which uploads to the correct folder but I could not retrieve the correct path in order to persist it in the DB.

Are you willing to work on it with little guidance?

Yes, of course. Happy to take this on 😊

@thetutlage
Copy link
Member

You can get it to work by defining the folder path as part of the name of the attachment.

import { cuid } from '@ioc:Adonis/Core/Helpers'

const folder = `${Env.get('NODE_ENV')}/${entity}/${refId}/${key}`
const name = `${folder}/${cuid()}.${file.extname}`

Attachment.fromFile(file, { name })

@thetutlage thetutlage self-assigned this Jun 20, 2022
@thetutlage thetutlage added the Type: Question Needs clarification label Jun 20, 2022
@Obapelumi
Copy link
Author

Oh great. Thanks!

@Obapelumi
Copy link
Author

You can get it to work by defining the folder path as part of the name of the attachment.

Tried this but Attachment.fromFile seems to only take one argument so it did not work.

Eventually got to work by doing:

const attachment = Attachment.fromFile(file).setOptions({ folder })
    const attachment = Attachment.fromFile(file).setOptions({ folder })
    attachment.save()
    await Media.query()
      .where({ id: media.id })
      .update({ file: JSON.stringify(attachment) })

@stale
Copy link

stale bot commented Sep 20, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Abandoned Dropped and not into consideration label Sep 20, 2022
@stale stale bot closed this as completed Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Abandoned Dropped and not into consideration Type: Question Needs clarification
Projects
None yet
Development

No branches or pull requests

2 participants