chore: set correct return URL in header

This commit is contained in:
2022-03-21 16:05:31 +01:00
parent 0aca62974c
commit 08e4868315
2 changed files with 11 additions and 9 deletions
+8 -6
View File
@@ -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,
}