diff --git a/backend/webdav/chunking.go b/backend/webdav/chunking.go index 36daab450..4660e9493 100644 --- a/backend/webdav/chunking.go +++ b/backend/webdav/chunking.go @@ -14,7 +14,6 @@ import ( "io" "net/http" "path" - "strings" "github.com/rclone/rclone/fs" "github.com/rclone/rclone/lib/readers" @@ -41,10 +40,6 @@ func (f *Fs) setUploadChunkSize(cs fs.SizeSuffix) (old fs.SizeSuffix, err error) return } -func (f *Fs) getChunksUploadURL() string { - return strings.Replace(f.endpointURL, "/dav/files/", "/dav/uploads/", 1) -} - func (o *Object) getChunksUploadDir() (string, error) { hasher := md5.New() _, err := hasher.Write([]byte(o.filePath())) @@ -55,12 +50,16 @@ func (o *Object) getChunksUploadDir() (string, error) { return uploadDir, nil } -func (f *Fs) verifyChunkConfig() error { - if f.opt.ChunkSize != 0 && !validateNextCloudChunkedURL.MatchString(f.endpointURL) { - return errors.New("chunked upload with nextcloud must use /dav/files/USER endpoint not /webdav") +func (f *Fs) getChunksUploadURL() (string, error) { + submatch := nextCloudURLRegex.FindStringSubmatch(f.endpointURL) + if submatch == nil { + return "", errors.New("the remote url looks incorrect. Note that nextcloud chunked uploads require you to use the /dav/files/USER endpoint instead of /webdav") } - return nil + baseURL, user := submatch[1], submatch[2] + chunksUploadURL := fmt.Sprintf("%s/dav/uploads/%s/", baseURL, user) + + return chunksUploadURL, nil } func (o *Object) shouldUseChunkedUpload(src fs.ObjectInfo) bool { diff --git a/backend/webdav/webdav.go b/backend/webdav/webdav.go index 397cd0b92..ed562bb98 100644 --- a/backend/webdav/webdav.go +++ b/backend/webdav/webdav.go @@ -569,7 +569,8 @@ func (f *Fs) fetchAndSetBearerToken() error { return nil } -var validateNextCloudChunkedURL = regexp.MustCompile(`^.*/dav/files/[^/]+/?$`) +// The WebDAV url can optionally be suffixed with a path. This suffix needs to be ignored for determining the temporary upload directory of chunks. +var nextCloudURLRegex = regexp.MustCompile(`^(.*)/dav/files/([^/]+)`) // setQuirks adjusts the Fs for the vendor passed in func (f *Fs) setQuirks(ctx context.Context, vendor string) error { @@ -592,11 +593,18 @@ func (f *Fs) setQuirks(ctx context.Context, vendor string) error { f.propsetMtime = true f.hasOCSHA1 = true f.canChunk = true - if err := f.verifyChunkConfig(); err != nil { - return err + + if f.opt.ChunkSize == 0 { + fs.Logf(nil, "Chunked uploads are disabled because nextcloud_chunk_size is set to 0") + } else { + chunksUploadURL, err := f.getChunksUploadURL() + if err != nil { + return err + } + + f.chunksUploadURL = chunksUploadURL + fs.Logf(nil, "Chunks temporary upload directory: %s", f.chunksUploadURL) } - f.chunksUploadURL = f.getChunksUploadURL() - fs.Logf(nil, "Chunks temporary upload directory: %s", f.chunksUploadURL) case "sharepoint": // To mount sharepoint, two Cookies are required // They have to be set instead of BasicAuth