Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller.file only works correctly with html files #294

Open
roboz0r opened this issue Feb 14, 2021 · 0 comments
Open

Controller.file only works correctly with html files #294

roboz0r opened this issue Feb 14, 2021 · 0 comments
Labels

Comments

@roboz0r
Copy link
Contributor

roboz0r commented Feb 14, 2021

The current implementation of Controller.file only works for html files as the called Giraffe code sets the ContentType:
here ctx.SetContentType "text/html; charset=utf-8"

Different typed files e.g. css will not work in the browser with the wrong content type set. I propose to change the implementation of Controller.file to:

let file (ctx: HttpContext) (filePath: string) (contentType: string) =
    task {
        let filePath =
            match Path.IsPathRooted filePath with
            | true -> filePath
            | false ->
                let fileProvider = ctx.GetWebHostEnvironment().WebRootFileProvider
                let fileInfo = fileProvider.GetFileInfo(filePath)
                fileInfo.PhysicalPath

        ctx.Response.ContentType <- contentType

        let! file = File.ReadAllBytesAsync filePath
        return! ctx.WriteBytesAsync file
    }

I added another extension to HttpContext:
ctx.GetWebHostEnvironment(): IWebHostEnvironment = ctx.GetService()

In my testing on Azure Functions WebRootFileProvider was initially NullFileProvider but could be instantiated like this:

        let webHostEnv = req.HttpContext.GetWebHostEnvironment()
        webHostEnv.WebRootPath <- Path.Combine(context.FunctionAppDirectory, "public")
        webHostEnv.WebRootFileProvider <- new PhysicalFileProvider(webHostEnv.WebRootPath)

Propose that an addition is also made to the Azure Function CE to enable:

azureFunction {
    execution_context context
    // ...
}

Which would automatically instantiate the WebRootFileProvider as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants