package filesystem
import (
"fmt"
"html"
"net/http"
"os"
"path"
"sort"
"strings"
"github.com/gofiber/fiber/v2"
)
func getFileExtension(path string) string {
n := strings.LastIndexByte(path, '.')
if n < 0 {
return ""
}
return path[n:]
}
func dirList(c *fiber.Ctx, f http.File) error {
fileinfos, err := f.Readdir(-1)
if err != nil {
return err
}
fm := make(map[string]os.FileInfo, len(fileinfos))
filenames := make([]string, 0, len(fileinfos))
for _, fi := range fileinfos {
name := fi.Name()
fm[name] = fi
filenames = append(filenames, name)
}
basePathEscaped := html.EscapeString(c.Path())
fmt.Fprintf(c, "
%s", basePathEscaped)
fmt.Fprintf(c, "%s
", basePathEscaped)
fmt.Fprint(c, "")
if len(basePathEscaped) > 1 {
parentPathEscaped := html.EscapeString(c.Path() + "/..")
fmt.Fprintf(c, `- ..
`, parentPathEscaped)
}
sort.Strings(filenames)
for _, name := range filenames {
pathEscaped := html.EscapeString(path.Join(c.Path() + "/" + name))
fi := fm[name]
auxStr := "dir"
className := "dir"
if !fi.IsDir() {
auxStr = fmt.Sprintf("file, %d bytes", fi.Size())
className = "file"
}
fmt.Fprintf(c, `- %s, %s, last modified %s
`,
pathEscaped, className, html.EscapeString(name), auxStr, fi.ModTime())
}
fmt.Fprint(c, "
")
c.Type("html")
return nil
}