Skip to content

Commit

Permalink
Refactor methods and improve Propfind result filtering
Browse files Browse the repository at this point in the history
Refactored `ConcatUris` to use generic parameter names and updated error messages accordingly. Enhanced `Propfind` result processing to exclude resources with `DisplayName` values of `".."` and `"."`. Improved readability of `GetFileBytesAsync` by removing unnecessary `else` block.
  • Loading branch information
bvdcode committed Oct 21, 2024
1 parent 0c33c7f commit 4eafc78
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions Sources/EasyExtensions.WebDav/WebDavCloudClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ public WebDavCloudClient(string server, string username, string password, string
_server = server;
}

private Uri ConcatUris(string server, string baseAddress)
private Uri ConcatUris(string part1, string part2)
{
if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(baseAddress))
if (string.IsNullOrEmpty(part1) || string.IsNullOrEmpty(part2))
{
throw new ArgumentException("Server and base address must not be null or empty.");
throw new ArgumentException("Path parts must not be null or empty.");
}
if (server.EndsWith('/') && baseAddress.StartsWith('/'))
if (part1.EndsWith('/') && part2.StartsWith('/'))
{
return new Uri(server + baseAddress[1..]);
return new Uri(part1 + part2[1..]);
}
if (!server.EndsWith('/') && !baseAddress.StartsWith('/'))
if (!part1.EndsWith('/') && !part2.StartsWith('/'))
{
return new Uri(server + '/' + baseAddress);
return new Uri(part1 + '/' + part2);
}
return new Uri(server + baseAddress);
return new Uri(part1 + part2);
}

/// <summary>
Expand Down Expand Up @@ -173,7 +173,10 @@ public async Task<IEnumerable<WebDavResource>> GetResourcesAsync(string folder)
{
return Array.Empty<WebDavResource>();
}
return result.Resources.Where(x => x.Uri != url);
return result.Resources.Where(x =>
x.Uri != url &&
x.DisplayName != ".." &&
x.DisplayName != ".");
}

/// <summary>
Expand All @@ -182,20 +185,17 @@ public async Task<IEnumerable<WebDavResource>> GetResourcesAsync(string folder)
/// <param name="filePath"> The file path. </param>
/// <returns> The file bytes. </returns>
public async Task<byte[]> GetFileBytesAsync(string filePath)
{
if (!await ExistsAsync(filePath))
{
throw new FileNotFoundException("File not found.", filePath);
}
else
{
if (!await ExistsAsync(filePath))
{
throw new FileNotFoundException("File not found.", filePath);
}
string requestUri = ConcatUris(_baseAddress, filePath).ToString();
WebDavStreamResponse webDavStreamResponse = await _client.GetRawFile(requestUri);
using MemoryStream memoryStream = new MemoryStream();
await webDavStreamResponse.Stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}

/// <summary>
/// Gets the underlying <see cref="WebDavClient"/>.
Expand Down

0 comments on commit 4eafc78

Please sign in to comment.