fileserver: browse: do not encode the paths in breadcrumbs and page title (#4410)

This commit is contained in:
Mohammed Al Sahaf 2021-11-23 11:13:09 +03:00 committed by GitHub
parent c8b5a81607
commit 1e10f6f725
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -77,9 +77,9 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
Mode: f.Mode(), Mode: f.Mode(),
}) })
} }
name, _ := url.PathUnescape(urlPath)
return browseTemplateContext{ return browseTemplateContext{
Name: path.Base(urlPath), Name: path.Base(name),
Path: urlPath, Path: urlPath,
CanGoUp: canGoUp, CanGoUp: canGoUp,
Items: fileInfos, Items: fileInfos,
@ -133,13 +133,16 @@ func (l browseTemplateContext) Breadcrumbs() []crumb {
if lpath[len(lpath)-1] == '/' { if lpath[len(lpath)-1] == '/' {
lpath = lpath[:len(lpath)-1] lpath = lpath[:len(lpath)-1]
} }
parts := strings.Split(lpath, "/") parts := strings.Split(lpath, "/")
result := make([]crumb, len(parts)) result := make([]crumb, len(parts))
for i, p := range parts { for i, p := range parts {
if i == 0 && p == "" { if i == 0 && p == "" {
p = "/" p = "/"
} }
// the directory name could include an encoded slash in its path,
// so the item name should be unescaped in the loop rather than unescaping the
// entire path outside the loop.
p, _ = url.PathUnescape(p)
lnk := strings.Repeat("../", len(parts)-i-1) lnk := strings.Repeat("../", len(parts)-i-1)
result[i] = crumb{Link: lnk, Text: p} result[i] = crumb{Link: lnk, Text: p}
} }

View file

@ -36,6 +36,19 @@ func TestBreadcrumbs(t *testing.T) {
{Link: "../", Text: "quux"}, {Link: "../", Text: "quux"},
{Link: "", Text: "corge"}, {Link: "", Text: "corge"},
}}, }},
{"/مجلد/", []crumb{
{Link: "../", Text: "/"},
{Link: "", Text: "مجلد"},
}},
{"/مجلد-1/مجلد-2", []crumb{
{Link: "../../", Text: "/"},
{Link: "../", Text: "مجلد-1"},
{Link: "", Text: "مجلد-2"},
}},
{"/مجلد%2F1", []crumb{
{Link: "../", Text: "/"},
{Link: "", Text: "مجلد/1"},
}},
} }
for _, d := range testdata { for _, d := range testdata {