Hello everyone, I've just joined this channel. and...
# server
p
Hello everyone, I've just joined this channel. and I'm developing Rest API server using ktor. I'm curious about that how do I allow to access files from Client side. From my understanding, there is static feature, it can provide static data such as resources, files, and so on. in static directory of this project. is it right? but it seems to be not enough for it. First of all, the client(Android App) sends some image files throgh MultiPart. the receive files are stored in specific directory, for instance (d:\\service\images\) The problem is that I don't know how to access above directory to download or load images in client side. My expected url is "

http://www.domain.com/images/pic.png

" Is it possible to provide for ktor? Anyone have any ideas? Thanks in advance.
b
There's a 'static' route builder dsl tat can mount a given folder on a given route
Looks like it's the one you need
p
@Big Chungus I'm gong try your comment. Thanks for your quick reply :)
b
Let me know if you can't figure it out, can boit up my pc and send you a snippet
p
@Big Chungus It's still not working. :( I think that It would be better to solve problem with actual my code and directory.
/* POST */
Copy code
post("/uploadFile") {
  val dir = File("D:\\carrotmarket_backend\\used_images\\${nickName}\\$id\\")
}
The received files are stored in like d:\\carrotmargeeet\\used_backeend\\csh0303\\10\\item.png • csh0303 : my nickname • 10 : item number • item.png : filename These values are dynamically decided at each time.
/* STATIC */
Copy code
static("/static") {
    staticRootFolder = File("D:\\carrotmarket_backend")
    static("used_images") {
        static("csh0303") {
            files("10")
        }
    }
}
According to ktor.io website, I can change static foldeer by using staticRootFolder. and then to reach sub-folder, I used multi static above code. Although, I tried many times and changed URL in browser. It's still not working. Is there any problem in my understanding and investigation? I hopefully want to know specific URL for this. Thanks.
b
Not quite how it works. Drop the second static builder and you should be fine. Static handler serves all files under rootFolder by the subpath in the request
So a request to /static/dynamic/path will lookup and serve a file D:/carrotmarket_backend/dynamic/path
Also have a look at custom route here to meet your case https://ktor.io/samples/feature/static.html
I think you can pass files(".") To serve root dir
p
@Big Chungus Thanks for your great support. When I put the files(".") in first static brace.
Copy code
static("/static") {
    defaultResource("index.html")
    staticRootFolder = File("D:\\carrotmarket_backend")
        files(".")
}
I could check and see resources(html and png) in client side. Awesome! But I couldn't fully understand why it works properly 😞 Anyway Thanks again.
b
It works becouse it then serves all subpaths from root directory mapped from request subpath
"." Translates to current directory
Which is the root in your case