1
0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-12-04 17:58:14 +01:00
go-gemini/fs.go

106 lines
2.3 KiB
Go
Raw Normal View History

2020-10-24 21:15:32 +02:00
package gemini
2020-10-12 00:57:04 +02:00
import (
"io"
2020-10-12 02:13:53 +02:00
"mime"
2020-10-12 00:57:04 +02:00
"os"
"path"
)
2020-10-12 02:13:53 +02:00
func init() {
// Add Gemini mime types
mime.AddExtensionType(".gmi", "text/gemini")
mime.AddExtensionType(".gemini", "text/gemini")
}
2020-10-24 21:29:12 +02:00
// FileServer takes a filesystem and returns a Responder which uses that filesystem.
// The returned Responder sanitizes paths before handling them.
2020-10-21 22:28:50 +02:00
func FileServer(fsys FS) Responder {
2020-10-12 00:57:04 +02:00
return fsHandler{fsys}
}
type fsHandler struct {
FS
}
2020-10-21 22:28:50 +02:00
func (fsh fsHandler) Respond(w *ResponseWriter, r *Request) {
2020-10-28 20:13:31 +01:00
p := path.Clean(r.URL.Path)
f, err := fsh.Open(p)
2020-10-12 00:57:04 +02:00
if err != nil {
w.WriteStatus(StatusNotFound)
2020-10-12 00:57:04 +02:00
return
}
2020-10-12 02:13:53 +02:00
// Detect mimetype
2020-10-28 20:13:31 +01:00
ext := path.Ext(p)
2020-10-12 02:13:53 +02:00
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(mimetype)
2020-10-12 00:57:04 +02:00
// Copy file to response writer
2020-10-14 02:22:12 +02:00
io.Copy(w, f)
2020-10-12 00:57:04 +02:00
}
// TODO: replace with io/fs.FS when available
type FS interface {
Open(name string) (File, error)
}
// TODO: replace with io/fs.File when available
type File interface {
Stat() (os.FileInfo, error)
Read([]byte) (int, error)
Close() error
}
// Dir implements FS using the native filesystem restricted to a specific directory.
type Dir string
// Open tries to open the file with the given name.
// If the file is a directory, it tries to open the index file in that directory.
func (d Dir) Open(name string) (File, error) {
p := path.Join(string(d), name)
2020-10-27 18:32:48 +01:00
return openFile(p)
}
// ServeFile responds to the request with the contents of the named file
// or directory.
// TODO: Use io/fs.FS when available.
func ServeFile(w *ResponseWriter, fs FS, name string) {
f, err := fs.Open(name)
if err != nil {
w.WriteStatus(StatusNotFound)
return
}
// Detect mimetype
2020-10-28 20:13:31 +01:00
ext := path.Ext(name)
2020-10-27 18:32:48 +01:00
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(mimetype)
2020-10-27 18:32:48 +01:00
// Copy file to response writer
io.Copy(w, f)
}
func openFile(p string) (File, error) {
2020-10-12 00:57:04 +02:00
f, err := os.OpenFile(p, os.O_RDONLY, 0644)
if err != nil {
return nil, err
}
if stat, err := f.Stat(); err == nil {
if stat.IsDir() {
f, err := os.Open(path.Join(p, "index.gmi"))
if err != nil {
return nil, err
}
stat, err := f.Stat()
if err != nil {
return nil, err
}
if stat.Mode().IsRegular() {
return f, nil
}
2020-11-05 05:46:05 +01:00
return nil, os.ErrNotExist
2020-10-12 00:57:04 +02:00
} else if !stat.Mode().IsRegular() {
2020-11-05 05:46:05 +01:00
return nil, os.ErrNotExist
2020-10-12 00:57:04 +02:00
}
}
return f, nil
}