2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-10-11 16:57:04 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2020-10-11 18:13:53 -06:00
|
|
|
"mime"
|
2020-10-11 16:57:04 -06:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2020-10-11 18:13:53 -06:00
|
|
|
func init() {
|
|
|
|
// Add Gemini mime types
|
2021-01-14 20:25:07 -07:00
|
|
|
mime.AddExtensionType(".gmi", "text/gemini")
|
|
|
|
mime.AddExtensionType(".gemini", "text/gemini")
|
2020-10-11 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:50:50 -07:00
|
|
|
// FileServer returns a handler that serves Gemini requests with the contents
|
2021-02-14 17:27:56 -07:00
|
|
|
// of the provided file system.
|
2021-02-08 10:50:50 -07:00
|
|
|
// The returned handler cleans paths before handling them.
|
|
|
|
func FileServer(fsys FS) Handler {
|
2020-10-11 16:57:04 -06:00
|
|
|
return fsHandler{fsys}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fsHandler struct {
|
|
|
|
FS
|
|
|
|
}
|
|
|
|
|
2021-02-09 07:45:10 -07:00
|
|
|
func (fsh fsHandler) ServeGemini(w ResponseWriter, r *Request) {
|
2020-10-28 13:13:31 -06:00
|
|
|
p := path.Clean(r.URL.Path)
|
|
|
|
f, err := fsh.Open(p)
|
2020-10-11 16:57:04 -06:00
|
|
|
if err != nil {
|
2021-01-07 15:08:50 -07:00
|
|
|
w.Status(StatusNotFound)
|
2020-10-11 16:57:04 -06:00
|
|
|
return
|
|
|
|
}
|
2020-10-11 18:13:53 -06:00
|
|
|
// Detect mimetype
|
2020-10-28 13:13:31 -06:00
|
|
|
ext := path.Ext(p)
|
2020-10-11 18:13:53 -06:00
|
|
|
mimetype := mime.TypeByExtension(ext)
|
2021-01-09 22:50:35 -07:00
|
|
|
w.Meta(mimetype)
|
2020-10-11 16:57:04 -06:00
|
|
|
// Copy file to response writer
|
2021-01-07 15:08:50 -07:00
|
|
|
_, _ = io.Copy(w, f)
|
2020-10-11 16:57:04 -06:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:27:56 -07:00
|
|
|
// FS represents a file system.
|
2020-10-11 16:57:04 -06:00
|
|
|
type FS interface {
|
|
|
|
Open(name string) (File, error)
|
|
|
|
}
|
|
|
|
|
2021-01-14 20:24:26 -07:00
|
|
|
// File represents a file.
|
2020-10-11 16:57:04 -06:00
|
|
|
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 11:32:48 -06:00
|
|
|
return openFile(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeFile responds to the request with the contents of the named file
|
|
|
|
// or directory.
|
2021-02-09 07:45:10 -07:00
|
|
|
func ServeFile(w ResponseWriter, fs FS, name string) {
|
2020-10-27 11:32:48 -06:00
|
|
|
f, err := fs.Open(name)
|
|
|
|
if err != nil {
|
2021-01-07 15:08:50 -07:00
|
|
|
w.Status(StatusNotFound)
|
2020-10-27 11:32:48 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Detect mimetype
|
2020-10-28 13:13:31 -06:00
|
|
|
ext := path.Ext(name)
|
2020-10-27 11:32:48 -06:00
|
|
|
mimetype := mime.TypeByExtension(ext)
|
2021-01-09 22:50:35 -07:00
|
|
|
w.Meta(mimetype)
|
2020-10-27 11:32:48 -06:00
|
|
|
// Copy file to response writer
|
2021-01-07 15:08:50 -07:00
|
|
|
_, _ = io.Copy(w, f)
|
2020-10-27 11:32:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func openFile(p string) (File, error) {
|
2020-10-11 16:57:04 -06: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-04 21:46:05 -07:00
|
|
|
return nil, os.ErrNotExist
|
2020-10-11 16:57:04 -06:00
|
|
|
} else if !stat.Mode().IsRegular() {
|
2020-11-04 21:46:05 -07:00
|
|
|
return nil, os.ErrNotExist
|
2020-10-11 16:57:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|