From 82f4077b6fcfb051cef9df3d3a1809477a2e2c4a Mon Sep 17 00:00:00 2001 From: Emile Date: Sun, 15 Mar 2020 23:16:07 +0100 Subject: filters --- config.yml | 14 ++++++++++++++ src/http/http.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/config.yml b/config.yml index c3fcb94..6c6780c 100644 --- a/config.yml +++ b/config.yml @@ -19,3 +19,17 @@ time: # # Jan 2 15:04:05 2006 MST format: "2006-01-02 03:04:05 MST" + +hide: + # hide files starting with a dot + files: true + + # hide files with the given extensions + extensions: + - aux + - log + - out + - synctex.gz + - tex + - toc + diff --git a/src/http/http.go b/src/http/http.go index 195a0f6..cdfdf68 100644 --- a/src/http/http.go +++ b/src/http/http.go @@ -98,6 +98,11 @@ func pathHandler(w http.ResponseWriter, r *http.Request) { for _, f := range files { logrus.Tracef("f: %s", f.Name()) + filterResult := filterIsValid(f.Name()) + if filterResult == true { + continue + } + // get the file or dirs modtime and format it in a readable way as // described in the config modTime := f.ModTime() @@ -180,7 +185,6 @@ func pathHandler(w http.ResponseWriter, r *http.Request) { err = t.ExecuteTemplate(w, "index", content) logrus.Warn(err) - w.WriteHeader(http.StatusInternalServerError) return } @@ -218,3 +222,32 @@ func breadcrumbs(r *http.Request) structs.Breadcrumbs { return breadcrumbs } + +// filter filters files returning true if they should be displayed and false if +// not. The descision is made using the hide config from the config file +func filterIsValid(name string) bool { + + // hide files starting with a dot if the "hide.file" directive is set + if viper.GetBool("hide.files") == true { + logrus.Info("checking hide") + if name[0] != '.' { + logrus.Info("hide") + return false + } + } + + extensions := viper.GetStringSlice("hide.extensions") + + splitFileName := strings.Split(name, ".") + fileExtension := splitFileName[len(splitFileName)-1] + + for _, extension := range extensions { + if fileExtension == extension { + logrus.Info("extension") + logrus.Info(extension) + return false + } + } + + return true +} -- cgit 1.4.1