1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-29 11:34:07 +02:00

Make BrowserUtils.DownloadFileAsync return WebClient and delete the file when cancelled

This commit is contained in:
chylex 2017-05-17 17:25:44 +02:00
parent b61479f84f
commit 9e44a86be0

View File

@ -69,20 +69,28 @@ public static string GetErrorName(CefErrorCode code){
return ConvertPascalCaseToScreamingSnakeCase(Enum.GetName(typeof(CefErrorCode), code) ?? string.Empty); return ConvertPascalCaseToScreamingSnakeCase(Enum.GetName(typeof(CefErrorCode), code) ?? string.Empty);
} }
public static void DownloadFileAsync(string url, string target, Action onSuccess, Action<Exception> onFailure){ public static WebClient DownloadFileAsync(string url, string target, Action onSuccess, Action<Exception> onFailure){
WebClient client = new WebClient{ Proxy = null }; WebClient client = new WebClient{ Proxy = null };
client.Headers[HttpRequestHeader.UserAgent] = HeaderUserAgent; client.Headers[HttpRequestHeader.UserAgent] = HeaderUserAgent;
client.DownloadFileCompleted += (sender, args) => { client.DownloadFileCompleted += (sender, args) => {
if (args.Error == null){ if (args.Cancelled){
onSuccess?.Invoke(); try{
File.Delete(target);
}catch{
// didn't want it deleted anyways
}
}
else if (args.Error != null){
onFailure?.Invoke(args.Error);
} }
else{ else{
onFailure?.Invoke(args.Error); onSuccess?.Invoke();
} }
}; };
client.DownloadFileAsync(new Uri(url), target); client.DownloadFileAsync(new Uri(url), target);
return client;
} }
public static void SetZoomLevel(IBrowser browser, int percentage){ public static void SetZoomLevel(IBrowser browser, int percentage){