using PDF_Master.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace PDF_Master.Helper
{
public static class FileComparisonHelper
{
#if DEBUG
public static string URI_AppXml = "http://test-pdf-pro.kdan.cn:3021/downloads/pdfreaderprocast_win_en_Us.xml";
#else
public static string URI_AppXml = "https://www.pdfreaderpro.com/downloads/pdfreaderprocast_win_en_Us.xml";
#endif
private static string OCRmodelMD5 = "";
///
/// 获取文件MD5
///
/// 文件路径
///
public static string CalculateFileMD5(string filePath)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filePath))
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}
///
/// 获取文件夹MD5
///
/// 文件路径
///
public static string CalculateFolderMD5(string folderPath)
{
var files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
using (var md5 = MD5.Create())
{
//遍历文件夹获取,各个文件夹MD5合并为一个MD5
foreach (var file in files)
{
var fileHash = CalculateFileMD5(file);
var hashBytes = Encoding.UTF8.GetBytes(fileHash);
md5.TransformBlock(hashBytes, 0, hashBytes.Length, hashBytes, 0);
}
md5.TransformFinalBlock(new byte[0], 0, 0);
var hash = md5.Hash;
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
///
/// 判断OCR资源是否存在
///
public static bool OCRModelItExist()
{
string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
if (Directory.Exists(folderPath))
{
Settings.Default.AppProperties.OCRmodelMD5 = "65d21a699138c194f06a8640dd40a901";
Settings.Default.AppProperties.OCRFile_Url = "http://test-pdf-pro.kdan.cn:3021/downloads/OCREngine.zip";
if (!Getpdfreaderprocast())
{
return false;
}
string folderMD5 = "";
if (string.IsNullOrEmpty(OCRmodelMD5))
{
folderMD5 = FileComparisonHelper.CalculateFolderMD5(folderPath);
}
else {
folderMD5 = OCRmodelMD5;
}
if (folderMD5 == Settings.Default.AppProperties.OCRmodelMD5)
{
OCRmodelMD5 = folderMD5;
return true;
}
}
else
{
Console.WriteLine("文件夹不存在");
}
if (!Getpdfreaderprocast())
{
return false;
}
return false;
}
///
/// 删除OCR资源
///
public static void RemoveOCRModel()
{
try
{
string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
if (Directory.Exists(folderPath))
{
Directory.Delete(folderPath, true);
}
string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
if (Directory.Exists(folderzipPath))
{
Directory.Delete(folderzipPath, true);
}
}
catch
{
}
}
///
/// 获取OCR下载信息
///
///
public static bool Getpdfreaderprocast()
{
if (string.IsNullOrEmpty(Settings.Default.AppProperties.OCRFile_Url) && string.IsNullOrEmpty(Settings.Default.AppProperties.OCRmodelMD5))
{
string folder = System.IO.Path.Combine(App.CurrentPath, "AppXml");
DirectoryInfo directory = new DirectoryInfo(folder);
try
{
if (!directory.Exists)
{
directory.Create();
}
string FileNameAppxml = folder + "\\" + "appxml" + ".xml";
//下载What`s new
WebClient client = new WebClient();
client.DownloadFile(URI_AppXml, FileNameAppxml);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(FileNameAppxml);
XmlNode root = xmlDoc.DocumentElement;
//获取OCR信息父节点
XmlNode itemNodes = root.SelectSingleNode("channel/item/ocrenclosure");
//获取OCR信息子节点个数
int siblingCount = 0;
foreach (XmlNode itemNode in itemNodes)
{
XmlNode parentNode = itemNode.ParentNode;
siblingCount = parentNode.ChildNodes.Count;
}
//遍历子节点
for (int i = 0; i < siblingCount; i++)
{
//对应子节点对象
XmlNode itemNode = root.SelectSingleNode("channel/item/ocrenclosure/ocrenclosure" + i);
XmlAttribute ocrstartversionUrlAttribute = itemNode.Attributes["sparkle:startversion"];
XmlAttribute ocrendversionUrlAttribute = itemNode.Attributes["sparkle:endversion"];
//版本号区间
Version ocrendversion = new Version(ocrendversionUrlAttribute.Value);
Version ocrstartversion = new Version(ocrstartversionUrlAttribute.Value);
Version Appversion = new Version(App.Version);
if (ocrstartversion <= Appversion && Appversion <= ocrendversion)
{
//获取OCR下载链接
XmlAttribute ocrUrlAttribute = itemNode.Attributes["ocrurl"];
Settings.Default.AppProperties.OCRFile_Url = ocrUrlAttribute?.Value;
//获取OCRMD5
XmlAttribute ocrUrlAttributemd5 = itemNode.Attributes["ocrmd5"];
Settings.Default.AppProperties.OCRmodelMD5 = ocrUrlAttributemd5?.Value;
return true;
}
}
return false;
}
catch { return false; }
}
return true;
}
}
}