From 69901b0beadcdb21e3350622335cdb42d7351676 Mon Sep 17 00:00:00 2001 From: ache Date: Sat, 17 Apr 2021 02:15:46 +0200 Subject: Trully Allow Hidden File / Directory --- server.go | 57 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 22 deletions(-) (limited to 'server.go') diff --git a/server.go b/server.go index aa4a6a5..9b1cc1a 100644 --- a/server.go +++ b/server.go @@ -12,6 +12,7 @@ import ( "net/url" "errors" "strconv" + "strings" "davy/config" ) @@ -60,6 +61,20 @@ func normaliseSize( size int64) string { } return fmt.Sprintf("%do", size); } + +func hasHidden( path string ) bool { + if path == "." { + return false + } + + for _, sub_node := range strings.Split(path, "/") { + if strings.HasPrefix(sub_node, ".") { + return true + } + } + + return false +} func getPath( r *http.Request ) string { if r.URL.Path == "/" { return "." @@ -67,21 +82,30 @@ func getPath( r *http.Request ) string { return r.URL.Path[1:] } +func writeNotFound( w http.ResponseWriter, writeMessage bool) { + w.WriteHeader(http.StatusNotFound) + if writeMessage { + fmt.Fprintln(w, "File not found") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Sry bro") + } +} + func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { filename := getPath(r) fmt.Printf("Trying to get [%s]\n", filename) + if !config.Config.AllowHidden && hasHidden(filename) { + writeNotFound(w, !headOnly) + return + } + stat, err := os.Stat(filename) switch { case err != nil || stat == nil : if os.IsNotExist(err) { - w.WriteHeader(http.StatusNotFound) - if ! headOnly { - fmt.Fprintln(w, "File not found") - fmt.Fprintln(w, "") - fmt.Fprintln(w, "Sry bro") - } + writeNotFound(w, !headOnly) return } case stat.IsDir() : @@ -99,12 +123,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { f, err := os.Open(filename) if err != nil { - w.WriteHeader(http.StatusNotFound) - if !headOnly { - fmt.Fprintln(w, "File not found") - fmt.Fprintln(w, "") - fmt.Fprintln(w, "Sry bro") - } + writeNotFound(w, !headOnly) return } @@ -112,12 +131,7 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { f.Close() if err != nil { - w.WriteHeader(http.StatusNotFound) - if !headOnly { - fmt.Fprintln(w, "File not found") - fmt.Fprintln(w, "") - fmt.Fprintln(w, "Sry bro") - } + writeNotFound(w, !headOnly) } initPath := r.URL.Path; @@ -130,11 +144,11 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { for _, file := range fileInfo { if file.IsDir() { - if config.Config.AllowHidden || file.Name()[0] != '.' { + if !config.Config.HideHidden || file.Name()[0] != '.' { cols = append(cols, Entry{file.Name(), initPath + file.Name(), 0, ""}) } } else { - if config.Config.AllowHidden || file.Name()[0] != '.' { + if !config.Config.HideHidden || file.Name()[0] != '.' { files = append(files, Entry{file.Name(), initPath + file.Name(), file.Size(), normaliseSize(file.Size())}) } } @@ -167,8 +181,7 @@ func handleDelete( w http.ResponseWriter, r *http.Request) { if err != nil { if os.IsNotExist(err) { - w.WriteHeader(http.StatusNotFound) - fmt.Fprintf(w, "Bro, they is nothing here") + writeNotFound(w, true) } else { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Shit happen") -- cgit v1.2.3