Skip to content

Commit

Permalink
merge pr
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 19, 2023
2 parents a400a02 + e31775a commit 75eb27f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
27 changes: 14 additions & 13 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ type Directory struct {
}

type HTTPStaticServer struct {
Root string
Prefix string
Upload bool
Delete bool
Title string
Theme string
PlistProxy string
GoogleTrackerID string
AuthType string
NoIndex bool
Root string
Prefix string
Upload bool
Delete bool
Title string
Theme string
PlistProxy string
GoogleTrackerID string
AuthType string
DeepPathMaxDepth int
NoIndex bool

indexes []IndexFileItem
m *mux.Router
Expand Down Expand Up @@ -575,6 +576,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)
Expand Down Expand Up @@ -619,7 +621,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"
Expand Down Expand Up @@ -744,9 +746,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 {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ type Configure struct {
ID string `yaml:"id"` // for oauth2
Secret string `yaml:"secret"` // for oauth2
} `yaml:"auth"`
NoIndex bool `yaml:"no-index"`
DeepPathMaxDepth int `yaml:"deep-path-max-depth"`
NoIndex bool `yaml:"no-index"`
}

type httpLogger struct{}
Expand Down Expand Up @@ -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
gcfg.NoIndex = false

kingpin.HelpFlag.Short('h')
Expand All @@ -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.Flag("no-index", "disable indexing").BoolVar(&gcfg.NoIndex)

kingpin.Parse() // first parse conf
Expand Down Expand Up @@ -186,6 +189,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)
Expand Down

0 comments on commit 75eb27f

Please sign in to comment.