https://kotlinlang.org logo
#ktor
Title
# ktor
f

FunkyMuse

05/29/2022, 3:17 PM
Hey guys, I have avatar folder, for each user i create a folder with his unique username /users/{username} ^ external folder Inside that folder I have avatar.jpg When I access the image via serverUrl/users/admin/avatar.jpg It says that the image cannot be displayed. How do I set this up for each user?
b

Big Chungus

05/29/2022, 3:33 PM
It's not ktor specific question, but rather js/frontend issue
Since ktor does not display anything, being a server and all...
f

FunkyMuse

05/29/2022, 4:13 PM
Ktor doesn't display anything but I need to make that image accessible to anyone with an URL to it, in spring boot there's resource handlers for external folders, so far I've seen static content which doesn't help my use case So yea the setup has to happen on client side which I've no idea how and the image is inaccessible to the outside even when the URL is exposed There hasn't been a sample/tutorial about that or i don't know what to search for in Ktor
Static resources can also work for you, just be sure to mount the external folder on your server/container before booting up the app
Alternatively you could just proxy the request into the external server
f

FunkyMuse

05/29/2022, 4:37 PM
I’ve tried but the user folder name is dynamic in this case the username is: test
the only way to make this work is if i hardcore the username folder path which doesn’t solve my case
Copy code
private fun Routing.userAvatars() {
    static("/users/test/") {
        staticRootFolder = File("users/test/")
        files(".")
    }
}
I tried using wildcards but it doesn’t work
Copy code
private fun Routing.userAvatars() {
    static("/users/*") {
        staticRootFolder = File("users/*")
        files(".")
    }
}
b

Big Chungus

05/29/2022, 6:34 PM
No no, you only need to mount your static files root. The uri paths then match file paths relative to the root
So for you it would be static("/")
But you better move users folder to some subdirectory instead. Mounting root is not a good idea
b

Big Chungus

05/29/2022, 6:46 PM
Why? What's the error? What's your setup?
I need more details to be able to help you
f

FunkyMuse

05/29/2022, 6:47 PM
it doesn’t work private fun Routing.userAvatars() { static("/") { } }
something like this should work in your case static("/users") { files("/users”) }
This is saying to mount users folder on users route. Subroutes are then defined by relative file paths
Note that /users in file path means folder users in your system root dir, not your working dir (due to slash)
I suggest you carefully read through the docs again https://ktor.io/docs/serving-static-content.html
You seem to be confused with the dsl and paths
f

FunkyMuse

05/30/2022, 7:22 AM
https://kotlinlang.slack.com/archives/C0A974TJ9/p1653850186463149?thread_ts=1653837478.129369&cid=C0A974TJ9 welp it doesn’t, but if i remove the slash it does thanks for your help I figured it out without using “static” and using a route, but then later on when i opened “static” it’s route in disguise
5 Views