diff --git a/SubtitleEdit.sln.DotSettings b/SubtitleEdit.sln.DotSettings index dd009cf93..794286398 100644 --- a/SubtitleEdit.sln.DotSettings +++ b/SubtitleEdit.sln.DotSettings @@ -21,4 +21,5 @@ True True True + True True \ No newline at end of file diff --git a/src/libse/Common/HttpClientHelper.cs b/src/libse/Common/HttpClientHelper.cs deleted file mode 100644 index c33a63d53..000000000 --- a/src/libse/Common/HttpClientHelper.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -using System.IO; -using System.Net; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace Nikse.SubtitleEdit.Core.Common -{ - public static class HttpClientHelper - { - public static HttpClient MakeHttpClient() - { - return new HttpClient(GetHttpClientHandler(Configuration.Settings.Proxy)); - } - - public static HttpClientHandler GetHttpClientHandler(ProxySettings proxySettings) - { - var handler = new HttpClientHandler(); - - if (!string.IsNullOrEmpty(proxySettings.ProxyAddress)) - { - handler.Proxy = new WebProxy(proxySettings.ProxyAddress); - handler.UseProxy = true; - } - - if (proxySettings.UseDefaultCredentials) - { - handler.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; - handler.Credentials = CredentialCache.DefaultNetworkCredentials; - } - else if (!string.IsNullOrEmpty(proxySettings.UserName) && !string.IsNullOrEmpty(proxySettings.ProxyAddress)) - { - var networkCredential = string.IsNullOrWhiteSpace(proxySettings.Domain) ? new NetworkCredential(proxySettings.UserName, proxySettings.Password) : new NetworkCredential(proxySettings.UserName, proxySettings.Password, proxySettings.Domain); - var credentialCache = new CredentialCache - { - { - new Uri(proxySettings.ProxyAddress), - proxySettings.AuthType, - networkCredential - } - }; - handler.Credentials = credentialCache; - } - - return handler; - } - - - public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, IProgress progress = null, CancellationToken cancellationToken = default) - { - try - { - client.Timeout = Timeout.InfiniteTimeSpan; - using (var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken)) - { - var contentLength = response.Content.Headers.ContentLength; - - using (var downloadStream = await response.Content.ReadAsStreamAsync()) - { - if (progress == null || !contentLength.HasValue) - { - await downloadStream.CopyToAsync(destination); - return; - } - - // Convert absolute progress (bytes downloaded) into relative progress (0% - 100%) - var relativeProgress = new Progress(totalBytes => progress.Report((float)totalBytes / contentLength.Value)); - // Use extension method to report progress while downloading - await CopyToAsync(downloadStream, destination, 81920, relativeProgress, cancellationToken); - progress.Report(1); - } - } - } - catch (Exception e) - { - SeLogger.Error(e, "DownloadAsync failed"); - throw; - } - } - - private static async Task CopyToAsync(Stream source, Stream destination, int bufferSize, IProgress progress = null, CancellationToken cancellationToken = default) - { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } - - if (destination == null) - { - throw new ArgumentNullException(nameof(destination)); - } - - if (bufferSize < 0) - { - throw new ArgumentOutOfRangeException(nameof(bufferSize)); - } - - var buffer = new byte[bufferSize]; - long totalBytesRead = 0; - int bytesRead; - while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) - { - await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); - totalBytesRead += bytesRead; - progress?.Report(totalBytesRead); - } - } - } -} diff --git a/src/libse/Common/Settings.cs b/src/libse/Common/Settings.cs index bad5c9bc4..7ffef6dc2 100644 --- a/src/libse/Common/Settings.cs +++ b/src/libse/Common/Settings.cs @@ -809,7 +809,7 @@ namespace Nikse.SubtitleEdit.Core.Common EbuStlNewLineRows = 2; PacVerticalTop = 1; - PacVerticalCenter = 5; + PacVerticalCenter = 6; PacVerticalBottom = 11; DvdStudioProHeader = @"$VertAlign = Bottom @@ -1470,6 +1470,7 @@ $HorzAlign = Center public bool DarkThemeShowListViewGridLines { get; set; } public bool ShowBetaStuff { get; set; } public bool DebugTranslationSync { get; set; } + public bool UseLegacyDownloader { get; set; } public GeneralSettings() { @@ -4479,6 +4480,12 @@ $HorzAlign = Center settings.General.DebugTranslationSync = Convert.ToBoolean(subNode.InnerText.Trim(), CultureInfo.InvariantCulture); } + subNode = node.SelectSingleNode("UseLegacyDownloader"); + if (subNode != null) + { + settings.General.UseLegacyDownloader = Convert.ToBoolean(subNode.InnerText.Trim(), CultureInfo.InvariantCulture); + } + subNode = node.SelectSingleNode("NewEmptyDefaultMs"); if (subNode != null) { @@ -10456,6 +10463,7 @@ $HorzAlign = Center textWriter.WriteElementString("DarkThemeShowListViewGridLines", settings.General.DarkThemeShowListViewGridLines.ToString(CultureInfo.InvariantCulture)); textWriter.WriteElementString("ShowBetaStuff", settings.General.ShowBetaStuff.ToString(CultureInfo.InvariantCulture)); textWriter.WriteElementString("DebugTranslationSync", settings.General.DebugTranslationSync.ToString(CultureInfo.InvariantCulture)); + textWriter.WriteElementString("UseLegacyDownloader", settings.General.UseLegacyDownloader.ToString(CultureInfo.InvariantCulture)); textWriter.WriteElementString("NewEmptyDefaultMs", settings.General.NewEmptyDefaultMs.ToString(CultureInfo.InvariantCulture)); textWriter.WriteEndElement(); diff --git a/src/libse/Forms/CheckForUpdatesHelper.cs b/src/libse/Forms/CheckForUpdatesHelper.cs index 772698fff..c49d29d5e 100644 --- a/src/libse/Forms/CheckForUpdatesHelper.cs +++ b/src/libse/Forms/CheckForUpdatesHelper.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using System; using System.Collections.Generic; using System.Linq; @@ -92,7 +93,7 @@ namespace Nikse.SubtitleEdit.Core.Forms { try { - using (var httpClient = HttpClientHelper.MakeHttpClient()) + using (var httpClient = DownloaderFactory.MakeHttpClient()) { _changeLog = httpClient.GetStringAsync(ChangeLogUrl).Result; } diff --git a/src/libse/Http/DownloaderFactory.cs b/src/libse/Http/DownloaderFactory.cs new file mode 100644 index 000000000..13379f003 --- /dev/null +++ b/src/libse/Http/DownloaderFactory.cs @@ -0,0 +1,54 @@ +using System; +using System.Net; +using System.Net.Http; +using Nikse.SubtitleEdit.Core.Common; + +namespace Nikse.SubtitleEdit.Core.Http +{ + public static class DownloaderFactory + { + public static IDownloader MakeHttpClient() + { + var httpClient = new HttpClient(GetHttpClientHandler(Configuration.Settings.Proxy)); + + if (Configuration.Settings.General.UseLegacyDownloader) + { + return new LegacyDownloader(httpClient); + } + + return new HttpClientDownloader(httpClient); + } + + public static HttpClientHandler GetHttpClientHandler(ProxySettings proxySettings) + { + var handler = new HttpClientHandler(); + + if (!string.IsNullOrEmpty(proxySettings.ProxyAddress)) + { + handler.Proxy = new WebProxy(proxySettings.ProxyAddress); + handler.UseProxy = true; + } + + if (proxySettings.UseDefaultCredentials) + { + handler.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; + handler.Credentials = CredentialCache.DefaultNetworkCredentials; + } + else if (!string.IsNullOrEmpty(proxySettings.UserName) && !string.IsNullOrEmpty(proxySettings.ProxyAddress)) + { + var networkCredential = string.IsNullOrWhiteSpace(proxySettings.Domain) ? new NetworkCredential(proxySettings.UserName, proxySettings.Password) : new NetworkCredential(proxySettings.UserName, proxySettings.Password, proxySettings.Domain); + var credentialCache = new CredentialCache + { + { + new Uri(proxySettings.ProxyAddress), + proxySettings.AuthType, + networkCredential + } + }; + handler.Credentials = credentialCache; + } + + return handler; + } + } +} diff --git a/src/libse/Http/HttpClientDownloader.cs b/src/libse/Http/HttpClientDownloader.cs new file mode 100644 index 000000000..fb37dd43f --- /dev/null +++ b/src/libse/Http/HttpClientDownloader.cs @@ -0,0 +1,109 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Common; + +namespace Nikse.SubtitleEdit.Core.Http +{ + public class HttpClientDownloader : IDownloader + { + private readonly HttpClient _httpClient; + + public HttpClientDownloader(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public Uri BaseAddress + { + get => _httpClient.BaseAddress; + set => _httpClient.BaseAddress = value; + } + + public HttpRequestHeaders DefaultRequestHeaders => _httpClient.DefaultRequestHeaders; + + public Task PostAsync(string uri, StringContent stringContent) + { + return _httpClient.PostAsync(uri, stringContent); + } + + public Task GetStringAsync(string url) + { + return _httpClient.GetStringAsync(url); + } + + public async Task DownloadAsync(string requestUri, Stream destination, IProgress progress = null, CancellationToken cancellationToken = default) + { + try + { + _httpClient.Timeout = Timeout.InfiniteTimeSpan; + using (var response = await _httpClient.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken)) + { + var contentLength = response.Content.Headers.ContentLength; + using (var downloadStream = await response.Content.ReadAsStreamAsync()) + { + if (progress == null || !contentLength.HasValue) + { + await downloadStream.CopyToAsync(destination); + return; + } + + // Convert absolute progress (bytes downloaded) into relative progress (0% - 100%) + var relativeProgress = new Progress(totalBytes => progress.Report((float)totalBytes / contentLength.Value)); + // Use extension method to report progress while downloading + await CopyToAsync(downloadStream, destination, 81920, relativeProgress, cancellationToken); + progress.Report(1); + } + } + } + catch (Exception e) + { + SeLogger.Error(e, $"DownloadAsync failed - {requestUri}"); + + if (Environment.OSVersion.Version.Major < 10) + { + Configuration.Settings.General.UseLegacyDownloader = true; + SeLogger.Error("Switching to legacy downloader due to old OS!"); + } + + throw; + } + } + + public static async Task CopyToAsync(Stream source, Stream destination, int bufferSize, IProgress progress = null, CancellationToken cancellationToken = default) + { + if (source == null) + { + throw new ArgumentNullException(nameof(source)); + } + + if (destination == null) + { + throw new ArgumentNullException(nameof(destination)); + } + + if (bufferSize < 0) + { + throw new ArgumentOutOfRangeException(nameof(bufferSize)); + } + + var buffer = new byte[bufferSize]; + long totalBytesRead = 0; + int bytesRead; + while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) + { + await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); + totalBytesRead += bytesRead; + progress?.Report(totalBytesRead); + } + } + + public void Dispose() + { + _httpClient?.Dispose(); + } + } +} \ No newline at end of file diff --git a/src/libse/Http/IDownloader.cs b/src/libse/Http/IDownloader.cs new file mode 100644 index 000000000..9bbcb9bdc --- /dev/null +++ b/src/libse/Http/IDownloader.cs @@ -0,0 +1,18 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; + +namespace Nikse.SubtitleEdit.Core.Http +{ + public interface IDownloader : IDisposable + { + Task DownloadAsync(string requestUri, Stream destination, IProgress progress = null, CancellationToken cancellationToken = default); + Uri BaseAddress { get; set; } + HttpRequestHeaders DefaultRequestHeaders { get; } + Task PostAsync(string uri, StringContent stringContent); + Task GetStringAsync(string url); + } +} \ No newline at end of file diff --git a/src/libse/Http/LegacyDownloader.cs b/src/libse/Http/LegacyDownloader.cs new file mode 100644 index 000000000..71ac13d80 --- /dev/null +++ b/src/libse/Http/LegacyDownloader.cs @@ -0,0 +1,167 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Common; + +namespace Nikse.SubtitleEdit.Core.Http +{ + public class LegacyDownloader : IDownloader + { + public Task DownloadAsync(string requestUri, Stream destination, IProgress progress = null, CancellationToken cancellationToken = default) + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; + + return Task.Factory.StartNew(delegate + { + try + { + const int bufferSize = 4096; + var request = (HttpWebRequest)HttpWebRequest.Create(requestUri); + request.Timeout = Timeout.Infinite; + var response = (HttpWebResponse)request.GetResponse(); + var responseStream = response.GetResponseStream(); + var tempFileName = Path.GetTempFileName(); + var fileStream = new FileStream(tempFileName, FileMode.OpenOrCreate, FileAccess.Write); + var buff = new byte[bufferSize]; + int bytesRead; + long totalBytesRead = 0; + while ((bytesRead = responseStream.Read(buff, 0, bufferSize)) > 0 && !cancellationToken.IsCancellationRequested) + { + fileStream.Write(buff, 0, bytesRead); + fileStream.Flush(); + + totalBytesRead += bytesRead; + + if (progress != null && response.ContentLength > 0) + { + progress.Report(totalBytesRead / (float)response.ContentLength); + } + } + fileStream.Close(); + responseStream.Close(); + + var fs = new FileStream(tempFileName, FileMode.Open); + CopyTo(fs, destination, 2048); + fs.Close(); + + try + { + File.Delete(tempFileName); + } + catch + { + // ignore + } + } + catch (Exception exception) + { + SeLogger.Error(exception, $"Error downloading {requestUri}"); + throw; + } + }, cancellationToken); + } + + private readonly HttpClient _httpClient; + + public LegacyDownloader(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public Uri BaseAddress + { + get => _httpClient.BaseAddress; + set => _httpClient.BaseAddress = value; + } + + public HttpRequestHeaders DefaultRequestHeaders => _httpClient.DefaultRequestHeaders; + + public Task PostAsync(string uri, StringContent stringContent) + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; + return _httpClient.PostAsync(uri, stringContent); + } + + public async Task GetStringAsync(string url) + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; + + var webClient = new WebClient { Proxy = GetProxy() }; + foreach (var header in _httpClient.DefaultRequestHeaders) + { + foreach (var v in header.Value) + { + webClient.Headers.Add(header.Key, v); + } + } + + return await Task.Run(() => webClient.DownloadStringTaskAsync(new Uri(url))).ConfigureAwait(false); + } + + public void Dispose() + { + _httpClient?.Dispose(); + } + + public static void CopyTo(Stream source, Stream destination, int bufferSize) + { + if (source == null) + { + throw new ArgumentNullException(nameof(source)); + } + + if (destination == null) + { + throw new ArgumentNullException(nameof(destination)); + } + + if (bufferSize < 0) + { + throw new ArgumentOutOfRangeException(nameof(bufferSize)); + } + + var buffer = new byte[bufferSize]; + var bytesRead = int.MaxValue; + while (bytesRead != 0) + { + if (bytesRead != int.MaxValue) + { + destination.Write(buffer, 0, bytesRead); + } + + bytesRead = source.Read(buffer, 0, buffer.Length); + } + } + + public static WebProxy GetProxy() + { + if (string.IsNullOrEmpty(Configuration.Settings.Proxy.ProxyAddress)) + { + return null; + } + + var proxy = new WebProxy(Configuration.Settings.Proxy.ProxyAddress); + if (!string.IsNullOrEmpty(Configuration.Settings.Proxy.UserName)) + { + if (string.IsNullOrEmpty(Configuration.Settings.Proxy.Domain)) + { + proxy.Credentials = new NetworkCredential(Configuration.Settings.Proxy.UserName, Configuration.Settings.Proxy.DecodePassword()); + } + else + { + proxy.Credentials = new NetworkCredential(Configuration.Settings.Proxy.UserName, Configuration.Settings.Proxy.DecodePassword(), Configuration.Settings.Proxy.Domain); + } + } + else + { + proxy.UseDefaultCredentials = true; + } + + return proxy; + } + } +} \ No newline at end of file diff --git a/src/libse/Translate/Service/GoogleTranslator1.cs b/src/libse/Translate/Service/GoogleTranslator1.cs index 86872fcb4..3c2135863 100644 --- a/src/libse/Translate/Service/GoogleTranslator1.cs +++ b/src/libse/Translate/Service/GoogleTranslator1.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using System; using System.Collections.Generic; using System.Linq; @@ -14,13 +15,14 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service /// public class GoogleTranslator1 : ITranslationStrategy { - private readonly HttpClient _httpClient; + private readonly IDownloader _httpClient; private const char SplitChar = '\n'; public GoogleTranslator1() { - _httpClient = HttpClientHelper.MakeHttpClient(); + var x = new HttpClient(); + _httpClient = DownloaderFactory.MakeHttpClient(); _httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8"); _httpClient.BaseAddress = new Uri("https://translate.googleapis.com/"); diff --git a/src/libse/Translate/Service/GoogleTranslator2.cs b/src/libse/Translate/Service/GoogleTranslator2.cs index 34bfd10a7..afc5078f5 100644 --- a/src/libse/Translate/Service/GoogleTranslator2.cs +++ b/src/libse/Translate/Service/GoogleTranslator2.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using System; using System.Collections.Generic; using System.Net; @@ -15,7 +16,7 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service public class GoogleTranslator2 : ITranslationStrategy { private readonly string _apiKey; - private readonly HttpClient _httpClient; + private readonly IDownloader _httpClient; public string GetName() { @@ -35,7 +36,7 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service public GoogleTranslator2(string apiKey) { _apiKey = apiKey; - _httpClient = HttpClientHelper.MakeHttpClient(); + _httpClient = DownloaderFactory.MakeHttpClient(); _httpClient.BaseAddress = new Uri("https://translation.googleapis.com/language/translate/v2/"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } diff --git a/src/libse/Translate/Service/GoogleTranslator3.cs b/src/libse/Translate/Service/GoogleTranslator3.cs index 609ec4131..672bb4ae2 100644 --- a/src/libse/Translate/Service/GoogleTranslator3.cs +++ b/src/libse/Translate/Service/GoogleTranslator3.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.SubtitleFormats; using System; using System.Collections.Generic; @@ -18,13 +19,13 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service { private readonly string _apiKey; private readonly string _projectNumberOrId; - private readonly HttpClient _httpClient; + private readonly IDownloader _httpClient; public GoogleTranslator3(string apiKey, string projectNumberOrId) { _apiKey = apiKey; _projectNumberOrId = projectNumberOrId; - _httpClient = HttpClientHelper.MakeHttpClient(); + _httpClient = DownloaderFactory.MakeHttpClient(); _httpClient.BaseAddress = new Uri("https://translation.googleapis.com/v3/"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } diff --git a/src/libse/Translate/Service/MicrosoftTranslationService.cs b/src/libse/Translate/Service/MicrosoftTranslationService.cs index 2df9cfe3d..653719583 100644 --- a/src/libse/Translate/Service/MicrosoftTranslationService.cs +++ b/src/libse/Translate/Service/MicrosoftTranslationService.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.SubtitleFormats; using System; using System.Collections.Generic; @@ -22,7 +23,7 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service private static List _translationPairs; private readonly string _accessToken; private readonly string _category; - private HttpClient _httpClient; + private IDownloader _httpClient; public MicrosoftTranslationService(string apiKey, string tokenEndpoint, string category) { @@ -38,11 +39,11 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service } } - private HttpClient GetTranslateClient() + private IDownloader GetTranslateClient() { if (_httpClient == null) { - _httpClient = HttpClientHelper.MakeHttpClient(); + _httpClient = DownloaderFactory.MakeHttpClient(); _httpClient.BaseAddress = new Uri("https://api.cognitive.microsofttranslator.com/"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); @@ -53,7 +54,7 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service private static string GetAccessToken(string apiKey, string tokenEndpoint) { - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); httpClient.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); @@ -69,7 +70,7 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service return _translationPairs; } - using (var httpClient = HttpClientHelper.MakeHttpClient()) + using (var httpClient = DownloaderFactory.MakeHttpClient()) { httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8"); diff --git a/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs b/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs index 09acb2a18..c61bb23f0 100644 --- a/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs +++ b/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using System; using System.Collections.Generic; using System.Drawing; @@ -19,7 +20,7 @@ namespace Nikse.SubtitleEdit.Core.VobSub.Ocr.Service public class GoogleCloudVisionApi : IOcrStrategy { private readonly string _apiKey; - private readonly HttpClient _httpClient; + private readonly IDownloader _httpClient; public string GetName() { @@ -152,7 +153,7 @@ namespace Nikse.SubtitleEdit.Core.VobSub.Ocr.Service public GoogleCloudVisionApi(string apiKey) { _apiKey = apiKey; - _httpClient = HttpClientHelper.MakeHttpClient(); + _httpClient = DownloaderFactory.MakeHttpClient(); _httpClient.BaseAddress = new Uri("https://vision.googleapis.com/v1/images:annotate"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } diff --git a/src/ui/Forms/AudioToText/VoskModelDownload.cs b/src/ui/Forms/AudioToText/VoskModelDownload.cs index 0794c42fc..d7e65d315 100644 --- a/src/ui/Forms/AudioToText/VoskModelDownload.cs +++ b/src/ui/Forms/AudioToText/VoskModelDownload.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Windows.Forms; using Nikse.SubtitleEdit.Core.AudioToText; using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; namespace Nikse.SubtitleEdit.Forms.AudioToText @@ -69,7 +70,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText buttonDownload.Enabled = false; Refresh(); Cursor = Cursors.WaitCursor; - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/AudioToText/WhisperDownload.cs b/src/ui/Forms/AudioToText/WhisperDownload.cs index 743530fe2..ac0ffc4e5 100644 --- a/src/ui/Forms/AudioToText/WhisperDownload.cs +++ b/src/ui/Forms/AudioToText/WhisperDownload.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading; using System.Windows.Forms; using Nikse.SubtitleEdit.Core.AudioToText; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Forms.AudioToText { @@ -81,13 +82,14 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText try { - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(downloadUrl, downloadStream, new Progress((progress) => { var pct = (int)Math.Round(progress * 100.0, MidpointRounding.AwayFromZero); labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait + " " + pct + "%"; + labelPleaseWait.Refresh(); }), _cancellationTokenSource.Token); while (!downloadTask.IsCompleted && !downloadTask.IsCanceled) diff --git a/src/ui/Forms/AudioToText/WhisperModelDownload.cs b/src/ui/Forms/AudioToText/WhisperModelDownload.cs index ee2f3f22c..ba99e53a9 100644 --- a/src/ui/Forms/AudioToText/WhisperModelDownload.cs +++ b/src/ui/Forms/AudioToText/WhisperModelDownload.cs @@ -1,5 +1,5 @@ using Nikse.SubtitleEdit.Core.AudioToText; -using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.IO; @@ -102,7 +102,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText foreach (var url in LastDownloadedModel.Urls) { - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); currentDownloadUrl = url; _downloadFileName = MakeDownloadFileName(LastDownloadedModel, url) + ".$$$"; labelFileName.Text = url.Split('/').Last(); diff --git a/src/ui/Forms/DownloadFfmpeg.cs b/src/ui/Forms/DownloadFfmpeg.cs index 0da002fae..1f05f8819 100644 --- a/src/ui/Forms/DownloadFfmpeg.cs +++ b/src/ui/Forms/DownloadFfmpeg.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.IO; @@ -53,7 +54,7 @@ namespace Nikse.SubtitleEdit.Forms labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait; buttonOK.Enabled = false; Cursor = Cursors.WaitCursor; - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/DownloadVosk.cs b/src/ui/Forms/DownloadVosk.cs index 209c81640..0410f6ed7 100644 --- a/src/ui/Forms/DownloadVosk.cs +++ b/src/ui/Forms/DownloadVosk.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.IO; @@ -62,7 +63,7 @@ namespace Nikse.SubtitleEdit.Forms Refresh(); Cursor = Cursors.WaitCursor; - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(VoskUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/DownloadYouTubeDl.cs b/src/ui/Forms/DownloadYouTubeDl.cs index fe3ef8b25..5614d1040 100644 --- a/src/ui/Forms/DownloadYouTubeDl.cs +++ b/src/ui/Forms/DownloadYouTubeDl.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.IO; @@ -53,7 +54,7 @@ namespace Nikse.SubtitleEdit.Forms buttonOK.Enabled = false; Refresh(); Cursor = Cursors.WaitCursor; - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(Url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/GetDictionaries.cs b/src/ui/Forms/GetDictionaries.cs index 9e16a5280..9cb01ff06 100644 --- a/src/ui/Forms/GetDictionaries.cs +++ b/src/ui/Forms/GetDictionaries.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.Collections.Generic; @@ -174,7 +175,7 @@ namespace Nikse.SubtitleEdit.Forms Refresh(); Cursor = Cursors.WaitCursor; - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(_downloadLink, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/DownloadTesseract302.cs b/src/ui/Forms/Ocr/DownloadTesseract302.cs index ed7cf2dad..d68288a94 100644 --- a/src/ui/Forms/Ocr/DownloadTesseract302.cs +++ b/src/ui/Forms/Ocr/DownloadTesseract302.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.IO; @@ -29,7 +30,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr try { - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/DownloadTesseract5.cs b/src/ui/Forms/Ocr/DownloadTesseract5.cs index 75da65fd6..a92815cdd 100644 --- a/src/ui/Forms/Ocr/DownloadTesseract5.cs +++ b/src/ui/Forms/Ocr/DownloadTesseract5.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.IO; @@ -28,7 +29,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr try { Utilities.SetSecurityProtocol(); - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(TesseractDownloadUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs b/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs index 985a4478e..3b1ef6a91 100644 --- a/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs +++ b/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.Collections.Generic; @@ -106,7 +107,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr try { - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/GetTesseractDictionaries.cs b/src/ui/Forms/Ocr/GetTesseractDictionaries.cs index eaf490120..1698bc3da 100644 --- a/src/ui/Forms/Ocr/GetTesseractDictionaries.cs +++ b/src/ui/Forms/Ocr/GetTesseractDictionaries.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.Collections.Generic; @@ -86,7 +87,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr ChosenLanguage = comboBoxDictionaries.Items[index].ToString(); - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Options/SettingsMpv.cs b/src/ui/Forms/Options/SettingsMpv.cs index 9027bead5..af154a924 100644 --- a/src/ui/Forms/Options/SettingsMpv.cs +++ b/src/ui/Forms/Options/SettingsMpv.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using Nikse.SubtitleEdit.Logic.VideoPlayers; using System; @@ -45,7 +46,7 @@ namespace Nikse.SubtitleEdit.Forms.Options buttonDownload.Enabled = false; Refresh(); Cursor = Cursors.WaitCursor; - var httpClient = HttpClientHelper.MakeHttpClient(); + var httpClient = DownloaderFactory.MakeHttpClient(); using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(_downloadUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/PluginsGet.cs b/src/ui/Forms/PluginsGet.cs index cdeea9ad9..044314fa8 100644 --- a/src/ui/Forms/PluginsGet.cs +++ b/src/ui/Forms/PluginsGet.cs @@ -1,4 +1,5 @@ using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Logic; using System; using System.Collections.Generic; @@ -82,7 +83,7 @@ namespace Nikse.SubtitleEdit.Forms Refresh(); ShowInstalledPlugins(); - using (var httpClient = HttpClientHelper.MakeHttpClient()) + using (var httpClient = DownloaderFactory.MakeHttpClient()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => @@ -301,7 +302,7 @@ namespace Nikse.SubtitleEdit.Forms Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = HttpClientHelper.MakeHttpClient()) + using (var httpClient = DownloaderFactory.MakeHttpClient()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => @@ -508,7 +509,7 @@ namespace Nikse.SubtitleEdit.Forms Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = HttpClientHelper.MakeHttpClient()) + using (var httpClient = DownloaderFactory.MakeHttpClient()) { _updatingAllPluginsCount = 0; _updatingAllPlugins = true;