Skip to content

How can I obtain the absolute url (i.e. that of the server) of a file I've just uploaded please ? #4534

Answered by franckDev21
franckDev21 asked this question in Help
Discussion options

You must be logged in to vote

I've just found the solution 🥳😊
Here it is:

1 - file ".env" :
APP_URL=http://localhost:8000
2 - router.ts

import { sep, normalize } from 'node:path'
import app from '@adonisjs/core/services/app'

const PATH_TRAVERSAL_REGEX = /(?:^|[\\/])\.\.(?:[\\/]|$)/

router.get('/uploads/*', ({ request, response }) => {
  const filePath = request.param('*').join(sep)
  const normalizedPath = normalize(filePath)

  if (PATH_TRAVERSAL_REGEX.test(normalizedPath)) {
    return response.badRequest('Malformed path')
  }

  const absolutePath = app.makePath('uploads', normalizedPath)
  return response.download(absolutePath)
})

3 - store :

async store({ request, response }: HttpContext) {
   const data = req…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by franckDev21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant