]> Sergey Matveev's repositories - godlighty.git/blob - dirlist.go
Trivial restyling
[godlighty.git] / dirlist.go
1 /*
2 godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
3 Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package godlighty
19
20 import (
21         _ "embed"
22         "html/template"
23         "io/fs"
24         "os"
25         "sort"
26         "time"
27
28         "github.com/dustin/go-humanize"
29 )
30
31 const MTimeFmt = "2006-01-02 15:04:05Z"
32
33 var (
34         //go:embed dirlist.tmpl
35         DirListTmplRaw string
36         DirListTmpl    = template.Must(template.New("dirlist").Parse(DirListTmplRaw))
37 )
38
39 type ByName []fs.DirEntry
40
41 func (a ByName) Len() int {
42         return len(a)
43 }
44
45 func (a ByName) Swap(i, j int) {
46         a[i], a[j] = a[j], a[i]
47 }
48
49 func (a ByName) Less(i, j int) bool {
50         return a[i].Name() < a[j].Name()
51 }
52
53 type DirListFile struct {
54         Idx     int
55         Name    string
56         Size    string
57         IsDir   bool
58         Type    string
59         ModTime string
60 }
61
62 func dirList(
63         cfg *HostCfg,
64         dir string,
65         entries []os.DirEntry,
66         readme string,
67 ) (*os.File, error) {
68         sort.Sort(ByName(entries))
69         files := make([]DirListFile, 0, len(entries))
70         for i, entry := range entries {
71                 fi, err := entry.Info()
72                 if err != nil {
73                         return nil, err
74                 }
75                 file := DirListFile{
76                         Idx:     i,
77                         Name:    fi.Name(),
78                         Size:    humanize.IBytes(uint64(fi.Size())),
79                         ModTime: fi.ModTime().UTC().Truncate(time.Second).Format(MTimeFmt),
80                 }
81                 if fi.IsDir() {
82                         file.IsDir = true
83                 } else {
84                         file.Type = mediaType(file.Name, cfg.MIMEs)
85                 }
86                 files = append(files, file)
87         }
88         fd, err := os.CreateTemp("", "godlist-dirlist-")
89         if err != nil {
90                 return nil, err
91         }
92         os.Remove(fd.Name())
93         var root string
94         if dir == "/" {
95                 dir = ""
96                 root = "/"
97         }
98         err = DirListTmpl.Execute(fd, struct {
99                 Root   string
100                 Dir    string
101                 Files  []DirListFile
102                 Readme string
103         }{
104                 Root:   root,
105                 Dir:    dir,
106                 Files:  files,
107                 Readme: readme,
108         })
109         if err != nil {
110                 fd.Close()
111                 return nil, err
112         }
113         return fd, err
114 }