diff --git a/server/server.go b/server/server.go index 138d8e6..e4f7474 100644 --- a/server/server.go +++ b/server/server.go @@ -6,7 +6,6 @@ import ( "fmt" "math/big" "net/http" - "path/filepath" "strings" "time" @@ -29,7 +28,6 @@ func (s *Server) HandlePut(w http.ResponseWriter, req *http.Request) { s.cors(w) case http.MethodPut: path := strings.TrimPrefix(req.URL.Path, "/put") - s.logger.Infof("uploading to %s", path) s.upload(path, w, req) default: w.WriteHeader(http.StatusBadRequest) @@ -52,8 +50,7 @@ func (s *Server) HandleUpload(w http.ResponseWriter, req *http.Request) { day := now.Format("20060102") prefixHash := hash(prefix) dayHash := hash(now.Format(time.RFC3339Nano)) - s.logger.Infof("uploading to %s/%s%s", day, prefixHash, dayHash) - s.upload(fmt.Sprintf("%s/%s%s", day, prefixHash, dayHash), w, req) + s.upload(fmt.Sprintf("/%s/%s%s", day, prefixHash, dayHash), w, req) default: w.WriteHeader(http.StatusBadRequest) _, _ = w.Write([]byte("This endpoint requires PUT")) @@ -65,6 +62,8 @@ func (s *Server) HandleHealth(w http.ResponseWriter, _ *http.Request) { } func (s *Server) upload(path string, w http.ResponseWriter, req *http.Request) { + s.logger.Infof("uploading to %s", path) + err := s.store.Store(path, req.Body) if err != nil { s.logger.WithError(err).Error("error storing object in bucket") @@ -74,7 +73,8 @@ func (s *Server) upload(path string, w http.ResponseWriter, req *http.Request) { } w.Header().Add("Access-Control-Expose-Headers", "X-File-URL") w.Header().Add("Access-Control-Allow-Origin", "*") - fileUrl := filepath.Join(s.returnUrl, path) + + fileUrl := fmt.Sprintf("%s%s", s.returnUrl, path) w.Header().Add("X-File-URL", fileUrl) s.logger.Infof("success - file is available at %s", fileUrl) _, _ = w.Write([]byte("success")) @@ -89,10 +89,12 @@ func (s *Server) cors(w http.ResponseWriter) { } func New(store storage.Storage, url string, logger log.Interface) http.Handler { + trimmedReturnUrl := strings.TrimSuffix(url, "/") + mux := &Server{ ServeMux: http.NewServeMux(), store: store, - returnUrl: url, + returnUrl: trimmedReturnUrl, logger: logger, now: time.Now, } diff --git a/server/server_test.go b/server/server_test.go index 0c02ef1..45a61f6 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -126,7 +126,7 @@ func TestServer(t *testing.T) { wantResp: "success", wantHeaders: map[string]string{ "Access-Control-Expose-Headers": "X-File-URL", - "X-File-URL": "https:/example.org/some/file", + "X-File-URL": "https://example.org/some/file", }, }, { @@ -175,7 +175,7 @@ func TestServer(t *testing.T) { args: args{ store: func(t *testing.T) StoreFunc { return func(path string, content io.Reader) error { - assert.Equal(t, "20211125/588b41ebf261820104615b83201c729bd16016d6e43649b28b0ef77d54ca5aaf8da0ce74ae3f20a4", path) + assert.Equal(t, "/20211125/588b41ebf261820104615b83201c729bd16016d6e43649b28b0ef77d54ca5aaf8da0ce74ae3f20a4", path) temp, err := ioutil.ReadAll(content) assert.NoError(t, err) assert.Equal(t, "abc", string(temp)) @@ -210,7 +210,7 @@ func TestServer(t *testing.T) { wantResp: "success", wantHeaders: map[string]string{ "Access-Control-Expose-Headers": "X-File-URL", - "X-File-URL": "https:/example.org/20211125/588b41ebf261820104615b83201c729bd16016d6e43649b28b0ef77d54ca5aaf8da0ce74ae3f20a4", + "X-File-URL": "https://example.org/20211125/588b41ebf261820104615b83201c729bd16016d6e43649b28b0ef77d54ca5aaf8da0ce74ae3f20a4", }, }, }