Closed Obapelumi closed 1 year ago
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 })
Oh great. Thanks!
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) })
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.
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
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 😊