aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext/fileserver.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/ext/fileserver.go')
-rw-r--r--pkg/ext/fileserver.go21
1 files changed, 18 insertions, 3 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)
}
}