Hi sorry for the basic question but I have this li...
# http4k
n
Hi sorry for the basic question but I have this line of code that doesn't work:
Copy code
FileInputStream(File("$fullOutputPath/$fileName")).use { fis ->
    return Response(Status.OK).with(Body.binary(ContentType.APPLICATION_PDF).toLens() of fis)
}
I just want to serve a PDF file ... I've tried to add fis.readAllBytes() but binary signature wants an inputstream, it seems .. What am I doing wrong? Thanks in advance
d
What are you seeing? I assume you're not getting any output or an exception? This is because you don't need to close the stream (it's done by the server layer when the http4k response is copied to the server response. Tldr: try removing the use block
Also - if you want to download the file then you will need to add a content disposition to tell the browser to download it
n
I added the "use" thinking it was better practice 😅 Removing that it works, thanks a lot as usual Dave ...
👍 2
a
As a suggestion, if all you're doing is serving static files, there's built-in support for filesystem, classpath, or webjar. https://github.com/http4k/http4k/blob/master/src/docs/blog/nanoservices/static_file_server.kt
n
Thanks Andrew I'll definitely have a look at this ..
j
If the paths there come from a client request, you are potentially opening up your server to be downloaded... e.g. filename = ../../../etc/passwd - much validation required...
d
The static file server does have tests against subverting the path like that. If there are more test cases to add then please PR them 🙃 . https://github.com/http4k/http4k/blob/ebdf6d12e41aa07c55bdc172c64ed9476ae5e0bf/http4k-core/src/testFixtures/kotlin/org/http4k/routing/StaticRoutingHttpHandlerTest.kt#L241
j
It was more for the code sample above ☝️
👍 1