From 27d94f9bb32aba525c9bb31144559582c9749f6a Mon Sep 17 00:00:00 2001 From: Michel Casabianca Date: Tue, 28 Jul 2015 18:45:23 +0200 Subject: [PATCH 1/3] Test code --- cheeseshop.go | 52 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/cheeseshop.go b/cheeseshop.go index 7579c98..bbc4e5f 100644 --- a/cheeseshop.go +++ b/cheeseshop.go @@ -73,16 +73,48 @@ func servePackage(dir, file string, w http.ResponseWriter, r *http.Request) { } func handler(w http.ResponseWriter, r *http.Request) { - parts := strings.Split(r.URL.Path[len(*path):], "/") - if len(parts) > 2 { - http.Error(w, fmt.Sprintf("%s is not a valid path", r.URL.Path), 404) - return - } else if len(parts) == 1 && parts[0] == "" { - listRoot(w, r) - } else if len(parts) == 1 { - listDirectory(parts[0], w, r) - } else { - servePackage(parts[0], parts[1], w, r) + if r.Method == "GET" { + parts := strings.Split(r.URL.Path[len(*path):], "/") + if len(parts) > 2 { + http.Error(w, fmt.Sprintf("%s is not a valid path", r.URL.Path), 404) + return + } else if len(parts) == 1 && parts[0] == "" { + listRoot(w, r) + } else if len(parts) == 1 { + listDirectory(parts[0], w, r) + } else { + servePackage(parts[0], parts[1], w, r) + } + } else if r.Method == "POST" { + err := r.ParseMultipartForm(100000) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + m := r.MultipartForm + // DEBUG + println(fmt.Sprintf(">>>>>>>>>> %#v", m)) + files := m.File["myfiles"] + for i, _ := range files { + // DEBUG + println(files[i].Filename) + //file, err := files[i].Open() + //defer file.Close() + //if err != nil { + // http.Error(w, err.Error(), http.StatusInternalServerError) + // return + //} + //dst, err := os.Create("/home/sanat/" + files[i].Filename) + //defer dst.Close() + //if err != nil { + // http.Error(w, err.Error(), http.StatusInternalServerError) + // return + //} + //if _, err := io.Copy(dst, file); err != nil { + // http.Error(w, err.Error(), http.StatusInternalServerError) + // return + //} + } } } From 24f6cdc8b5757a3d9d6cf45b5cf8d9413fa3dce0 Mon Sep 17 00:00:00 2001 From: Michel Casabianca Date: Tue, 28 Jul 2015 21:56:50 +0200 Subject: [PATCH 2/3] Package upload implemented (without auth) --- cheeseshop.go | 62 +++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/cheeseshop.go b/cheeseshop.go index bbc4e5f..4d4e491 100644 --- a/cheeseshop.go +++ b/cheeseshop.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "io" "io/ioutil" "log" "net/http" @@ -72,6 +73,37 @@ func servePackage(dir, file string, w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filename) } +func copyFile(w http.ResponseWriter, r *http.Request) { + err := r.ParseMultipartForm(100000) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + m := r.MultipartForm + files := m.File["content"] + for _, file := range files { + name := file.Filename + pack := name[:strings.LastIndex(name, "-")] + log.Printf("Writing file %s", name) + f, err := file.Open() + defer f.Close() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + dst, err := os.Create(filepath.Join(*root, pack, name)) + defer dst.Close() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if _, err := io.Copy(dst, f); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } +} + func handler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { parts := strings.Split(r.URL.Path[len(*path):], "/") @@ -86,35 +118,7 @@ func handler(w http.ResponseWriter, r *http.Request) { servePackage(parts[0], parts[1], w, r) } } else if r.Method == "POST" { - err := r.ParseMultipartForm(100000) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - m := r.MultipartForm - // DEBUG - println(fmt.Sprintf(">>>>>>>>>> %#v", m)) - files := m.File["myfiles"] - for i, _ := range files { - // DEBUG - println(files[i].Filename) - //file, err := files[i].Open() - //defer file.Close() - //if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - //} - //dst, err := os.Create("/home/sanat/" + files[i].Filename) - //defer dst.Close() - //if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - //} - //if _, err := io.Copy(dst, file); err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - //} - } + copyFile(w, r) } } From 43a29615b2472a0a327ca8805dd9f28598b9b36a Mon Sep 17 00:00:00 2001 From: Michel Casabianca Date: Tue, 28 Jul 2015 21:59:11 +0200 Subject: [PATCH 3/3] Updated for release --- CHANGELOG.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.yml b/CHANGELOG.yml index e41bf88..0bf9b3c 100644 --- a/CHANGELOG.yml +++ b/CHANGELOG.yml @@ -1,5 +1,11 @@ # Semantic changelog: https://github.com/c4s4/changelog +- version: 1.1.0 + date: 2015-07-28 + summary: Added package upload + added: + - "Package upload without authentication." + - version: 1.0.0 date: 2015-07-28 summary: First release