diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-25 18:36:33 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-25 18:36:33 +0200 |
commit | 438aa0e19b628923482dbc3fadcbb2e1ecfd5d7a (patch) | |
tree | 2a5e716b381373d6348f387e68db3eb9d6c3161c | |
parent | ead855b7991b14554eb098616cfac29d91e796eb (diff) | |
download | lens-438aa0e19b628923482dbc3fadcbb2e1ecfd5d7a.tar.gz lens-438aa0e19b628923482dbc3fadcbb2e1ecfd5d7a.tar.bz2 lens-438aa0e19b628923482dbc3fadcbb2e1ecfd5d7a.zip |
fix: Actually read from embedded
Previous implementation was not reading from embedded fs.
-rw-r--r-- | pkg/ext/fileserver.go | 21 | ||||
-rw-r--r-- | templates/layout.html | 2 |
2 files changed, 19 insertions, 4 deletions
diff --git a/pkg/ext/fileserver.go b/pkg/ext/fileserver.go index fdea08e..ee4a80b 100644 --- a/pkg/ext/fileserver.go +++ b/pkg/ext/fileserver.go @@ -2,6 +2,8 @@ package ext import ( "io/fs" + "mime" + "path/filepath" "github.com/valyala/fasthttp" ) @@ -10,9 +12,22 @@ type FileSystem interface { Open(name string) (fs.File, error) } +// This is a VERY simple file server. It does not take a lot into consideration +// and it should only be used to return small predictable files, like in the +// static folder. func FileServer(rootFS FileSystem, rootPath string) fasthttp.RequestHandler { - return func(r *fasthttp.RequestCtx) { - path := r.UserValue("filepath").(string) - r.SendFile(rootPath + path) + return func(ctx *fasthttp.RequestCtx) { + path := ctx.UserValue("filepath").(string) + + f, err := rootFS.Open(rootPath + path) + if err != nil { + InternalServerError(ctx, err) + return + } + defer f.Close() + + m := mime.TypeByExtension(filepath.Ext(path)) + ctx.SetContentType(m) + ctx.SetBodyStream(f, -1) } } diff --git a/templates/layout.html b/templates/layout.html index e21ec5e..bbf1558 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -4,7 +4,7 @@ <meta charset="utf-8"> <title>img | {{block "title" .}} noop {{end}}</title> <link rel="stylesheet" href="/static/main.css"> - <link rel="icon" href="static/square.svg" sizes="any" type="image/svg+xml"> + <link rel="icon" href="/static/square.svg" sizes="any" type="image/svg+xml"> <meta name="viewport" content="width=device-width, initial-scale=1" /> </head> <body> |