mirror of https://github.com/2dust/v2rayN
Optimize file download
parent
d014724a2d
commit
7eb869ab1d
|
@ -1,4 +1,5 @@
|
||||||
using Downloader;
|
using Downloader;
|
||||||
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
||||||
namespace v2rayN.Base
|
namespace v2rayN.Base
|
||||||
|
@ -85,5 +86,72 @@ namespace v2rayN.Base
|
||||||
downloader = null;
|
downloader = null;
|
||||||
downloadOpt = null;
|
downloadOpt = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task DownloadFileAsync(IWebProxy webProxy, string url, string fileName, IProgress<double> progress, int timeout)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(url))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("url");
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(fileName))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("fileName");
|
||||||
|
}
|
||||||
|
if (File.Exists(fileName))
|
||||||
|
{
|
||||||
|
File.Delete(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cancellationToken = new CancellationTokenSource();
|
||||||
|
cancellationToken.CancelAfter(timeout * 1000);
|
||||||
|
|
||||||
|
var downloadOpt = new DownloadConfiguration()
|
||||||
|
{
|
||||||
|
Timeout = timeout * 1000,
|
||||||
|
MaxTryAgainOnFailover = 2,
|
||||||
|
RequestConfiguration =
|
||||||
|
{
|
||||||
|
Timeout= timeout * 1000,
|
||||||
|
Proxy = webProxy
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var progressPercentage = 0;
|
||||||
|
var hasValue = false;
|
||||||
|
var downloader = new DownloadService(downloadOpt);
|
||||||
|
downloader.DownloadStarted += (sender, value) =>
|
||||||
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.Report(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
downloader.DownloadProgressChanged += (sender, value) =>
|
||||||
|
{
|
||||||
|
hasValue = true;
|
||||||
|
var percent = (int)value.ProgressPercentage;// Convert.ToInt32((totalRead * 1d) / (total * 1d) * 100);
|
||||||
|
if (progressPercentage != percent && percent % 10 == 0)
|
||||||
|
{
|
||||||
|
progressPercentage = percent;
|
||||||
|
progress.Report(percent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
downloader.DownloadFileCompleted += (sender, value) =>
|
||||||
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
if (hasValue && value.Error == null)
|
||||||
|
{
|
||||||
|
progress.Report(101);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await downloader.DownloadFileTaskAsync(url, fileName, cancellationToken: cancellationToken.Token);
|
||||||
|
|
||||||
|
downloader.Dispose();
|
||||||
|
downloader = null;
|
||||||
|
downloadOpt = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -70,11 +70,6 @@ namespace v2rayN.Handler
|
||||||
Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13);
|
Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13);
|
||||||
UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Downloading));
|
UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Downloading));
|
||||||
|
|
||||||
var client = new HttpClient(new SocketsHttpHandler()
|
|
||||||
{
|
|
||||||
Proxy = GetWebProxy(blProxy)
|
|
||||||
});
|
|
||||||
|
|
||||||
var progress = new Progress<double>();
|
var progress = new Progress<double>();
|
||||||
progress.ProgressChanged += (sender, value) =>
|
progress.ProgressChanged += (sender, value) =>
|
||||||
{
|
{
|
||||||
|
@ -85,12 +80,11 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var cancellationToken = new CancellationTokenSource();
|
_ = DownloaderHelper.Instance.DownloadFileAsync(GetWebProxy(blProxy),
|
||||||
_ = HttpClientHelper.GetInstance().DownloadFileAsync(client,
|
url,
|
||||||
url,
|
Utils.GetTempPath(Utils.GetDownloadFileName(url)),
|
||||||
Utils.GetTempPath(Utils.GetDownloadFileName(url)),
|
progress,
|
||||||
progress,
|
downloadTimeout);
|
||||||
cancellationToken.Token);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue