Skip to content

Commit

Permalink
Fix bug on uploading file on private folder
Browse files Browse the repository at this point in the history
  • Loading branch information
nodauf committed Feb 19, 2021
1 parent f1b6120 commit b57f285
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/controllers/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func BasicAuth(h http.HandlerFunc) http.HandlerFunc {
}
//fmt.Println("Serving: "+ path.Join((*Private), path.Clean(r.URL.Path)))
// h.ServeHTTP(w, r)
HandleFile(w, r)
//HandleFile(w, r)
h(w, r)
}
}
11 changes: 8 additions & 3 deletions src/controllers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"net/http"
"os"
"path"
"strings"
)

func UploadFile(w http.ResponseWriter, r *http.Request) {
filepath := path.Join((*Root_folder), path.Clean(r.URL.Path))
if strings.Contains(r.URL.Path, "/private/") {
r.URL.Path = strings.Replace(r.URL.Path, "/private/", "", 1)
filepath = path.Join((*Private), path.Clean(r.URL.Path))
}
fmt.Println(r.URL.Path)
// Maximum upload of 1000 MB files
r.ParseMultipartForm(1000 << 20)
r.ParseMultipartForm(10000000 << 20)

// Get handler for filename, size and headers
filesHandler := r.MultipartForm.File["files"]
Expand All @@ -26,7 +32,7 @@ func UploadFile(w http.ResponseWriter, r *http.Request) {
defer file.Close()

// Create file
dst, err := os.Create(*Root_folder + r.URL.Path + handler.Filename)
dst, err := os.Create(filepath + "/" + handler.Filename)
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -38,7 +44,6 @@ func UploadFile(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filepath := path.Join((*Root_folder), path.Clean(r.URL.Path))
f, err := os.Open(filepath)
defer f.Close()
if err != nil {
Expand Down
53 changes: 28 additions & 25 deletions src/routers/router.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
package routers

import "fmt"
import "net/http"
import "SimpleHTTPServer-golang/src/controllers"
import (
"SimpleHTTPServer-golang/src/controllers"
"fmt"
"net/http"
)

// Routing function
func Router(w http.ResponseWriter, req *http.Request) {
fmt.Printf("\"%s %s %s %s\" \"%s\" \"%s\"\n",
req.RemoteAddr,
req.Method,
req.URL.String(),
req.Proto,
req.Referer(),
req.UserAgent()) // TODO: Improve this crappy logging
switch req.Method {
case "GET":
//controllers.HandleFile(w, req)
//controllers.ParseHttpParameter(w, req)
ParseHttpParameter(w, req)
fmt.Printf("\"%s %s %s %s\" \"%s\" \"%s\"\n",
req.RemoteAddr,
req.Method,
req.URL.String(),
req.Proto,
req.Referer(),
req.UserAgent()) // TODO: Improve this crappy logging
switch req.Method {
case "GET":
//controllers.HandleFile(w, req)
//controllers.ParseHttpParameter(w, req)
ParseHttpParameter(w, req)

case "POST":
// Call ParseForm() to parse the raw query and update r.PostForm and r.Form.
if err := req.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
controllers.UploadFile(w,req)
case "POST":
// Call ParseForm() to parse the raw query and update r.PostForm and r.Form.
if err := req.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
controllers.UploadFile(w, req)

default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}

// See https://gist.github.com/elithrar/7600878#comment-955958 for how to extend it to suit simple http.Handler's
func Use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for _, m := range middleware {
h = m(h)

}
return h
}

0 comments on commit b57f285

Please sign in to comment.