public static bool CanDeleteLanguage(string language) { var localLanguages = _localLanguages.Where(l => l.Language.Equals(language)); if (localLanguages.Count() > 0) { OcrLanguage ocrLanguage = localLanguages.ElementAt(0); if (ocrLanguage.LocalPaths != null && ocrLanguage.LocalPaths.Count > 0) { if (_embeddedLanguageFiles != null) { foreach (KeyValuePair <string, string> entry in ocrLanguage.LocalPaths) { var embeddedFiles = _embeddedLanguageFiles.Where(e => Path.GetFileNameWithoutExtension(e).Equals(Path.GetFileNameWithoutExtension(entry.Key), StringComparison.OrdinalIgnoreCase)); if (embeddedFiles != null && embeddedFiles.Count() > 0) { // This language is an embedded language and not downloaded, so it can't be deleted return(false); } } } } } return(true); }
private static OcrLanguage GetCurrentlyDownloadingLanguage(string url) { OcrLanguage currentlyDownloadingLanguage = null; string identifier = Path.GetFileName(url).Split('.')[1]; var downloadableLanguages = DownloadableLanguages.Where(l => l.Language.Equals(identifier)); if (downloadableLanguages.Count() > 0) { currentlyDownloadingLanguage = downloadableLanguages.ElementAt(0); } return(currentlyDownloadingLanguage); }
public static void ParseManifest(Dictionary <string, Dictionary <string, Dictionary <string, object> > > manifest, out ObservableCollection <OcrLanguage> localLanguages, out ObservableCollection <OcrLanguage> downloadableLanguages) { localLanguages = new ObservableCollection <OcrLanguage>(); downloadableLanguages = new ObservableCollection <OcrLanguage>(); foreach (string languageID in manifest.Keys) { OcrLanguage language = new OcrLanguage(languageID); if (language.FileSize == 0) { localLanguages.Add(language); } else { language.IsDownloadable = true; downloadableLanguages.Add(language); } } }
private static void DownloadLanguageFiles(Dictionary <string, string> urls, string language, ProgressDelegate progress, CompletionDelegate completion) { OcrLanguage currentlyDownloadingLanguage = null; List <Exception> downloadErrors = new List <Exception>(); bool cancelled = false; try { if (urls.Count == 0) { var downloadableLanguages = DownloadableLanguages.Where(l => l.Language.Equals(language)); if (downloadableLanguages.Count() > 0) { currentlyDownloadingLanguage = downloadableLanguages.ElementAt(0); } completion?.Invoke(currentlyDownloadingLanguage, null); return; } currentlyDownloadingLanguage = GetCurrentlyDownloadingLanguage(urls.Values.ElementAt(0)); if (currentlyDownloadingLanguage != null) { if (currentlyDownloadingLanguage.CancellationTokenSource != null) { currentlyDownloadingLanguage.CancellationTokenSource.Dispose(); currentlyDownloadingLanguage.CancellationTokenSource = null; } currentlyDownloadingLanguage.CancellationTokenSource = new CancellationTokenSource(); currentlyDownloadingLanguage.IsDownloading = true; } long totalSizeToDownload = 0; Task task = null; Dictionary <string, long> totalBytesReceivedForAllDownloads = new Dictionary <string, long>(); List <Task> downloadTasks = new List <Task>(); HttpClientDownloader httpClientDownloader = new HttpClientDownloader(); httpClientDownloader.DownloadCompleted += (object sender, DownloadCompletedEventArgs e) => { if (!e.Cancelled && e.Error == null) { string finalPath = Path.Combine(LanguagesDirectory, Path.GetFileName(e.TargetFilePath)); MoveFile(e.TargetFilePath, finalPath); } else if (e.Cancelled) { cancelled = true; } else if (e.Error != null) { downloadErrors.Add(e.Error); } if (cancelled || e.Error != null) { // Delete temporary file in case or cancellation or error. DeleteFile(e.TargetFilePath); } }; httpClientDownloader.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => { // if error occurred while downloading any of this language files, then abort the rest. if (downloadErrors.Count > 0) { CancelDownloadForLanguage(currentlyDownloadingLanguage.Language); return; } if (currentlyDownloadingLanguage != null) { if (!currentlyDownloadingLanguage.IsDownloading) { // User cancelled the download of this language return; } long totalBytesReceived = 0; lock (totalBytesReceivedForAllDownloads) { totalBytesReceivedForAllDownloads[e.SourceFileUrl] = e.BytesReceived; foreach (KeyValuePair <string, long> entry in totalBytesReceivedForAllDownloads) { totalBytesReceived += entry.Value; } } currentlyDownloadingLanguage.DownloadPercentage = Math.Round((double)totalBytesReceived / totalSizeToDownload * 100, 2); progress?.Invoke(currentlyDownloadingLanguage, currentlyDownloadingLanguage.DownloadPercentage); } }; void StartDownloadTask(string url) { try { if (downloadErrors.Count > 0) { return; } string targetFilePath = Path.Combine(_downloadLanguagesDirectory, Path.GetFileName(url)); task = httpClientDownloader.DownloadFileAsync(url, targetFilePath, currentlyDownloadingLanguage.CancellationTokenSource?.Token); downloadTasks.Add(task); } catch (Exception ex2) { Console.WriteLine(ex2.Message); } } foreach (KeyValuePair <string, string> entry in urls) { string url = entry.Value; string languageFile = Path.GetFileName(url); string identifier = languageFile.Split('.')[1]; object sizeValue = Manifest[identifier][languageFile][FileSizeKeyString]; if (sizeValue.GetType() == typeof(long)) { totalSizeToDownload += (long)sizeValue; } StartDownloadTask(url); } Task.WaitAll(downloadTasks.ToArray(), -1, (currentlyDownloadingLanguage.CancellationTokenSource != null) ? currentlyDownloadingLanguage.CancellationTokenSource.Token : CancellationToken.None); downloadTasks.Clear(); if (!cancelled) { DownloadableLanguages.Remove(currentlyDownloadingLanguage); LocalLanguages.Add(currentlyDownloadingLanguage); //UpdateLanguagesWithCurrentManifest(); } } catch (Exception ex) { Console.WriteLine(ex.Message); if (ex.GetType() != typeof(TaskCanceledException) && ex.GetType() != typeof(OperationCanceledException) && currentlyDownloadingLanguage != null) { downloadErrors.Add(ex); } } finally { Exception error = null; currentlyDownloadingLanguage.IsDownloading = false; var errors = downloadErrors.Where(e => e != null); if (errors != null && errors.Count() > 0) { error = errors.ElementAt(0); } if (error != null) { DownloadableLanguagesError = error; currentlyDownloadingLanguage.DownloadPercentage = 0; } else { currentlyDownloadingLanguage.DownloadPercentage = 100; } if (!cancelled) { completion?.Invoke(currentlyDownloadingLanguage, error); } } }