From 41762bfbb3ca6d69e5f84d935cc4977bc1b04c25 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Tue, 20 Oct 2020 17:12:05 +0200 Subject: [PATCH] Pass io.ReadSeeker to tus client instead of io.Reader --- .../grpc/services/gateway/storageprovider.go | 5 ++++- internal/http/services/owncloud/ocdav/put.go | 16 ++++++++++++++-- pkg/storage/utils/eosfs/upload.go | 7 ++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/grpc/services/gateway/storageprovider.go b/internal/grpc/services/gateway/storageprovider.go index c3ce9261993..6e437c65b79 100644 --- a/internal/grpc/services/gateway/storageprovider.go +++ b/internal/grpc/services/gateway/storageprovider.go @@ -48,7 +48,10 @@ type transferClaims struct { } func (s *svc) sign(_ context.Context, target string) (string, error) { - ttl := time.Duration(s.c.TransferExpires) * time.Second + // Tus sends a separate request to the datagateway service for every chunk. + // For large files, this can take a long time, so we extend the expiration + // for 10 minutes. TODO: Make this configureable. + ttl := time.Duration(s.c.TransferExpires) * 10 * time.Minute claims := transferClaims{ StandardClaims: jwt.StandardClaims{ ExpiresAt: time.Now().Add(ttl).Unix(), diff --git a/internal/http/services/owncloud/ocdav/put.go b/internal/http/services/owncloud/ocdav/put.go index 95093537319..c339f4c6e5d 100644 --- a/internal/http/services/owncloud/ocdav/put.go +++ b/internal/http/services/owncloud/ocdav/put.go @@ -21,6 +21,7 @@ package ocdav import ( "io" "net/http" + "os" "path" "regexp" "strconv" @@ -166,11 +167,22 @@ func (s *svc) handlePut(w http.ResponseWriter, r *http.Request, ns string) { return } } + fileName, fd, err := s.createChunkTempFile() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + defer os.RemoveAll(fileName) + defer fd.Close() + if _, err := io.Copy(fd, r.Body); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } - s.handlePutHelper(w, r, r.Body, fn, length) + s.handlePutHelper(w, r, fd, fn, length) } -func (s *svc) handlePutHelper(w http.ResponseWriter, r *http.Request, content io.Reader, fn string, length int64) { +func (s *svc) handlePutHelper(w http.ResponseWriter, r *http.Request, content io.ReadSeeker, fn string, length int64) { ctx := r.Context() log := appctx.GetLogger(ctx) diff --git a/pkg/storage/utils/eosfs/upload.go b/pkg/storage/utils/eosfs/upload.go index 283c70b09ea..39a6da9501e 100644 --- a/pkg/storage/utils/eosfs/upload.go +++ b/pkg/storage/utils/eosfs/upload.go @@ -236,7 +236,12 @@ func (upload *fileUpload) GetInfo(ctx context.Context) (tusd.FileInfo, error) { // GetReader returns an io.Reader for the upload func (upload *fileUpload) GetReader(ctx context.Context) (io.Reader, error) { - return os.Open(upload.binPath) + f, err := os.Open(upload.binPath) + if err != nil { + return nil, err + } + defer f.Close() + return f, nil } // WriteChunk writes the stream from the reader to the given offset of the upload