added directory cleanup

This commit is contained in:
ston1th 2022-03-17 21:10:23 +01:00
commit 74ab10358a
5 changed files with 87 additions and 22 deletions

View file

@ -5,8 +5,8 @@ package srv
import (
"bytes"
"encoding/xml"
"html/template"
"net/http"
"sort"
"strings"
"cachefs/pkg/fs"
@ -41,12 +41,22 @@ func (s *statusInterceptor) Status() int {
type dirContents struct {
AllPaths []string `xml:"a"`
Dirs []string `xml:"-"`
Dirs dirs `xml:"-"`
Files files `xml:"-"`
}
type dir struct {
Name template.HTML
URI template.HTML
}
type dirs []dir
func (dirs) Less(i, j dir) bool { return i.Name < j.Name }
type file struct {
Name string
Name template.HTML
URI template.HTML
Anchor string
Status int
}
@ -80,25 +90,36 @@ func (r *responseInterceptor) Status() int {
return r.status
}
func (r *responseInterceptor) GetPaths(path string, fs *fs.FS) (dir dirContents, err error) {
err = xml.Unmarshal(r.buf.Bytes(), &dir)
func (r *responseInterceptor) GetPaths(path string, fs *fs.FS) (dc dirContents, err error) {
buf := r.buf.Bytes()
buf = bytes.ReplaceAll(buf, []byte{'&'}, []byte("&#38;"))
err = xml.Unmarshal(buf, &dc)
if err != nil {
return
}
path = strings.TrimSuffix(path, "/")
for _, p := range dir.AllPaths {
for _, p := range dc.AllPaths {
p = strings.ReplaceAll(p, "&amp;", "&")
p = strings.ReplaceAll(p, "&#39;", "'")
name := template.HTML(p)
full := path + "/" + p
uri := template.HTML(full)
if p[len(p)-1] == '/' {
dir.Dirs = append(dir.Dirs, p)
dc.Dirs = append(dc.Dirs, dir{
Name: name,
URI: uri,
})
} else {
dir.Files = append(dir.Files, file{
Name: p,
dc.Files = append(dc.Files, file{
Name: name,
URI: uri,
Anchor: anchor(p),
Status: fs.CacheStatus(path + "/" + p),
Status: fs.CacheStatus(full),
})
}
}
sort.Strings(dir.Dirs)
slices.SortFunc(dir.Files, dir.Files.Less)
slices.SortFunc(dc.Dirs, dc.Dirs.Less)
slices.SortFunc(dc.Files, dc.Files.Less)
return
}

View file

@ -24,13 +24,13 @@ var (
</pre>
<table>
<tr><th style="width:100%;">Path</th><th>Options</th></tr>
<tr><td><a href="../">../</a></td><td></td></tr>
<tr><td><a href="../">../</a></td><td>[dir]</td></tr>
{{range $s := .Dirs -}}
<tr><td><a href="{{$s}}">{{$s}}</a></td><td></td></tr>
<tr><td><a href="{{$s.URI}}">{{$s.Name}}</a></td><td>[dir]</td></tr>
{{end -}}
{{range $s := .Files -}}
<tr><td><a id="{{$s.Anchor}}" href="{{$s.Name}}">{{$s.Name}}</a></td>
<td><a href="{{$s.Name}}?o=v">[v]</a>&nbsp;<a href="{{$s.Name}}?o=n">[n]</a>&nbsp;<a href="{{$s.Name}}?o=p">[p]</a>&nbsp;<a href="{{$s.Name}}?o=s">[s]</a>&nbsp;{{if ne $s.Status -1}}{{$s.Status}}%{{end}}</td></tr>
<tr><td><a id="{{$s.Anchor}}" href="{{$s.URI}}">{{$s.Name}}</a></td>
<td><a href="{{$s.URI}}?o=v">[v]</a>&nbsp;<a href="{{$s.URI}}?o=n">[n]</a>&nbsp;<a href="{{$s.URI}}?o=p">[p]</a>&nbsp;<a href="{{$s.URI}}?o=s">[s]</a>&nbsp;{{if ne $s.Status -1}}{{$s.Status}}%{{end}}</td></tr>
{{end -}}
</table>
</article>