From e8ec7d101ba39c59b1f3fcb33e04d9721ed3ddcb Mon Sep 17 00:00:00 2001 From: Chapuis Bertil Date: Wed, 16 Sep 2015 10:37:37 +0200 Subject: [PATCH] replaced singular with plural dir names --- backend/rest/rest.go | 26 ++++++++--- backend/rest_test.go | 108 ++++++++++++++----------------------------- 2 files changed, 55 insertions(+), 79 deletions(-) diff --git a/backend/rest/rest.go b/backend/rest/rest.go index 22a012b66..3b6ac3776 100644 --- a/backend/rest/rest.go +++ b/backend/rest/rest.go @@ -19,14 +19,28 @@ const connLimit = 10 // Returns the url of the resource func restPath(url *url.URL, t backend.Type, name string) string { - location := url.String() - if !strings.HasSuffix(location, "/") { - location += "/" + ept := url.String() + if !strings.HasSuffix(ept, "/") { + ept += "/" } - if t == backend.Config { - return location + string(t) + var dir string + switch t { + case backend.Config: + dir = backend.Paths.Config + case backend.Data: + dir = backend.Paths.Data + case backend.Snapshot: + dir = backend.Paths.Snapshots + case backend.Index: + dir = backend.Paths.Index + case backend.Lock: + dir = backend.Paths.Locks + case backend.Key: + dir = backend.Paths.Keys + default: + dir = string(t) } - return location + string(t) + "/" + name + return ept + dir + "/" + name } type RestBlob struct { diff --git a/backend/rest_test.go b/backend/rest_test.go index 75e5ec539..14232ae09 100644 --- a/backend/rest_test.go +++ b/backend/rest_test.go @@ -2,7 +2,6 @@ package backend_test import ( "encoding/json" - "fmt" "io/ioutil" "net/http" "net/http/httptest" @@ -24,11 +23,11 @@ func TestRestBackend(t *testing.T) { defer os.RemoveAll(path) dirs := []string{ path, - filepath.Join(path, string(backend.Data)), - filepath.Join(path, string(backend.Snapshot)), - filepath.Join(path, string(backend.Index)), - filepath.Join(path, string(backend.Lock)), - filepath.Join(path, string(backend.Key)), + filepath.Join(path, string(backend.Paths.Data)), + filepath.Join(path, string(backend.Paths.Snapshots)), + filepath.Join(path, string(backend.Paths.Index)), + filepath.Join(path, string(backend.Paths.Locks)), + filepath.Join(path, string(backend.Paths.Keys)), } for _, d := range dirs { os.MkdirAll(d, backend.Modes.Dir) @@ -67,15 +66,11 @@ func TestRestBackend(t *testing.T) { ioutil.WriteFile(file, bytes, 0600) }).Methods("POST") - // List the blobs of a given type. - r.HandleFunc("/{type}/", func(w http.ResponseWriter, r *http.Request) { + // List the blobs of a given dir. + r.HandleFunc("/{dir}/", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - blobType, errType := backend.ParseType(filepath.Clean(vars["type"])) - if errType != nil { - http.Error(w, "403 invalid blob type", 403) - return - } - path := filepath.Join(path, string(blobType)) + dir := filepath.Clean(vars["dir"]) + path := filepath.Join(path, dir) files, _ := ioutil.ReadDir(path) names := make([]string, len(files)) for i, f := range files { @@ -85,40 +80,24 @@ func TestRestBackend(t *testing.T) { w.Write(data) }).Methods("GET") - // Check if a blob of a given type exists. - r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) { + // Check if a blob of a given dir exists. + r.HandleFunc("/{dir}/{name}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - blobType, errType := backend.ParseType(filepath.Clean(vars["type"])) - if errType != nil { - http.Error(w, "403 invalid blob type", 403) - return - } - blobID, errID := backend.ParseID(vars["blob"]) - if errID != nil { - http.Error(w, "403 invalid blob ID", 403) - return - } - blob := filepath.Join(path, string(blobType), blobID.String()) - if _, err := os.Stat(blob); err != nil { + dir := filepath.Clean(vars["dir"]) + name := filepath.Clean(vars["name"]) + path := filepath.Join(path, dir, name) + if _, err := os.Stat(path); err != nil { http.Error(w, "404 blob not found", 404) } }).Methods("HEAD") - // Get a blob of a given type. - r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) { + // Get a blob of a given dir. + r.HandleFunc("/{dir}/{name}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - blobType, errType := backend.ParseType(filepath.Clean(vars["type"])) - if errType != nil { - http.Error(w, "403 invalid blob type", 403) - return - } - blobID, errID := backend.ParseID(vars["blob"]) - if errID != nil { - http.Error(w, "403 invalid blob ID", 403) - return - } - blob := filepath.Join(path, string(blobType), blobID.String()) - file, err := os.Open(blob) + dir := filepath.Clean(vars["dir"]) + name := filepath.Clean(vars["name"]) + path := filepath.Join(path, dir, name) + file, err := os.Open(path) defer file.Close() if err != nil { http.Error(w, "404 blob not found", 404) @@ -127,48 +106,31 @@ func TestRestBackend(t *testing.T) { http.ServeContent(w, r, "", time.Unix(0, 0), file) }).Methods("GET") - // Save a blob of a given type. - r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) { + // Save a blob of a given dir. + r.HandleFunc("/{dir}/{name}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - blobType, errType := backend.ParseType(filepath.Clean(vars["type"])) - if errType != nil { - http.Error(w, "403 invalid blob type", 403) - return - } - blobID, errID := backend.ParseID(vars["blob"]) - if errID != nil { - http.Error(w, "403 invalid blob ID", 403) - return - } - blob := filepath.Join(path, string(blobType), blobID.String()) - if _, err := os.Stat(blob); err == nil { + dir := filepath.Clean(vars["dir"]) + name := filepath.Clean(vars["name"]) + path := filepath.Join(path, dir, name) + if _, err := os.Stat(path); err == nil { http.Error(w, "409 blob already uploaded", 409) return } bytes, _ := ioutil.ReadAll(r.Body) - ioutil.WriteFile(blob, bytes, 0600) + ioutil.WriteFile(path, bytes, 0600) }).Methods("POST") - // Delete a blob of a given type. - r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) { + // Delete a blob of a given dir. + r.HandleFunc("/{dir}/{name}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - blobType, errType := backend.ParseType(filepath.Clean(vars["type"])) - if errType != nil { - http.Error(w, "403 invalid blob type", 403) - return - } - blobID, errID := backend.ParseID(vars["blob"]) - if errID != nil { - http.Error(w, "403 invalid blob ID", 403) - return - } - blob := filepath.Join(path, string(blobType), blobID.String()) - if _, err := os.Stat(blob); err != nil { + dir := filepath.Clean(vars["dir"]) + name := filepath.Clean(vars["name"]) + path := filepath.Join(path, dir, name) + if _, err := os.Stat(path); err != nil { http.Error(w, "404 blob not found", 404) return } - if err := os.Remove(blob); err != nil { - fmt.Println(err.Error()) + if err := os.Remove(path); err != nil { http.Error(w, "500 internal server error", 500) return }