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

Handle non-TUS uploads called directly with upload path #1314

Merged
merged 4 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog/unreleased/uploads-refactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ refactors that workflow to accept incoming requests following the tus protocol
while using simpler transmission internally.

https://github.com/cs3org/reva/pull/1285
https://github.com/cs3org/reva/pull/1314
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *svc) handleGet(w http.ResponseWriter, r *http.Request, ns string) {
w.Header().Set("ETag", info.Etag)
w.Header().Set("OC-FileId", wrapResourceID(info.Id))
w.Header().Set("OC-ETag", info.Etag)
t := utils.TSToTime(info.Mtime)
t := utils.TSToTime(info.Mtime).UTC()
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set("Last-Modified", lastModifiedString)
w.Header().Set("Content-Length", strconv.FormatUint(info.Size, 10))
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *svc) handleHead(w http.ResponseWriter, r *http.Request, ns string) {
w.Header().Set("ETag", info.Etag)
w.Header().Set("OC-FileId", wrapResourceID(info.Id))
w.Header().Set("OC-ETag", info.Etag)
t := utils.TSToTime(info.Mtime)
t := utils.TSToTime(info.Mtime).UTC()
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set("Last-Modified", lastModifiedString)
w.WriteHeader(http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (s *svc) handlePutHelper(w http.ResponseWriter, r *http.Request, content io
w.Header().Set("ETag", newInfo.Etag)
w.Header().Set("OC-FileId", wrapResourceID(newInfo.Id))
w.Header().Set("OC-ETag", newInfo.Etag)
t := utils.TSToTime(newInfo.Mtime)
t := utils.TSToTime(newInfo.Mtime).UTC()
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set("Last-Modified", lastModifiedString)

Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (s *svc) handleTusPost(w http.ResponseWriter, r *http.Request, ns string) {
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)
t := utils.TSToTime(info.Mtime).UTC()
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set("Last-Modified", lastModifiedString)
}
Expand Down
26 changes: 21 additions & 5 deletions pkg/storage/fs/ocis/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,26 @@ var defaultFilePerm = os.FileMode(0664)
func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) (err error) {
upload, err := fs.GetUpload(ctx, ref.GetPath())
if err != nil {
return errors.Wrap(err, "ocisfs: error retrieving upload")
// Upload corresponding to this ID was not found.
// Assume that this corresponds to the resource path to which the file has to be uploaded.

// Set the length to 0 and set SizeIsDeferred to true
metadata := map[string]string{"sizedeferred": "true"}
uploadID, err := fs.InitiateUpload(ctx, ref, 0, metadata)
if err != nil {
return err
}
if upload, err = fs.GetUpload(ctx, uploadID); err != nil {
return errors.Wrap(err, "ocisfs: error retrieving upload")
}
}

uploadInfo := upload.(*fileUpload)

p := uploadInfo.info.Storage["NodeName"]
ok, err := chunking.IsChunked(p)
if err != nil {
return errors.Wrap(err, "ocfs: error checking path")
return errors.Wrap(err, "ocisfs: error checking path")
}
if ok {
var assembledFile string
Expand All @@ -69,7 +80,7 @@ func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.Read
uploadInfo.info.Storage["NodeName"] = p
fd, err := os.Open(assembledFile)
if err != nil {
return errors.Wrap(err, "eos: error opening assembled file")
return errors.Wrap(err, "ocisfs: error opening assembled file")
}
defer fd.Close()
defer os.RemoveAll(assembledFile)
Expand Down Expand Up @@ -111,8 +122,13 @@ func (fs *ocisfs) InitiateUpload(ctx context.Context, ref *provider.Reference, u
Size: uploadLength,
}

if metadata != nil && metadata["mtime"] != "" {
info.MetaData["mtime"] = metadata["mtime"]
if metadata != nil {
if metadata["mtime"] != "" {
info.MetaData["mtime"] = metadata["mtime"]
}
if _, ok := metadata["sizedeferred"]; ok {
info.SizeIsDeferred = true
}
}

log.Debug().Interface("info", info).Interface("node", n).Interface("metadata", metadata).Msg("ocisfs: resolved filename")
Expand Down
24 changes: 20 additions & 4 deletions pkg/storage/fs/owncloud/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ var defaultFilePerm = os.FileMode(0664)
func (fs *ocfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) error {
upload, err := fs.GetUpload(ctx, ref.GetPath())
if err != nil {
return errors.Wrap(err, "ocfs: error retrieving upload")
// Upload corresponding to this ID was not found.
// Assume that this corresponds to the resource path to which the file has to be uploaded.

// Set the length to 0 and set SizeIsDeferred to true
metadata := map[string]string{"sizedeferred": "true"}
uploadID, err := fs.InitiateUpload(ctx, ref, 0, metadata)
if err != nil {
return err
}
if upload, err = fs.GetUpload(ctx, uploadID); err != nil {
return errors.Wrap(err, "ocfs: error retrieving upload")
}
}

uploadInfo := upload.(*fileUpload)
Expand All @@ -69,7 +80,7 @@ func (fs *ocfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCl
uploadInfo.info.Storage["InternalDestination"] = p
fd, err := os.Open(assembledFile)
if err != nil {
return errors.Wrap(err, "eos: error opening assembled file")
return errors.Wrap(err, "ocfs: error opening assembled file")
}
defer fd.Close()
defer os.RemoveAll(assembledFile)
Expand Down Expand Up @@ -103,8 +114,13 @@ func (fs *ocfs) InitiateUpload(ctx context.Context, ref *provider.Reference, upl
Size: uploadLength,
}

if metadata != nil && metadata["mtime"] != "" {
info.MetaData["mtime"] = metadata["mtime"]
if metadata != nil {
if metadata["mtime"] != "" {
info.MetaData["mtime"] = metadata["mtime"]
}
if _, ok := metadata["sizedeferred"]; ok {
info.SizeIsDeferred = true
}
}

upload, err := fs.NewUpload(ctx, info)
Expand Down
36 changes: 27 additions & 9 deletions pkg/storage/utils/localfs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,26 @@ var defaultFilePerm = os.FileMode(0664)
func (fs *localfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) error {
upload, err := fs.GetUpload(ctx, ref.GetPath())
if err != nil {
return errors.Wrap(err, "ocisfs: error retrieving upload")
// Upload corresponding to this ID was not found.
// Assume that this corresponds to the resource path to which the file has to be uploaded.

// Set the length to 0 and set SizeIsDeferred to true
metadata := map[string]string{"sizedeferred": "true"}
uploadID, err := fs.InitiateUpload(ctx, ref, 0, metadata)
if err != nil {
return err
}
if upload, err = fs.GetUpload(ctx, uploadID); err != nil {
return errors.Wrap(err, "localfs: error retrieving upload")
}
}

uploadInfo := upload.(*fileUpload)

p := uploadInfo.info.Storage["InternalDestination"]
ok, err := chunking.IsChunked(p)
if err != nil {
return errors.Wrap(err, "ocfs: error checking path")
return errors.Wrap(err, "localfs: error checking path")
}
if ok {
var assembledFile string
Expand All @@ -60,22 +71,22 @@ func (fs *localfs) Upload(ctx context.Context, ref *provider.Reference, r io.Rea
}
if p == "" {
if err = uploadInfo.Terminate(ctx); err != nil {
return errors.Wrap(err, "ocfs: error removing auxiliary files")
return errors.Wrap(err, "localfs: error removing auxiliary files")
}
return errtypes.PartialContent(ref.String())
}
uploadInfo.info.Storage["InternalDestination"] = p
fd, err := os.Open(assembledFile)
if err != nil {
return errors.Wrap(err, "eos: error opening assembled file")
return errors.Wrap(err, "localfs: error opening assembled file")
}
defer fd.Close()
defer os.RemoveAll(assembledFile)
r = fd
}

if _, err := uploadInfo.WriteChunk(ctx, 0, r); err != nil {
return errors.Wrap(err, "ocisfs: error writing to binary file")
return errors.Wrap(err, "localfs: error writing to binary file")
}

return uploadInfo.FinishUpload(ctx)
Expand All @@ -101,8 +112,13 @@ func (fs *localfs) InitiateUpload(ctx context.Context, ref *provider.Reference,
Size: uploadLength,
}

if metadata != nil && metadata["mtime"] != "" {
info.MetaData["mtime"] = metadata["mtime"]
if metadata != nil {
if metadata["mtime"] != "" {
info.MetaData["mtime"] = metadata["mtime"]
}
if _, ok := metadata["sizedeferred"]; ok {
info.SizeIsDeferred = true
}
}

upload, err := fs.NewUpload(ctx, info)
Expand Down Expand Up @@ -331,8 +347,10 @@ func (upload *fileUpload) FinishUpload(ctx context.Context) error {

// only delete the upload if it was successfully written to the fs
if err := os.Remove(upload.infoPath); err != nil {
log := appctx.GetLogger(ctx)
log.Err(err).Interface("info", upload.info).Msg("localfs: could not delete upload info")
if !os.IsNotExist(err) {
log := appctx.GetLogger(ctx)
log.Err(err).Interface("info", upload.info).Msg("localfs: could not delete upload info")
}
}

// TODO: set mtime if specified in metadata
Expand Down