Skip to content

Commit

Permalink
handle 0 sized files while updating
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Jul 8, 2024
1 parent e8791e2 commit edaf667
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend/teldrive/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type UpdateFileInformation struct {
CreatedAt string `json:"createdAt,omitempty"`
Parts []FilePart `json:"parts,omitempty"`
Size int64 `json:"size,omitempty"`
UploadId int64 `json:"uploadId,omitempty"`
UploadId string `json:"uploadId,omitempty"`
}

// RemoveFileRequest is used for deleting a file
Expand Down
45 changes: 28 additions & 17 deletions backend/teldrive/teldrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,21 +564,32 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op

modTime := src.ModTime(ctx)

uploadInfo, err := o.uploadMultipart(ctx, bufio.NewReader(in), src)
var (
uploadInfo *uploadInfo
err error
)

if err != nil {
return err
if src.Size() > 0 {
uploadInfo, err = o.uploadMultipart(ctx, bufio.NewReader(in), src)
if err != nil {
return err
}
}

payload := &api.UpdateFileInformation{
UpdatedAt: modTime.UTC().Format(timeFormat),
Parts: uploadInfo.fileChunks,
Size: src.Size(),
}

if uploadInfo != nil {
payload.Parts = uploadInfo.fileChunks
payload.UploadId = uploadInfo.uploadID
}

opts := rest.Opts{
Method: "PUT",
Path: "/api/files/" + o.id + "/parts",
Method: "PUT",
Path: "/api/files/" + o.id + "/parts",
NoResponse: true,
}

err = o.fs.pacer.Call(func() (bool, error) {
Expand Down Expand Up @@ -643,10 +654,10 @@ func (f *Fs) OpenChunkWriter(
func (f *Fs) CreateDir(ctx context.Context, base string, leaf string) (err error) {

var resp *http.Response
var apiErr api.Error
opts := rest.Opts{
Method: "POST",
Path: "/api/files/directories",
Method: "POST",
Path: "/api/files/directories",
NoResponse: true,
}

dir := base
Expand All @@ -663,7 +674,7 @@ func (f *Fs) CreateDir(ctx context.Context, base string, leaf string) (err error
Path: f.opt.Enc.FromStandardPath(dir),
}
err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.CallJSON(ctx, &opts, &mkdir, &apiErr)
resp, err = f.srv.CallJSON(ctx, &opts, &mkdir, nil)
return shouldRetry(ctx, resp, err)
})
if err != nil {
Expand All @@ -688,8 +699,9 @@ func (f *Fs) Mkdir(ctx context.Context, dir string) (err error) {
func (f *Fs) Rmdir(ctx context.Context, dir string) (err error) {
var resp *http.Response
opts := rest.Opts{
Method: "POST",
Path: "/api/files/delete",
Method: "POST",
Path: "/api/files/delete",
NoResponse: true,
}
rm := api.RemoveFileRequest{
Source: f.dirPath(dir),
Expand Down Expand Up @@ -771,8 +783,9 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
srcPath := srcFs.dirPath(srcRemote)

opts := rest.Opts{
Method: "POST",
Path: "/api/files/directories/move",
Method: "POST",
Path: "/api/files/directories/move",
NoResponse: true,
}
move := api.DirMove{
Source: srcPath,
Expand Down Expand Up @@ -938,10 +951,8 @@ func (o *Object) Storable() bool {

// SetModTime sets the modification time of the local fs object
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
time := modTime.UTC().Format(timeFormat)
updateInfo := &api.UpdateFileInformation{
UpdatedAt: time,
CreatedAt: time,
UpdatedAt: modTime.UTC().Format(timeFormat),
}
err := o.fs.updateFileInformation(ctx, updateInfo, o.id)
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions backend/teldrive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func (w *objectChunkWriter) WriteChunk(ctx context.Context, chunkNumber int, rea
}

resp, err := w.f.srv.CallJSON(ctx, &opts, nil, &response)

retry, err := shouldRetry(ctx, resp, err)

if err != nil {
fs.Debugf(w.o, "Error sending chunk %d (retry=%v): %v: %#v", chunkNumber, retry, err, err)
}
Expand Down Expand Up @@ -344,8 +346,9 @@ func (o *Object) createFile(ctx context.Context, src fs.ObjectInfo, uploadInfo *
}
}
opts := rest.Opts{
Method: "POST",
Path: "/api/files",
Method: "POST",
Path: "/api/files",
NoResponse: true,
}

payload := api.CreateFileRequest{
Expand All @@ -369,8 +372,9 @@ func (o *Object) createFile(ctx context.Context, src fs.ObjectInfo, uploadInfo *
}
if src.Size() > 0 {
opts = rest.Opts{
Method: "DELETE",
Path: "/api/uploads/" + uploadInfo.uploadID,
Method: "DELETE",
Path: "/api/uploads/" + uploadInfo.uploadID,
NoResponse: true,
}

err = o.fs.pacer.Call(func() (bool, error) {
Expand Down

0 comments on commit edaf667

Please sign in to comment.