added back anchor and selfheal broken chunks

This commit is contained in:
ston1th 2022-10-09 01:12:05 +02:00
commit 6122768619
8 changed files with 65 additions and 44 deletions

View file

@ -47,8 +47,9 @@ type dirContents struct {
}
type dir struct {
Name template.HTML
URI template.HTML
Name template.HTML
URI template.HTML
Anchor string
}
type dirs []dir
@ -96,7 +97,19 @@ func (r *responseInterceptor) Status() int {
return r.status
}
func (r *responseInterceptor) GetPaths(path string, filesystem *fs.FS, relative bool) (dc dirContents, err error) {
func xmldecode(s string) string {
s = strings.ReplaceAll(s, "&", "&")
s = strings.ReplaceAll(s, "'", "'")
return s
}
func urlencode(s string) string {
s = strings.ReplaceAll(s, "[", "%5B")
s = strings.ReplaceAll(s, "]", "%5D")
return s
}
func (r *responseInterceptor) GetPaths(path string, filesystem *fs.FS, relative bool) (dc dirContents, pa string, err error) {
buf := r.buf.Bytes()
buf = bytes.ReplaceAll(buf, []byte{'&'}, []byte("&"))
err = xml.Unmarshal(buf, &dc)
@ -105,19 +118,20 @@ func (r *responseInterceptor) GetPaths(path string, filesystem *fs.FS, relative
}
dc.Base = path
path = strings.TrimSuffix(path, "/")
pa = "#" + anchor(path)
for _, p := range dc.AllPaths {
p = strings.ReplaceAll(p, "&", "&")
p = strings.ReplaceAll(p, "'", "'")
p = xmldecode(p)
name := template.HTML(p)
full := path + "/" + p
uri := template.HTML(full)
uri := template.HTML(urlencode(full))
if relative {
uri = template.HTML(p)
uri = template.HTML(urlencode(p))
}
if p[len(p)-1] == '/' {
dc.Dirs = append(dc.Dirs, dir{
Name: name,
URI: uri,
Name: name,
URI: uri,
Anchor: pathAnchor(p),
})
} else {
dc.Files = append(dc.Files, file{
@ -139,7 +153,7 @@ func getPreloads(path string, filesystem *fs.FS) (dc dirContents) {
for _, p := range filesystem.Preloads() {
dc.Files = append(dc.Files, file{
Name: template.HTML(p.Name),
URI: template.HTML(p.Name),
URI: template.HTML(urlencode(p.Name)),
Size: fs.FileSize(p.Size),
Rate: p.Rate(),
Status: p.Status,
@ -151,6 +165,10 @@ func getPreloads(path string, filesystem *fs.FS) (dc dirContents) {
return
}
func pathAnchor(s string) string {
return anchor(strings.TrimSuffix(s, "/"))
}
func anchor(s string) string {
if i := strings.LastIndex(s, "/"); i > 0 && i+1 < len(s) {
s = s[i+1:]