This repository has been archived on 2023-05-01. You can view files and clone it, but cannot push or open issues or pull requests.
gus/contrib/fs/file.go

57 lines
1.2 KiB
Go

package fs
import (
"context"
"io/fs"
"mime"
"strings"
"tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/gemini"
)
// FileHandler builds a handler function which serves up a file system.
func FileHandler(fileSystem fs.FS) gus.Handler {
return func(ctx context.Context, req *gus.Request) *gus.Response {
file, err := fileSystem.Open(strings.TrimPrefix(req.Path, "/"))
if isNotFound(err) {
return nil
}
if err != nil {
return gemini.Failure(err)
}
isDir, err := fileIsDir(file)
if err != nil {
return gemini.Failure(err)
}
if isDir {
return nil
}
return gemini.Success(mediaType(req.Path), file)
}
}
func mediaType(filePath string) string {
if strings.HasSuffix(filePath, ".gmi") {
// This may not be present in the listings searched by mime.TypeByExtension,
// so provide a dedicated fast path for it here.
return "text/gemini"
}
slashIdx := strings.LastIndex(filePath, "/")
dotIdx := strings.LastIndex(filePath[slashIdx+1:], ".")
if dotIdx == -1 {
return "application/octet-stream"
}
ext := filePath[slashIdx+dotIdx:]
mtype := mime.TypeByExtension(ext)
if mtype == "" {
return "application/octet-stream"
}
return mtype
}