2 godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
3 Copyright (C) 2021-2022 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/>.
28 "github.com/dustin/go-humanize"
31 const MTimeFmt = "2006-01-02 15:04:05Z"
34 //go:embed dirlist.tmpl
36 DirListTmpl = template.Must(template.New("dirlist").Parse(DirListTmplRaw))
39 type ByName []fs.DirEntry
41 func (a ByName) Len() int {
45 func (a ByName) Swap(i, j int) {
46 a[i], a[j] = a[j], a[i]
49 func (a ByName) Less(i, j int) bool {
50 return a[i].Name() < a[j].Name()
53 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 files = append(files, file)
88 fd, err := os.CreateTemp("", "godlist-dirlist-")
98 err = DirListTmpl.Execute(fd, struct {