Skip to content

Commit

Permalink
[TUS] Return metadata headers after direct upload (cs3org#813)
Browse files Browse the repository at this point in the history
When uploading the full file using POST with the TUS extension
"creation-with-upload", the fileid, etag and mtime are now returned
through headers.
  • Loading branch information
Vincent Petry authored and David Christofas committed Jun 29, 2020
1 parent 82198c1 commit 1e83adb
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/http/services/owncloud/ocdav/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ package ocdav
import (
"net/http"
"path"
"time"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/internal/http/utils"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rhttp"
tusd "github.com/tus/tusd/pkg/handler"
Expand Down Expand Up @@ -177,6 +179,39 @@ func (s *svc) handleTusPost(w http.ResponseWriter, r *http.Request, ns string) {
w.WriteHeader(httpRes.StatusCode)
return
}

// check if upload was fully completed
if httpRes.Header.Get("Upload-Offset") == r.Header.Get("Upload-Length") {
// get uploaded file metadata
sRes, err := client.Stat(ctx, sReq)
if err != nil {
log.Error().Err(err).Msg("error sending grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return
}

if sRes.Status.Code != rpc.Code_CODE_OK {
if sRes.Status.Code != rpc.Code_CODE_NOT_FOUND {
w.WriteHeader(http.StatusInternalServerError)
return
}
}

info := sRes.Info
if info == nil {
log.Error().Str("fn", fn).Msg("No info found for uploaded file")
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", info.MimeType)
w.Header().Set("OC-FileId", wrapResourceID(info.Id))
w.Header().Set("OC-ETag", info.Etag)
w.Header().Set("ETag", info.Etag)
t := utils.TSToTime(info.Mtime)
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set("Last-Modified", lastModifiedString)
}
}
w.WriteHeader(http.StatusCreated)
}

0 comments on commit 1e83adb

Please sign in to comment.