fs: Show listing for directories without index files
This commit is contained in:
parent
e3d1fc2785
commit
fa7ec1ac87
89
fs.go
89
fs.go
|
@ -1,10 +1,14 @@
|
||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"mime"
|
"mime"
|
||||||
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -39,20 +43,6 @@ func (fs fileServer) ServeGemini(w ResponseWriter, r *Request) {
|
||||||
// the provided name is constructed from user input, it should be sanitized
|
// the provided name is constructed from user input, it should be sanitized
|
||||||
// before calling ServeFile.
|
// before calling ServeFile.
|
||||||
func ServeFile(w ResponseWriter, fsys fs.FS, name string) {
|
func ServeFile(w ResponseWriter, fsys fs.FS, name string) {
|
||||||
f, err := openFile(fsys, name)
|
|
||||||
if err != nil {
|
|
||||||
w.Status(StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Detect mimetype
|
|
||||||
ext := path.Ext(name)
|
|
||||||
mimetype := mime.TypeByExtension(ext)
|
|
||||||
w.Meta(mimetype)
|
|
||||||
// Copy file to response writer
|
|
||||||
_, _ = io.Copy(w, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
func openFile(fsys fs.FS, name string) (fs.File, error) {
|
|
||||||
if name == "/" {
|
if name == "/" {
|
||||||
name = "."
|
name = "."
|
||||||
} else {
|
} else {
|
||||||
|
@ -61,31 +51,68 @@ func openFile(fsys fs.FS, name string) (fs.File, error) {
|
||||||
|
|
||||||
f, err := fsys.Open(name)
|
f, err := fsys.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
w.Status(StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
stat, err := f.Stat()
|
stat, err := f.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
w.Status(StatusTemporaryFailure)
|
||||||
}
|
return
|
||||||
if stat.Mode().IsRegular() {
|
|
||||||
return f, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if stat.IsDir() {
|
if stat.IsDir() {
|
||||||
// Try opening index.gmi
|
// Try opening index file
|
||||||
f, err := fsys.Open(path.Join(name, "index.gmi"))
|
index, err := fsys.Open(path.Join(name, "index.gmi"))
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return nil, err
|
defer index.Close()
|
||||||
}
|
istat, err := index.Stat()
|
||||||
stat, err := f.Stat()
|
if err == nil {
|
||||||
if err != nil {
|
f = index
|
||||||
return nil, err
|
stat = istat
|
||||||
}
|
}
|
||||||
if stat.Mode().IsRegular() {
|
|
||||||
return f, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fs.ErrNotExist
|
if stat.IsDir() {
|
||||||
|
// Failed to find index file
|
||||||
|
dirList(w, f)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect mimetype from file extension
|
||||||
|
ext := path.Ext(name)
|
||||||
|
mimetype := mime.TypeByExtension(ext)
|
||||||
|
w.Meta(mimetype)
|
||||||
|
io.Copy(w, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dirList(w ResponseWriter, f fs.File) {
|
||||||
|
var entries []fs.DirEntry
|
||||||
|
var err error
|
||||||
|
d, ok := f.(fs.ReadDirFile)
|
||||||
|
if ok {
|
||||||
|
entries, err = d.ReadDir(-1)
|
||||||
|
}
|
||||||
|
if !ok || err != nil {
|
||||||
|
w.Header(StatusTemporaryFailure, "Error reading directory")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(entries, func(i, j int) bool {
|
||||||
|
return entries[i].Name() < entries[j].Name()
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
name := entry.Name()
|
||||||
|
if entry.IsDir() {
|
||||||
|
name += "/"
|
||||||
|
}
|
||||||
|
link := LineLink{
|
||||||
|
Name: name,
|
||||||
|
URL: (&url.URL{Path: name}).EscapedPath(),
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, link.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user