123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Office.Interop.Excel;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.Properties;
- using PDF_Master.Strings.MainPage;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
- {
- public class OCRDownloadProgressViewModel : BindableBase, IDialogAware
- {
- public string Title => throw new NotImplementedException();
- private string T_progressBarName;
- public string T_ProgressBarName
- {
- get { return T_progressBarName; }
- set
- {
- SetProperty(ref T_progressBarName, value);
- }
- }
- private string T_tryAgainBtn;
- public string T_TryAgainBtn
- {
- get { return T_tryAgainBtn; }
- set
- {
- SetProperty(ref T_tryAgainBtn, value);
- }
- }
- private string T_errorTextBlock;
- public string T_ErrorTextBlock
- {
- get { return T_errorTextBlock; }
- set
- {
- SetProperty(ref T_errorTextBlock, value);
- }
- }
- private string T_cancelBtn;
- public string T_CancelBtn
- {
- get { return T_cancelBtn; }
- set
- {
- SetProperty(ref T_cancelBtn, value);
- }
- }
- private void SetLangText()
- {
- T_ProgressBarName = App.MainPageLoader.GetString("OCR_Downloading");
- T_tryAgainBtn = App.MainPageLoader.GetString("OCR_DownloadRedownload");
- T_ErrorTextBlock = App.MainPageLoader.GetString("OCR_DownloadError");
- T_CancelBtn = App.MainPageLoader.GetString("OCR_CancelDownload");
- }
- private Visibility errorStackPanelVisible = Visibility.Collapsed;
- public Visibility ErrorStackPanelVisible
- {
- get { return errorStackPanelVisible; }
- set
- {
- SetProperty(ref errorStackPanelVisible, value);
- }
- }
- private Visibility tryAgainBtnVisible = Visibility.Collapsed;
- public Visibility TryAgainBtnVisible
- {
- get { return tryAgainBtnVisible; }
- set
- {
- SetProperty(ref tryAgainBtnVisible, value);
- }
- }
- System.Net.WebClient client = null;
- private MainPage mainPage = null;
- public string exePath = "";
- private string FileNameComPDFKit = "";
- private bool isItemClieck = false;
- private bool isDownloadCancelled = false;
- private CPDFViewer Pdfviewer { set; get; }
- private string password = "";
- private double currentSize;
- private string progressBarCurrentSizeValue = "0 MB";
- public string ProgressBarCurrentSizeValue
- {
- get { return progressBarCurrentSizeValue; }
- set
- {
- SetProperty(ref progressBarCurrentSizeValue, value);
- }
- }
- private string progressBarFileSize = "0 MB";
- public string ProgressBarFileSize
- {
- get { return progressBarFileSize; }
- set
- {
- SetProperty(ref progressBarFileSize, value);
- }
- }
- private double progressBarCurrentValue = 0;
- public double ProgressBarCurrentValue
- {
- get { return progressBarCurrentValue; }
- set
- {
- SetProperty(ref progressBarCurrentValue, value);
- }
- }
- #region 委托声明
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand TryAgainCommnad { get; set; }
- #endregion
- public OCRDownloadProgressViewModel()
- {
- CancelCommand = new DelegateCommand(cancel);
- TryAgainCommnad = new DelegateCommand(tryAgain);
- SetLangText();
- }
- private async void tryAgain()
- {
- try
- {
- client = null;
- FileComparisonHelper.RemoveOCRModel();
- FileComparisonHelper.Getpdfreaderprocast();
- await DownloadOCR();
- }
- catch
- {
- await Task.Delay(100);
- ErrorStackPanelVisible = Visibility.Visible;
- TryAgainBtnVisible = Visibility.Visible;
- }
- }
- private void cancel()
- {
- isDownloadCancelled = true;
- if (client != null)
- {
- //防止退出没释放
- client.CancelAsync();
- client.Dispose();
- FileComparisonHelper.RemoveOCRModel();
- }
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- public async Task<bool> DownloadOCR()
- {
- bool canDownloadCancelled = false;
- ErrorStackPanelVisible = Visibility.Collapsed;
- TryAgainBtnVisible = Visibility.Collapsed;
- try
- {
- await Task.Run(() =>
- {
- string folder = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
- FileNameComPDFKit = folder + "\\" + "OCREngine" + ".zip";
- long fileSize = 0;
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- client = new System.Net.WebClient();
- System.Net.WebRequest request = System.Net.WebRequest.Create(Settings.Default.AppProperties.OCRFile_Url);
- request.Method = "HEAD";
- using (System.Net.WebResponse response = request.GetResponse())
- {
- if (long.TryParse(response.Headers.Get("Content-Length"), out long contentLength))
- {
- fileSize = contentLength;
- App.Current.Dispatcher.Invoke(() =>
- {
- ProgressBarFileSize = ((double)fileSize / (1024 * 1024)).ToString("0.00") + " MB";
- currentSize = ((double)fileSize / (1024 * 1024));
- });
- }
- }
- client.DownloadProgressChanged += Client_DownloadProgressChanged;
- client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadCompleted);
- client.DownloadFileAsync(new Uri(Settings.Default.AppProperties.OCRFile_Url), FileNameComPDFKit);
- }
- canDownloadCancelled = true;
- });
- return canDownloadCancelled;
- }
- catch
- {
- canDownloadCancelled = false;
- ErrorStackPanelVisible = Visibility.Visible;
- TryAgainBtnVisible = Visibility.Visible;
- //Close();
- return canDownloadCancelled;
- }
- }
- private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
- {
- string folderPath = App.CurrentPath;
- //解压转档资源文件,intiRes
- string folderModelPath = App.CurrentPath;
- if (!Directory.Exists(folderModelPath + "//OCREngine"))
- {
- ExtractWithProgress(FileNameComPDFKit, folderModelPath);
- }
- exePath = folderModelPath;
- }
- public void ExtractWithProgress(string sourceZipFilePath, string destinationFolderPath)
- {
- var extractedCount = 0;
- if (System.IO.File.Exists(sourceZipFilePath))
- {
- try
- {
- using (var zip = ZipFile.OpenRead(sourceZipFilePath))
- {
- var totalEntries = zip.Entries.Count;
- var progressStep = totalEntries / 20;
- foreach (var entry in zip.Entries)
- {
- var extractionPath = System.IO.Path.Combine(destinationFolderPath, entry.FullName);
- string replacement = "OCREngine";
- //提换文件夹名字保证文件夹统一
- int slashIndex = entry.FullName.IndexOf('/');
- if (slashIndex != -1)
- {
- string modifiedString = replacement + entry.FullName.Substring(slashIndex);
- extractionPath = System.IO.Path.Combine(destinationFolderPath, modifiedString);
- }
- if (entry.Name == "")
- {
- // 处理目录的情况,例如在ZIP文件中的文件夹
- Directory.CreateDirectory(extractionPath);
- continue;
- }
- var entryDirectoryName = System.IO.Path.GetDirectoryName(extractionPath);
- if (!Directory.Exists(entryDirectoryName))
- {
- Directory.CreateDirectory(entryDirectoryName);
- }
- entry.ExtractToFile(extractionPath, true);
- extractedCount++;
- // 更新进度条:输出百分比或者使用其他进度条组件
- App.Current.Dispatcher.Invoke(() =>
- {
- ProgressBarCurrentValue = extractedCount / progressStep + 80;
- ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
- Trace.WriteLine("进度条 : " + ProgressBarCurrentValue);
- });
- }
- }
- App.Current.Dispatcher.Invoke(() =>
- {
- ProgressBarCurrentValue = 0;
- ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
- string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
- if (Directory.Exists(folderzipPath))
- {
- Directory.Delete(folderzipPath, true);
- }
- //Close()
- if (FileComparisonHelper.OCRModelItExist())
- {
- var dialogresult = new DialogResult(ButtonResult.OK);
- RequestClose.Invoke(dialogresult);
- }
- else
- {
- string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
- if (Directory.Exists(folderPath))
- {
- Directory.Delete(folderPath, true);
- }
- //MessageBoxEx.Show("需要更新最新版的APP才能下载哦");
- }
- });
- }
- catch
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- ProgressBarCurrentValue = 0;
- ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
- //Close();
- ErrorStackPanelVisible = Visibility.Visible;
- TryAgainBtnVisible = Visibility.Visible;
- });
- }
- }
- else
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- ProgressBarCurrentValue = 0;
- ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
- ErrorStackPanelVisible = Visibility.Visible;
- TryAgainBtnVisible = Visibility.Visible;
- //Close();
- });
- }
- }
- private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- ProgressBarCurrentValue = e.ProgressPercentage * 0.8;
- ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
- });
- if (isDownloadCancelled)
- {
- if (client != null)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- client.CancelAsync();
- });
- }
- // 执行其他取消操作
- // ...
- }
- }
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public async void OnDialogOpened(IDialogParameters parameters)
- {
- await DownloadOCR();
- }
- }
- }
|