diff --git a/src/SharpSync/Storage/WebDavStorage.cs b/src/SharpSync/Storage/WebDavStorage.cs index 40b9dff..7b5e3c3 100644 --- a/src/SharpSync/Storage/WebDavStorage.cs +++ b/src/SharpSync/Storage/WebDavStorage.cs @@ -930,14 +930,42 @@ private string GetFullPath(string relativePath) { } private string GetRelativePath(string fullUrl) { - var prefix = string.IsNullOrEmpty(RootPath) ? _baseUrl : $"{_baseUrl}/{RootPath}"; - - if (fullUrl.StartsWith(prefix)) { - var relativePath = fullUrl.Substring(prefix.Length).Trim('/'); - return string.IsNullOrEmpty(relativePath) ? "/" : relativePath; + // The fullUrl can be either a full URL (http://server/path) or just a path (/path) + // We need to strip the base URL and RootPath to get the relative path + + // Extract the path portion if it's a full URL + string path; + if (Uri.TryCreate(fullUrl, UriKind.Absolute, out var uri)) { + // It's a full URL - get the path component and decode it + path = Uri.UnescapeDataString(uri.AbsolutePath); + } else { + // It's already a path + path = fullUrl; } - - return fullUrl; + + // Remove leading slash for consistency + path = path.TrimStart('/'); + + // If there's no root path, return the path as-is (trimming trailing slashes) + if (string.IsNullOrEmpty(RootPath)) { + return path.TrimEnd('/'); + } + + // Normalize the root path (no leading/trailing slashes) + var normalizedRoot = RootPath.Trim('/'); + + // The path should start with RootPath/ + if (path.StartsWith($"{normalizedRoot}/")) { + return path.Substring(normalizedRoot.Length + 1).TrimEnd('/'); + } + + // If it's exactly the root path itself (directory listing) + if (path == normalizedRoot || path == $"{normalizedRoot}/") { + return ""; + } + + // Otherwise return as-is (trim trailing slashes) + return path.TrimEnd('/'); } private bool _rootPathCreated; diff --git a/tests/SharpSync.Tests/Storage/WebDavStorageTests.cs b/tests/SharpSync.Tests/Storage/WebDavStorageTests.cs index 9a10642..95d2ef7 100644 --- a/tests/SharpSync.Tests/Storage/WebDavStorageTests.cs +++ b/tests/SharpSync.Tests/Storage/WebDavStorageTests.cs @@ -606,11 +606,6 @@ public async Task ListItemsAsync_WithFiles_ReturnsAllItems() { await Task.Delay(100); } - // Debug output - foreach (var item in items!) { - System.Diagnostics.Debug.WriteLine($"Found item: {item.Path}, IsDirectory: {item.IsDirectory}"); - } - // Assert Assert.Equal(3, items.Count); Assert.Contains(items, i => i.Path.EndsWith("file1.txt") && !i.IsDirectory);