-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
889 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,6 +204,8 @@ public static int LoadConfig(ref Config? config) | |
}; | ||
} | ||
|
||
config.webDavItem ??= new(); | ||
|
||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using System.Net; | ||
using WebDav; | ||
|
||
namespace ServiceLib.Handler | ||
{ | ||
public sealed class WebDavHandler | ||
{ | ||
private static readonly Lazy<WebDavHandler> _instance = new(() => new()); | ||
public static WebDavHandler Instance => _instance.Value; | ||
|
||
private Config? _config; | ||
private WebDavClient? _client; | ||
private string? _lastDescription; | ||
private string _webDir = "mywebdir"; | ||
private string _webFileName = "backup.zip"; | ||
private string _logTitle = "WebDav--"; | ||
|
||
public WebDavHandler() | ||
{ | ||
_config = LazyConfig.Instance.Config; | ||
} | ||
|
||
private async Task<bool> GetClient() | ||
{ | ||
try | ||
{ | ||
if (_config.webDavItem.url.IsNullOrEmpty() | ||
|| _config.webDavItem.userName.IsNullOrEmpty() | ||
|| _config.webDavItem.password.IsNullOrEmpty()) | ||
{ | ||
throw new ArgumentException("webdav parameter error or null"); | ||
} | ||
if (_client != null) | ||
{ | ||
_client?.Dispose(); | ||
_client = null; | ||
} | ||
|
||
var clientParams = new WebDavClientParams | ||
{ | ||
BaseAddress = new Uri(_config.webDavItem.url), | ||
Credentials = new NetworkCredential(_config.webDavItem.userName, _config.webDavItem.password) | ||
}; | ||
_client = new WebDavClient(clientParams); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SaveLog(ex); | ||
return false; | ||
} | ||
return await Task.FromResult(true); | ||
} | ||
|
||
private async Task<bool> CheckProp() | ||
{ | ||
if (_client is null) return false; | ||
try | ||
{ | ||
var result = await _client.Propfind(_webDir); | ||
if (result.IsSuccessful) | ||
{ | ||
return true; | ||
} | ||
var result2 = await _client.Mkcol(_webDir); // create a directory | ||
if (result2.IsSuccessful) | ||
{ | ||
return true; | ||
} | ||
SaveLog(result2.Description); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SaveLog(ex); | ||
} | ||
return false; | ||
} | ||
|
||
private void SaveLog(string desc) | ||
{ | ||
_lastDescription = desc; | ||
Logging.SaveLog(_logTitle + desc); | ||
} | ||
|
||
private void SaveLog(Exception ex) | ||
{ | ||
_lastDescription = ex.Message; | ||
Logging.SaveLog(_logTitle, ex); | ||
} | ||
|
||
public async Task<bool> CheckConnection() | ||
{ | ||
if (await GetClient() == false) | ||
{ | ||
return false; | ||
} | ||
if (await CheckProp() == false) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public async Task<bool> PutFile(string fileName) | ||
{ | ||
if (await GetClient() == false) | ||
{ | ||
return false; | ||
} | ||
if (await CheckProp() == false) | ||
{ | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
using var fs = File.OpenRead(fileName); | ||
var result = await _client.PutFile($"{_webDir}/{_webFileName}", fs); // upload a resource | ||
if (result.IsSuccessful) | ||
{ | ||
return true; | ||
} | ||
|
||
SaveLog(result.Description); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SaveLog(ex); | ||
} | ||
return false; | ||
} | ||
|
||
public async Task<bool> GetRawFile(string fileName) | ||
{ | ||
if (await GetClient() == false) | ||
{ | ||
return false; | ||
} | ||
if (await CheckProp() == false) | ||
{ | ||
return false; | ||
} | ||
try | ||
{ | ||
var response = await _client.GetRawFile($"{_webDir}/{_webFileName}"); | ||
if (!response.IsSuccessful) | ||
{ | ||
SaveLog(response.Description); | ||
} | ||
using var outputFileStream = new FileStream(fileName, FileMode.Create); | ||
response.Stream.CopyTo(outputFileStream); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
SaveLog(ex); | ||
} | ||
return false; | ||
} | ||
|
||
public string GetLastError() => _lastDescription ?? string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.