about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2020-03-15 23:16:07 +0100
committerEmile <hanemile@protonmail.com>2020-03-15 23:16:07 +0100
commit82f4077b6fcfb051cef9df3d3a1809477a2e2c4a (patch)
tree572ded9d694485a6a8910299d583ed01d01fe17e
parent873ea3b352402635599251bbf2f628fb8f907fa5 (diff)
filters
-rw-r--r--config.yml14
-rw-r--r--src/http/http.go35
2 files changed, 48 insertions, 1 deletions
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
+}