tMove PUT/GET handlers to their own functions - partage - File upload system
 (HTM) git clone git://git.z3bra.org/partage.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit fcfbd59d73a493ce08a2805ab3ffd7cc42fd8b86
 (DIR) parent db7c8a75c2da5d006f6160b3e911b503445849d9
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon, 11 Oct 2021 20:35:15 +0200
       
       Move PUT/GET handlers to their own functions
       
       Diffstat:
         M partage.go                          |      74 +++++++++++++++++--------------
       
       1 file changed, 41 insertions(+), 33 deletions(-)
       ---
 (DIR) diff --git a/partage.go b/partage.go
       t@@ -92,14 +92,50 @@ func servefile(f *os.File, w http.ResponseWriter) {
                }
        }
        
       -func parse(w http.ResponseWriter, r *http.Request) {
       -
       +func uploaderPut(w http.ResponseWriter, r *http.Request) {
                // Max 15 Gb uploads
                if r.ContentLength > conf.maxsize {
                        w.WriteHeader(http.StatusRequestEntityTooLarge)
                        w.Write([]byte("File is too big"))
                }
        
       +        tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path))
       +        f, err := os.Create(tmp.Name())
       +        if err != nil {
       +                fmt.Println(err)
       +                return
       +        }
       +        defer f.Close()
       +
       +        if writefile(f, r.Body, r.ContentLength) < 0 {
       +                w.WriteHeader(http.StatusInternalServerError)
       +                return
       +        }
       +
       +        resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n"
       +        w.Write([]byte(resp))
       +}
       +
       +func uploaderGet(w http.ResponseWriter, r *http.Request) {
       +        // r.URL.Path is sanitized regarding "." and ".."
       +        filename := r.URL.Path
       +        if r.URL.Path == "/" {
       +                filename = "/index.html"
       +        }
       +
       +        f, err := os.Open(conf.rootdir + filename)
       +        if err != nil {
       +                w.WriteHeader(http.StatusNotFound)
       +                fmt.Println(err)
       +                return
       +        }
       +        defer f.Close()
       +
       +        servefile(f, w)
       +}
       +
       +func uploader(w http.ResponseWriter, r *http.Request) {
       +
                err := r.ParseForm()
                if err != nil {
                        fmt.Printf("%s %s: %s", r.Method, r.URL.Path, err)
       t@@ -107,38 +143,10 @@ func parse(w http.ResponseWriter, r *http.Request) {
        
                switch r.Method {
                case "PUT":
       -                tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(r.URL.Path))
       -                f, err := os.Create(tmp.Name())
       -                if err != nil {
       -                        fmt.Println(err)
       -                        return
       -                }
       -                defer f.Close()
       -
       -                if writefile(f, r.Body, r.ContentLength) < 0 {
       -                        w.WriteHeader(http.StatusInternalServerError)
       -                        return
       -                }
       -
       -                resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) + "\r\n"
       -                w.Write([]byte(resp))
       +                uploaderPut(w, r)
        
                case "GET":
       -                // r.URL.Path is sanitized regarding "." and ".."
       -                filename := r.URL.Path
       -                if r.URL.Path == "/" {
       -                        filename = "/index.html"
       -                }
       -
       -                f, err := os.Open(conf.rootdir + filename)
       -                if err != nil {
       -                        w.WriteHeader(http.StatusNotFound)
       -                        fmt.Println(err)
       -                        return
       -                }
       -                defer f.Close()
       -
       -                servefile(f, w)
       +                uploaderGet(w, r)
                }
        }
        
       t@@ -150,7 +158,7 @@ func main() {
                conf.baseuri = "http://192.168.0.3:8080"
                conf.filectx = "/f/"
        
       -        http.HandleFunc("/", parse)
       +        http.HandleFunc("/", uploader)
                http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
                http.ListenAndServe("0.0.0.0:8080", nil)
        }