How to use a custom directory to store files within the AdonisJS 5 API #1567
-
Hello ! public async store({ request }: HttpContextContract) {
const timestamp = new Date().getTime()
const serviceId = request.input('service_id')
const document = request.file('picture', {
extnames: ['jpg', 'jpeg', 'png', 'svg']
})
if (document?.hasErrors) return document.errors
const picture = await Picture.create({
filename: `${timestamp}.${document!.extname}`,
serviceId
})
await document?.move(Application.makePath(`../public/storage/services`), {
name: `${timestamp}.${document.extname}`
})
await Picture.query().where('id', picture.id).update({
filename: document?.fileName,
label: document!.clientName
})
return { picture }
} My uplaod folder is work ! My problem is that if I follow the documentation 100% my files are uploads in the public folder of my application (so that I can retrieve my images on my frontend) but when I have to re-build my application my uploads are deleted because the public folder is purged and then re-created. With my controller above, my uploads are saved in a public folder at the root of my application. The problem is that with this method, I can't get my image path for my nuxtJS front. Can you help me ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You are simply relying on the
|
Beta Was this translation helpful? Give feedback.
You are simply relying on the
/public
folder to serve images for you. As shared earlier in the discord, you should just write a 2 liner route to serve images from a custom directory.../storage/uploads
./uploads/<filename>