]> Sergey Matveev's repositories - godlighty.git/blob - dirlist.go
Use mtime instead of ctime
[godlighty.git] / dirlist.go
1 // godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
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.
7 //
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.
12 //
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/>.
15
16 package godlighty
17
18 import (
19         _ "embed"
20         "html/template"
21         "io/fs"
22         "os"
23         "path"
24         "sort"
25         "time"
26
27         "github.com/dustin/go-humanize"
28 )
29
30 const MTimeFmt = "2006-01-02 15:04:05Z"
31
32 var (
33         //go:embed dirlist.tmpl
34         DirListTmplRaw string
35         DirListTmpl    = template.Must(template.New("dirlist").Parse(DirListTmplRaw))
36 )
37
38 type ByName []fs.DirEntry
39
40 func (a ByName) Len() int {
41         return len(a)
42 }
43
44 func (a ByName) Swap(i, j int) {
45         a[i], a[j] = a[j], a[i]
46 }
47
48 func (a ByName) Less(i, j int) bool {
49         return a[i].Name() < a[j].Name()
50 }
51
52 type DirListFile struct {
53         Idx     int
54         Name    string
55         Size    string
56         IsDir   bool
57         Type    string
58         ModTime string
59         Symlink string
60 }
61
62 func dirList(
63         cfg *HostCfg,
64         dir, pth 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                 if (entry.Type() & fs.ModeSymlink) > 0 {
87                         file.Symlink, _ = os.Readlink(path.Join(pth, fi.Name()))
88                 }
89                 files = append(files, file)
90         }
91         fd, err := os.CreateTemp("", "godlist-dirlist-")
92         if err != nil {
93                 return nil, err
94         }
95         os.Remove(fd.Name())
96         var root string
97         if dir == "/" {
98                 dir = ""
99                 root = "/"
100         }
101         err = DirListTmpl.Execute(fd, struct {
102                 Root   string
103                 Dir    string
104                 Files  []DirListFile
105                 Readme string
106         }{
107                 Root:   root,
108                 Dir:    dir,
109                 Files:  files,
110                 Readme: readme,
111         })
112         if err != nil {
113                 fd.Close()
114                 return nil, err
115         }
116         return fd, err
117 }