1 // godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "github.com/dustin/go-humanize"
30 const MTimeFmt = "2006-01-02 15:04:05Z"
33 //go:embed dirlist.tmpl
35 DirListTmpl = template.Must(template.New("dirlist").Parse(DirListTmplRaw))
38 type ByName []fs.DirEntry
40 func (a ByName) Len() int {
44 func (a ByName) Swap(i, j int) {
45 a[i], a[j] = a[j], a[i]
48 func (a ByName) Less(i, j int) bool {
49 return a[i].Name() < a[j].Name()
52 type DirListFile struct {
65 entries []os.DirEntry,
68 sort.Sort(ByName(entries))
69 files := make([]DirListFile, 0, len(entries))
70 for i, entry := range entries {
71 fi, err := entry.Info()
78 Size: humanize.IBytes(uint64(fi.Size())),
79 ModTime: fi.ModTime().UTC().Truncate(time.Second).Format(MTimeFmt),
84 file.Type = mediaType(file.Name, cfg.MIMEs)
86 if (entry.Type() & fs.ModeSymlink) > 0 {
87 file.Symlink, _ = os.Readlink(path.Join(pth, fi.Name()))
89 files = append(files, file)
91 fd, err := os.CreateTemp("", "godlist-dirlist-")
101 err = DirListTmpl.Execute(fd, struct {