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