diff --git a/backend/webdav/webdav.go b/backend/webdav/webdav.go index e441b06a5..628358cd4 100644 --- a/backend/webdav/webdav.go +++ b/backend/webdav/webdav.go @@ -86,6 +86,15 @@ func init() { Name: "bearer_token_command", Help: "Command to run to get a bearer token", Advanced: true, + }, { + Name: "delete_on_error", + Default: false, + Help: `Delete a partially uploaded file on upload failure. + +Some webdav backends (eg rclone serve webdav) leave behind half +written files on error. This flag causes them to be deleted if the +upload fails part of the way through.`, + Advanced: true, }}, }) } @@ -98,6 +107,7 @@ type Options struct { Pass string `config:"pass"` BearerToken string `config:"bearer_token"` BearerTokenCommand string `config:"bearer_token_command"` + DeleteOnError bool `config:"delete_on_error"` } // Fs represents a remote webdav @@ -1193,14 +1203,16 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op return o.fs.shouldRetry(resp, err) }) if err != nil { - // Give the WebDAV server a chance to get its internal state in order after the - // error. The error may have been local in which case we closed the connection. - // The server may still be dealing with it for a moment. A sleep isn't ideal but I - // haven't been able to think of a better method to find out if the server has - // finished - ncw - time.Sleep(1 * time.Second) - // Remove failed upload - _ = o.Remove(ctx) + if o.fs.opt.DeleteOnError { + // Give the WebDAV server a chance to get its internal state in order after the + // error. The error may have been local in which case we closed the connection. + // The server may still be dealing with it for a moment. A sleep isn't ideal but I + // haven't been able to think of a better method to find out if the server has + // finished - ncw + time.Sleep(1 * time.Second) + // Remove failed upload + _ = o.Remove(ctx) + } return err } // read metadata from remote diff --git a/cmd/serve/webdav/webdav_test.go b/cmd/serve/webdav/webdav_test.go index cef20546d..74f633a83 100644 --- a/cmd/serve/webdav/webdav_test.go +++ b/cmd/serve/webdav/webdav_test.go @@ -62,11 +62,12 @@ func TestWebDav(t *testing.T) { // Config for the backend we'll use to connect to the server config := configmap.Simple{ - "type": "webdav", - "vendor": "other", - "url": w.Server.URL(), - "user": testUser, - "pass": obscure.MustObscure(testPass), + "type": "webdav", + "vendor": "other", + "url": w.Server.URL(), + "user": testUser, + "pass": obscure.MustObscure(testPass), + "delete_on_error": "true", } return config, func() {