about summary refs log tree commit diff
path: root/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/http')
-rw-r--r--src/http/http.go48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/http/http.go b/src/http/http.go
index 76ca62d..824c7a3 100644
--- a/src/http/http.go
+++ b/src/http/http.go
@@ -22,6 +22,8 @@ func Server() {
 	fs := http.FileServer(http.Dir("./hosted/static"))
 	static.PathPrefix("/").Handler(http.StripPrefix("/static/", fs))
 
+	r.HandleFunc("/download", downloadHandler).Methods("GET")
+
 	t := r.PathPrefix("/").Subrouter()
 	t.PathPrefix("/").HandlerFunc(pathHandler)
 
@@ -39,17 +41,49 @@ func Server() {
 	logrus.Fatal(httpServer.ListenAndServe())
 }
 
+func downloadHandler(w http.ResponseWriter, r *http.Request) {
+	query := r.URL.Query()
+
+	file := query["file"][0]
+
+	logrus.Info(file)
+
+	root := viper.GetString("server.root")
+	logrus.Info(root)
+	strippedFile := strings.Replace(file, root, "", -1)
+	strippedFile = strings.Replace(strippedFile, "..", "", -1)
+
+	logrus.Info(strippedFile)
+	logrus.Infof("stripped: %s", strippedFile)
+
+	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", strippedFile))
+	w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
+
+	actualFile := fmt.Sprintf("%s/%s", root, strippedFile)
+	logrus.Infof("actual: %s", actualFile)
+	http.ServeFile(w, r, actualFile)
+}
+
 func pathHandler(w http.ResponseWriter, r *http.Request) {
 	var content map[string]interface{}
 	content = make(map[string]interface{})
 
-	breadcrumbsList := breadcrumbs(r)
-	content["Breadcrumbs"] = breadcrumbsList
-
 	root := viper.GetString("server.root")
 	requestURI := fmt.Sprintf("%s%s", root, r.RequestURI)
-	logrus.Infof("requestURI: %s", requestURI)
-	logrus.Infof("r.RequestURI: %s", r.RequestURI)
+
+	query := r.URL.Query()
+
+	if query["download"] != nil {
+
+		strippedFile := strings.Replace(requestURI, root, "", -1)
+		strippedFile = strings.Replace(strippedFile, "?download", "", -1)
+		path := fmt.Sprintf("/download?file=%s", strippedFile)
+		http.Redirect(w, r, path, http.StatusSeeOther)
+		return
+	}
+
+	breadcrumbsList := breadcrumbs(r)
+	content["Breadcrumbs"] = breadcrumbsList
 
 	// get all files in the request dir
 	files, err := ioutil.ReadDir(requestURI)
@@ -95,6 +129,8 @@ func pathHandler(w http.ResponseWriter, r *http.Request) {
 		if f.IsDir() == true {
 			item.IsDir = true
 			dirCount++
+		} else {
+			item.Download = true
 		}
 
 		items = append(items, item)
@@ -143,8 +179,6 @@ func pathHandler(w http.ResponseWriter, r *http.Request) {
 
 // breadcrumbs get's the breadcrumbs from the request
 func breadcrumbs(r *http.Request) structs.Breadcrumbs {
-	logrus.Debugf("----------------------------------------------------------")
-	logrus.Debugf("%s\n", r.RequestURI)
 
 	request := r.RequestURI