Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove encoding and add CORS for upload/download URL handlers. #710

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions pkg/cloud/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -319,11 +318,13 @@ func (r *LocalStorageService) PreSignUrl(ctx context.Context, req *storagepb.Sto

var address string = ""

// XXX: Do not URL encode keys (path needs to be preserved)
// TODO: May need to re-write slashes to a non-escapable character format
switch req.Operation {
case storagepb.StoragePreSignUrlRequest_WRITE:
address = fmt.Sprintf("http://localhost:%d/write/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, url.PathEscape(req.Key))
address = fmt.Sprintf("http://localhost:%d/write/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, req.Key)
case storagepb.StoragePreSignUrlRequest_READ:
address = fmt.Sprintf("http://localhost:%d/read/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, url.PathEscape(req.Key))
address = fmt.Sprintf("http://localhost:%d/read/%s/%s", r.storageListener.Addr().(*net.TCPAddr).Port, req.BucketName, req.Key)
}

if address == "" {
Expand All @@ -340,6 +341,21 @@ type StorageOptions struct {
SecretKey string
}

func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, PUT")
w.Header().Set("Access-Control-Allow-Headers", "*")

if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}

next.ServeHTTP(w, r)
})
}

func NewLocalStorageService(opts StorageOptions) (*LocalStorageService, error) {
var err error

Expand All @@ -354,8 +370,9 @@ func NewLocalStorageService(opts StorageOptions) (*LocalStorageService, error) {
}

router := mux.NewRouter()
router.Use(corsMiddleware)

router.HandleFunc("/read/{bucket}/{file}", func(w http.ResponseWriter, r *http.Request) {
router.HandleFunc("/read/{bucket}/{file:.*}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
file := vars["file"]
Expand All @@ -379,7 +396,7 @@ func NewLocalStorageService(opts StorageOptions) (*LocalStorageService, error) {
}
})

router.HandleFunc("/write/{bucket}/{file}", func(w http.ResponseWriter, r *http.Request) {
router.HandleFunc("/write/{bucket}/{file:.*}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
file := vars["file"]
Expand Down
Loading