From 8b2bdd0dc2036a77a43f2944234bb94d94fb0d5f Mon Sep 17 00:00:00 2001 From: tuhao Date: Thu, 28 Jan 2021 23:38:05 +0800 Subject: [PATCH 1/2] modifiy DirList's default html format --- core/router/fs.go | 53 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/core/router/fs.go b/core/router/fs.go index 0bbafd64e..02715af72 100644 --- a/core/router/fs.go +++ b/core/router/fs.go @@ -13,8 +13,11 @@ import ( "os" "path" "path/filepath" + "reflect" "regexp" + "runtime" "sort" + "strconv" "strings" "sync" "time" @@ -103,6 +106,9 @@ type DirOptions struct { // See `DirListRich` package-level function too. DirList DirListFunc + // show hidden files or directories or not when `ShowList` is true + ShowHidden bool + // Files downloaded and saved locally. Attachments Attachments @@ -526,6 +532,22 @@ func toBaseName(s string) string { return s } +// IsHidden checks a file is hidden or not +func IsHidden(file os.FileInfo) bool { + isHidden := false + if runtime.GOOS == "windows" { + fa := reflect.ValueOf(file.Sys()).Elem().FieldByName("FileAttributes").Uint() + bytefa := []byte(strconv.FormatUint(fa, 2)) + if bytefa[len(bytefa)-2] == '1' { + isHidden = true + } + } else { + isHidden = file.Name()[0] == '.' + } + + return isHidden +} + // DirList is a `DirListFunc` which renders directories and files in html, but plain, mode. // See `DirListRich` for more. func DirList(ctx *context.Context, dirOptions DirOptions, dirName string, dir http.File) error { @@ -537,12 +559,33 @@ func DirList(ctx *context.Context, dirOptions DirOptions, dirName string, dir ht sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) ctx.ContentType(context.ContentHTMLHeaderValue) - _, err = ctx.WriteString("
\n")
+	_, err = ctx.WriteString("
\n") + if err != nil { + return err + } + + // show current directory + _, err = ctx.Writef("

Current Directory: %s

", ctx.Request().RequestURI) + if err != nil { + return err + } + + _, err = ctx.WriteString("
    ") + if err != nil { + return err + } + + // link to parent directory + _, err = ctx.WriteString("
  • drwxrwxrwx../
  • ") if err != nil { return err } for _, d := range dirs { + if !dirOptions.ShowHidden && IsHidden(d) { + continue + } + name := toBaseName(d.Name()) upath := path.Join(ctx.Request().RequestURI, name) @@ -561,12 +604,16 @@ func DirList(ctx *context.Context, dirOptions DirOptions, dirName string, dir ht // name may contain '?' or '#', which must be escaped to remain // part of the URL path, and not indicate the start of a query // string or fragment. - _, err = ctx.Writef("%s\n", url.String(), downloadAttr, html.EscapeString(viewName)) + _, err = ctx.Writef("
  • "+ + "%s"+ + "%s"+ + "
  • ", + d.Mode().String(), url.String(), downloadAttr, html.EscapeString(viewName)) if err != nil { return err } } - _, err = ctx.WriteString("
\n") + _, err = ctx.WriteString("\n") return err } From 33344283b8271a2d9ca21addee829bbb8d759b8e Mon Sep 17 00:00:00 2001 From: tuhao Date: Fri, 29 Jan 2021 01:10:40 +0800 Subject: [PATCH 2/2] use DirOptions.ShowHidden in DirListRich --- core/router/fs.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/router/fs.go b/core/router/fs.go index 02715af72..1192bb3cd 100644 --- a/core/router/fs.go +++ b/core/router/fs.go @@ -661,6 +661,10 @@ func DirListRich(opts ...DirListRichOptions) DirListFunc { } for _, d := range dirs { + if !dirOptions.ShowHidden && IsHidden(d) { + continue + } + name := toBaseName(d.Name()) upath := path.Join(ctx.Request().RequestURI, name)