fix fileserver parent dir with trailing slash

This commit is contained in:
dre 2021-07-10 12:26:55 +08:00
parent e58ec93de3
commit 8e66aa4314

View file

@ -80,6 +80,7 @@ func readFile(filepath string) ([]byte, string, error) {
return nil, "", fmt.Errorf("file: %w", err) return nil, "", fmt.Errorf("file: %w", err)
} }
defer file.Close() defer file.Close()
data, err := ioutil.ReadAll(file) data, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
return nil, "", fmt.Errorf("read: %w", err) return nil, "", fmt.Errorf("read: %w", err)
@ -101,14 +102,15 @@ func listDirectory(fullpath, relpath string) ([]byte, string, error) {
} }
var out []byte var out []byte
parent := filepath.Dir(relpath)
if relpath != "/" {
idx := strings.TrimRight(relpath, "/") idx := strings.TrimRight(relpath, "/")
parent := filepath.Dir(idx)
if relpath != "/" {
out = append(out, []byte(fmt.Sprintf("Index of %s/\n\n", idx))...) out = append(out, []byte(fmt.Sprintf("Index of %s/\n\n", idx))...)
out = append(out, []byte(fmt.Sprintf("=> %s ..\n", parent))...) out = append(out, []byte(fmt.Sprintf("=> %s ..\n", parent))...)
} else { } else {
out = append(out, []byte(fmt.Sprintf("Index of %s\n\n", relpath))...) out = append(out, []byte(fmt.Sprintf("Index of %s\n\n", relpath))...)
} }
for _, f := range files { for _, f := range files {
if relpath == "/" { if relpath == "/" {
out = append(out, []byte(fmt.Sprintf("=> %s\n", f.Name()))...) out = append(out, []byte(fmt.Sprintf("=> %s\n", f.Name()))...)