From 5dc004037562d653cc33d5798e17e2f415b210cb Mon Sep 17 00:00:00 2001 From: Gary Marigliano Date: Thu, 9 Mar 2023 11:48:20 +0100 Subject: [PATCH] Make deep path depth configurable Allow to configure with --deep-path-max-depth the depth of paths to combine when listing the folders. The default is still 5. --- httpstaticserver.go | 25 +++++++++++++------------ main.go | 4 ++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/httpstaticserver.go b/httpstaticserver.go index c5ddf9e..4e4e182 100644 --- a/httpstaticserver.go +++ b/httpstaticserver.go @@ -48,15 +48,16 @@ type Directory struct { } type HTTPStaticServer struct { - Root string - Prefix string - Upload bool - Delete bool - Title string - Theme string - PlistProxy string - GoogleTrackerID string - AuthType string + Root string + Prefix string + Upload bool + Delete bool + Title string + Theme string + PlistProxy string + GoogleTrackerID string + AuthType string + DeepPathMaxDepth int indexes []IndexFileItem m *mux.Router @@ -571,6 +572,7 @@ func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) { auth := s.readAccessConf(realPath) auth.Upload = auth.canUpload(r) auth.Delete = auth.canDelete(r) + maxDepth := s.DeepPathMaxDepth // path string -> info os.FileInfo fileInfoMap := make(map[string]os.FileInfo, 0) @@ -615,7 +617,7 @@ func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) { lr.Name = filepath.ToSlash(name) // fix for windows } if info.IsDir() { - name := deepPath(realPath, info.Name()) + name := deepPath(realPath, info.Name(), maxDepth) lr.Name = name lr.Path = filepath.Join(filepath.Dir(path), name) lr.Type = "dir" @@ -740,9 +742,8 @@ func (s *HTTPStaticServer) readAccessConf(realPath string) (ac AccessConf) { return } -func deepPath(basedir, name string) string { +func deepPath(basedir, name string, maxDepth int) string { // loop max 5, incase of for loop not finished - maxDepth := 5 for depth := 0; depth <= maxDepth; depth += 1 { finfos, err := ioutil.ReadDir(filepath.Join(basedir, name)) if err != nil || len(finfos) != 1 { diff --git a/main.go b/main.go index 0b5aea4..3f2596d 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,7 @@ type Configure struct { ID string `yaml:"id"` // for oauth2 Secret string `yaml:"secret"` // for oauth2 } `yaml:"auth"` + DeepPathMaxDepth int `yaml:"deep-path-max-depth"` } type httpLogger struct{} @@ -99,6 +100,7 @@ func parseFlags() error { gcfg.Auth.OpenID = defaultOpenID gcfg.GoogleTrackerID = "UA-81205425-2" gcfg.Title = "Go HTTP File Server" + gcfg.DeepPathMaxDepth = 5 kingpin.HelpFlag.Short('h') kingpin.Version(versionMessage()) @@ -121,6 +123,7 @@ func parseFlags() error { kingpin.Flag("plistproxy", "plist proxy when server is not https").Short('p').StringVar(&gcfg.PlistProxy) kingpin.Flag("title", "server title").StringVar(&gcfg.Title) kingpin.Flag("google-tracker-id", "set to empty to disable it").StringVar(&gcfg.GoogleTrackerID) + kingpin.Flag("deep-path-max-depth", "set to -1 to not combine dirs").IntVar(&gcfg.DeepPathMaxDepth) kingpin.Parse() // first parse conf @@ -172,6 +175,7 @@ func main() { ss.Upload = gcfg.Upload ss.Delete = gcfg.Delete ss.AuthType = gcfg.Auth.Type + ss.DeepPathMaxDepth = gcfg.DeepPathMaxDepth if gcfg.PlistProxy != "" { u, err := url.Parse(gcfg.PlistProxy)