Merge branch 'return_url' into 'main'
chore: set correct return URL in header See merge request unboundsoftware/s3uploader!82
This commit was merged in pull request #86.
This commit is contained in:
+8
-6
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -29,7 +28,6 @@ func (s *Server) HandlePut(w http.ResponseWriter, req *http.Request) {
|
|||||||
s.cors(w)
|
s.cors(w)
|
||||||
case http.MethodPut:
|
case http.MethodPut:
|
||||||
path := strings.TrimPrefix(req.URL.Path, "/put")
|
path := strings.TrimPrefix(req.URL.Path, "/put")
|
||||||
s.logger.Infof("uploading to %s", path)
|
|
||||||
s.upload(path, w, req)
|
s.upload(path, w, req)
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
@@ -52,8 +50,7 @@ func (s *Server) HandleUpload(w http.ResponseWriter, req *http.Request) {
|
|||||||
day := now.Format("20060102")
|
day := now.Format("20060102")
|
||||||
prefixHash := hash(prefix)
|
prefixHash := hash(prefix)
|
||||||
dayHash := hash(now.Format(time.RFC3339Nano))
|
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:
|
default:
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = w.Write([]byte("This endpoint requires PUT"))
|
_, _ = 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) {
|
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)
|
err := s.store.Store(path, req.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).Error("error storing object in bucket")
|
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-Expose-Headers", "X-File-URL")
|
||||||
w.Header().Add("Access-Control-Allow-Origin", "*")
|
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)
|
w.Header().Add("X-File-URL", fileUrl)
|
||||||
s.logger.Infof("success - file is available at %s", fileUrl)
|
s.logger.Infof("success - file is available at %s", fileUrl)
|
||||||
_, _ = w.Write([]byte("success"))
|
_, _ = 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 {
|
func New(store storage.Storage, url string, logger log.Interface) http.Handler {
|
||||||
|
trimmedReturnUrl := strings.TrimSuffix(url, "/")
|
||||||
|
|
||||||
mux := &Server{
|
mux := &Server{
|
||||||
ServeMux: http.NewServeMux(),
|
ServeMux: http.NewServeMux(),
|
||||||
store: store,
|
store: store,
|
||||||
returnUrl: url,
|
returnUrl: trimmedReturnUrl,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
now: time.Now,
|
now: time.Now,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ func TestServer(t *testing.T) {
|
|||||||
wantResp: "success",
|
wantResp: "success",
|
||||||
wantHeaders: map[string]string{
|
wantHeaders: map[string]string{
|
||||||
"Access-Control-Expose-Headers": "X-File-URL",
|
"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{
|
args: args{
|
||||||
store: func(t *testing.T) StoreFunc {
|
store: func(t *testing.T) StoreFunc {
|
||||||
return func(path string, content io.Reader) error {
|
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)
|
temp, err := ioutil.ReadAll(content)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "abc", string(temp))
|
assert.Equal(t, "abc", string(temp))
|
||||||
@@ -210,7 +210,7 @@ func TestServer(t *testing.T) {
|
|||||||
wantResp: "success",
|
wantResp: "success",
|
||||||
wantHeaders: map[string]string{
|
wantHeaders: map[string]string{
|
||||||
"Access-Control-Expose-Headers": "X-File-URL",
|
"Access-Control-Expose-Headers": "X-File-URL",
|
||||||
"X-File-URL": "https:/example.org/20211125/588b41ebf261820104615b83201c729bd16016d6e43649b28b0ef77d54ca5aaf8da0ce74ae3f20a4",
|
"X-File-URL": "https://example.org/20211125/588b41ebf261820104615b83201c729bd16016d6e43649b28b0ef77d54ca5aaf8da0ce74ae3f20a4",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user