wasDAVClient

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 4  →  ?path2? @ 5
/wasDAVClient/Client.cs
@@ -1,4 +1,11 @@
using System;
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -47,8 +54,7 @@
private string _encodedBasePath;
private string _server;
 
 
public Client(NetworkCredential credential = null)
public Client(ICredentials credential = null)
{
var handler = new HttpClientHandler();
 
@@ -64,12 +70,10 @@
handler.PreAuthenticate = true;
}
 
_client = new HttpClient(handler);
_client.Timeout = TimeSpan.FromSeconds(Timeout);
_client = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(Timeout)};
_client.DefaultRequestHeaders.ExpectContinue = false;
 
_uploadClient = new HttpClient(handler);
_uploadClient.Timeout = TimeSpan.FromSeconds(Timeout);
_uploadClient = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(Timeout)};
_uploadClient.DefaultRequestHeaders.ExpectContinue = false;
}
 
@@ -81,11 +85,7 @@
public string Server
{
get { return _server; }
set
{
value = value.TrimEnd('/');
_server = value;
}
set { _server = value.TrimEnd('/'); }
}
 
/// <summary>
@@ -171,31 +171,29 @@
 
var listUrl = listUri.ToString();
 
var result = new List<Item>(items.Count());
foreach (var item in items)
return items.AsParallel().Select(async item =>
{
// If it's not a collection, add it to the result
if (!item.IsCollection)
switch (!item.IsCollection)
{
result.Add(item);
case true:
return item;
default:
// If it's not the requested parent folder, add it to the result
if (!string.Equals((await GetServerUrl(item.Href, true).ConfigureAwait(false)).ToString(),
listUrl, StringComparison.CurrentCultureIgnoreCase))
{
return item;
}
break;
}
else
{
// If it's not the requested parent folder, add it to the result
var fullHref = await GetServerUrl(item.Href, true).ConfigureAwait(false);
if (!string.Equals(fullHref.ToString(), listUrl, StringComparison.CurrentCultureIgnoreCase))
{
result.Add(item);
}
}
}
return result;
 
return null;
}).Select(o => o.Result).OfType<Item>();
}
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
@@ -205,8 +203,7 @@
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
public async Task<Item> GetFolder(string path = "/")
{
var listUri = await GetServerUrl(path, true).ConfigureAwait(false);
return await Get(listUri.Uri, path).ConfigureAwait(false);
return await Get((await GetServerUrl(path, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
/// <summary>
@@ -215,8 +212,7 @@
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
public async Task<Item> GetFile(string path = "/")
{
var listUri = await GetServerUrl(path, false).ConfigureAwait(false);
return await Get(listUri.Uri, path).ConfigureAwait(false);
return await Get((await GetServerUrl(path, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
 
@@ -224,13 +220,12 @@
/// List all files present on the server.
/// </summary>
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
private async Task<Item> Get(Uri listUri, string path)
private async Task<Item> Get(Uri listUri)
{
// Depth header: http://webdav.org/specs/rfc4918.html#rfc.section.9.1.4
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Depth", "0");
 
 
HttpResponseMessage response = null;
 
try
@@ -244,7 +239,7 @@
(int) response.StatusCode != HttpStatusCode_MultiStatus)
{
throw new wasDAVException((int) response.StatusCode,
string.Format("Failed retrieving item/folder (Status Code: {0})", response.StatusCode));
$"Failed retrieving item/folder (Status Code: {response.StatusCode})");
}
 
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
@@ -261,8 +256,7 @@
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
@@ -313,8 +307,7 @@
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
@@ -322,8 +315,8 @@
/// <summary>
/// Create a directory on the server
/// </summary>
/// <param name="remotePath">Destination path of the directory on the server</param>
/// <param name="name"></param>
/// <param name="remotePath">Destination path of the directory on the server.</param>
/// <param name="name">The name of the folder to create.</param>
public async Task<bool> CreateDir(string remotePath, string name)
{
// Should not have a trailing slash.
@@ -350,21 +343,18 @@
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
public async Task DeleteFolder(string href)
{
var listUri = await GetServerUrl(href, true).ConfigureAwait(false);
await Delete(listUri.Uri).ConfigureAwait(false);
await Delete((await GetServerUrl(href, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
public async Task DeleteFile(string href)
{
var listUri = await GetServerUrl(href, false).ConfigureAwait(false);
await Delete(listUri.Uri).ConfigureAwait(false);
await Delete((await GetServerUrl(href, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
 
@@ -382,19 +372,19 @@
public async Task<bool> MoveFolder(string srcFolderPath, string dstFolderPath)
{
// Should have a trailing slash.
var srcUri = await GetServerUrl(srcFolderPath, true).ConfigureAwait(false);
var dstUri = await GetServerUrl(dstFolderPath, true).ConfigureAwait(false);
 
return await Move(srcUri.Uri, dstUri.Uri).ConfigureAwait(false);
return
await
Move((await GetServerUrl(srcFolderPath, true).ConfigureAwait(false)).Uri,
(await GetServerUrl(dstFolderPath, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
public async Task<bool> MoveFile(string srcFilePath, string dstFilePath)
{
// Should not have a trailing slash.
var srcUri = await GetServerUrl(srcFilePath, false).ConfigureAwait(false);
var dstUri = await GetServerUrl(dstFilePath, false).ConfigureAwait(false);
 
return await Move(srcUri.Uri, dstUri.Uri).ConfigureAwait(false);
return
await
Move((await GetServerUrl(srcFilePath, false).ConfigureAwait(false)).Uri,
(await GetServerUrl(dstFilePath, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
 
@@ -402,8 +392,10 @@
{
const string requestContent = "MOVE";
 
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Destination", dstUri.ToString());
IDictionary<string, string> headers = new Dictionary<string, string>
{
{"Destination", dstUri.ToString()}
};
 
var response =
await
@@ -436,10 +428,9 @@
using (var request = new HttpRequestMessage(method, uri))
{
request.Headers.Connection.Add("Keep-Alive");
if (!string.IsNullOrWhiteSpace(UserAgent))
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, UserAgentVersion));
else
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
request.Headers.UserAgent.Add(!string.IsNullOrWhiteSpace(UserAgent)
? new ProductInfoHeaderValue(UserAgent, UserAgentVersion)
: new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
 
if (headers != null)
{
@@ -473,10 +464,9 @@
using (var request = new HttpRequestMessage(method, uri))
{
request.Headers.Connection.Add("Keep-Alive");
if (!string.IsNullOrWhiteSpace(UserAgent))
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, UserAgentVersion));
else
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
request.Headers.UserAgent.Add(!string.IsNullOrWhiteSpace(UserAgent)
? new ProductInfoHeaderValue(UserAgent, UserAgentVersion)
: new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
 
if (headers != null)
{
@@ -492,8 +482,7 @@
request.Content = new StreamContent(content);
}
 
var client = _uploadClient ?? _client;
return await client.SendAsync(request).ConfigureAwait(false);
return await (_uploadClient ?? _client).SendAsync(request).ConfigureAwait(false);
}
}
 
@@ -518,7 +507,7 @@
if (_encodedBasePath == null)
{
var baseUri = new UriBuilder(_server) {Path = _basePath};
var root = await Get(baseUri.Uri, null).ConfigureAwait(false);
var root = await Get(baseUri.Uri).ConfigureAwait(false);
 
_encodedBasePath = root.Href;
}
@@ -582,10 +571,8 @@
 
public void Dispose()
{
if(_client != null)
_client.Dispose();
if (_uploadClient != null)
_uploadClient.Dispose();
_client?.Dispose();
_uploadClient?.Dispose();
}
 
#endregion