KMResourceDownloadManager.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // KMResourceDownloadManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/8/29.
  6. //
  7. import Cocoa
  8. import Foundation
  9. import ZipArchive // 请确保已导入 SSZipArchive 或其他合适的解压库
  10. import ComDocumentAIKit
  11. import ComPDFKit_Conversion
  12. let xmlURLString = KMServerConfig().kResourceServerURL
  13. let kResourcePath: String = kAppSupportOfBundleIdentifierDirectory.path
  14. let kLocalFilePath: String = kAppSupportOfBundleIdentifierDirectory.appendingPathComponent("DocumentAI.bundle").path
  15. enum KMResourceDownloadState {
  16. case none
  17. case unzipFailed
  18. case moveFailed
  19. case success
  20. case retry
  21. case cancel
  22. }
  23. struct Version: Comparable {
  24. let components: [Int]
  25. init?(_ version: String) {
  26. components = version.split(separator: ".").compactMap { Int($0) }
  27. guard !components.isEmpty else { return nil }
  28. }
  29. static func < (lhs: Version, rhs: Version) -> Bool {
  30. for (l, r) in zip(lhs.components, rhs.components) {
  31. if l != r { return l < r }
  32. }
  33. return lhs.components.count < rhs.components.count
  34. }
  35. }
  36. class KMResourceDownloadManager: NSObject {
  37. static let manager = KMResourceDownloadManager()
  38. var downloadTask: URLSessionDownloadTask?
  39. var progressBlock: ((Double) -> Void)?
  40. var downloadResultBlock: ((Bool, KMResourceDownloadState) -> Void)?
  41. var reachabilityAlert: NSAlert?
  42. func downloadFramework(progress: @escaping (Double) -> Void, result: @escaping (Bool, KMResourceDownloadState) -> Void) {
  43. self.progressBlock = progress
  44. self.downloadResultBlock = result
  45. KMRequestServer.requestServer.reachabilityStatusChange { [weak self] status in
  46. if status == .notReachable {
  47. KMPrint("无网络")
  48. self?.downloadTask?.cancel()
  49. self?.downloadTask = nil
  50. self?.downloadResultBlock?(false, .none)
  51. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [weak self] in
  52. if self?.reachabilityAlert == nil {
  53. self?.reachabilityAlert = NSAlert()
  54. self?.reachabilityAlert?.messageText = NSLocalizedString("Network Disconnected", comment: "")
  55. self?.reachabilityAlert?.informativeText = NSLocalizedString("Please connect to the internet and download again.", comment: "")
  56. self?.reachabilityAlert?.addButton(withTitle: NSLocalizedString("Retry", comment: ""))
  57. self?.reachabilityAlert?.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  58. var window = NSWindow.currentWindow()
  59. if window != nil {
  60. self?.reachabilityAlert?.beginSheetModal(for: window) { result in
  61. if (result == .alertSecondButtonReturn) {
  62. self?.reachabilityAlert = nil
  63. self?.cancelDownload()
  64. } else if result == .alertFirstButtonReturn {
  65. self?.reachabilityAlert = nil
  66. self?.downloadResultBlock?(false, .retry)
  67. self?.cancelDownload()
  68. return
  69. }
  70. self?.reachabilityAlert = nil
  71. }
  72. } else {
  73. self?.reachabilityAlert = nil
  74. }
  75. } else {
  76. self?.reachabilityAlert = nil
  77. }
  78. })
  79. } else {
  80. KMPrint("有网络")
  81. }
  82. }
  83. if self.downloadTask == nil {
  84. self.downloadXML { [unowned self] content in
  85. let urlString = self.dealXML(content: content)
  86. // let urlString = "http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip"
  87. if let url = URL(string: urlString) {
  88. let configuration = URLSessionConfiguration.default
  89. let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
  90. let downloadTask = session.downloadTask(with: url)
  91. downloadTask.resume()
  92. self.downloadTask = downloadTask
  93. } else {
  94. dealDownloadResult(isSuccess: false, state: .none)
  95. }
  96. }
  97. } else {
  98. dealDownloadResult(isSuccess: false, state: .none)
  99. }
  100. }
  101. func documentAIBundleExists(complete:@escaping (_ result: Bool) -> Void) {
  102. let filePath: String = kLocalFilePath
  103. let fileManager = FileManager.default
  104. debugPrint(filePath)
  105. debugPrint(FileManager.default.temporaryDirectory.appendingPathComponent("XMLResources"))
  106. let exists = fileManager.fileExists(atPath: filePath as String)
  107. //如果存在则判断版本是否符合,如果不符合则删除后重新下载
  108. if exists {
  109. self.checkDocumentAIVersion(complete: complete)
  110. self.loadDocumentAIBundle(bundlePath: kLocalFilePath)
  111. } else {
  112. self.removeDownloadResourcePath()
  113. complete(false)
  114. }
  115. }
  116. func cancelDownload() {
  117. downloadTask?.cancel()
  118. downloadTask = nil
  119. progressBlock = nil
  120. downloadResultBlock = nil
  121. self.downloadResultBlock?(false, .cancel)
  122. }
  123. //结果处理
  124. func dealDownloadResult(isSuccess: Bool, state: KMResourceDownloadState) {
  125. DispatchQueue.main.async {
  126. self.downloadResultBlock?(isSuccess, state)
  127. self.cancelDownload()
  128. }
  129. }
  130. func checkDocumentAIVersion(complete: @escaping (_ results: Bool) -> Void) {
  131. self.downloadXML { [unowned self] content in
  132. let urlString = self.dealXML(content: content)
  133. if urlString.count != 0 {
  134. try?FileManager.default.removeItem(atPath: kLocalFilePath)
  135. DispatchQueue.main.async {
  136. complete(false)
  137. }
  138. } else {
  139. DispatchQueue.main.async {
  140. complete(true)
  141. }
  142. }
  143. }
  144. }
  145. func loadDocumentAIBundle(bundlePath: String) {
  146. guard FileManager.default.fileExists(atPath: bundlePath) else {
  147. print("Bundle does not exist at specified path.")
  148. return
  149. }
  150. if let bundle = Bundle(path: bundlePath) {
  151. if let resourcePath = bundle.path(forResource: "DocumentAI", ofType: "model") {
  152. print("Found resource at path: \(resourcePath)")
  153. //绑定资源包
  154. CDocumentAIKit.sharedInstance().setOCRModelPath(bundlePath)
  155. CPDFConvertKit.setOCRModelPath(bundlePath)
  156. } else {
  157. print("Resource not found.")
  158. }
  159. } else {
  160. print("Failed to load bundle.")
  161. }
  162. }
  163. }
  164. //MARK: UI
  165. extension KMResourceDownloadManager {
  166. func needDownloadOCRResource(complete:@escaping (_ result: Bool) -> Void) {
  167. #if VERSION_DMG
  168. KMResourceDownloadManager.manager.documentAIBundleExists(complete: { result in
  169. complete(!result)
  170. })
  171. #else
  172. complete(false)
  173. #endif
  174. }
  175. func downLoadOCRResource(window: NSWindow) {
  176. #if VERSION_DMG
  177. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  178. KMResourceDownloadManager.manager.documentAIBundleExists(complete: { [weak self] result in
  179. if result {
  180. } else {
  181. let alert = NSAlert()
  182. alert.alertStyle = .critical
  183. alert.messageText = KMLocalizedString("Need DownLoad OCR Resource", comment: "")
  184. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  185. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  186. alert.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] response in
  187. if response == NSApplication.ModalResponse.alertFirstButtonReturn {
  188. self?.downLoad(window: window)
  189. }
  190. }
  191. }
  192. })
  193. }
  194. #endif
  195. }
  196. #if VERSION_DMG
  197. func downLoad(window: NSWindow) {
  198. let controller = KMOCRDownloadViewController(nibName: "KMOCRDownloadViewController", bundle: nil)
  199. let tempWindow = NSWindow(contentViewController: controller)
  200. tempWindow.maxSize = CGSizeMake(480, 208)
  201. tempWindow.minSize = CGSizeMake(480, 208)
  202. tempWindow.styleMask.remove(.resizable)
  203. window.beginSheet(tempWindow)
  204. controller.closeAction = { [unowned self] controller2 in
  205. window.endSheet(tempWindow)
  206. }
  207. controller.completionAction = { [unowned self] controller2 in
  208. window.endSheet(tempWindow)
  209. }
  210. controller.begin()
  211. }
  212. #endif
  213. }
  214. extension KMResourceDownloadManager: XMLParserDelegate {
  215. func downloadXML(completion: @escaping (_ content: String) -> Void) {
  216. if let xmlURL = URL(string: xmlURLString) {
  217. let request = URLRequest(url: xmlURL)
  218. let session = URLSession.shared
  219. let task = session.dataTask(with: request) { (data, response, error) in
  220. if let error = error {
  221. print("Error: \(error)")
  222. } else if let data = data {
  223. if let xmlString = String(data: data, encoding: .utf8) {
  224. print("XML Data: \(xmlString)")
  225. completion(xmlString)
  226. }
  227. }
  228. }
  229. task.resume()
  230. } else {
  231. print("Invalid URL")
  232. completion("")
  233. }
  234. }
  235. func dealXML(content: String) -> String {
  236. //
  237. // 1. 定义 XML 内容,包括版本号
  238. // let xmlContent = """
  239. // <root>
  240. // <resources>
  241. // <resource>
  242. // <maxVersion>1.3.0</maxVersion>
  243. // <minVersion>1.1.0</minVersion>
  244. // <resourceURL>https://www.pdfreaderpro.com/downloads/DocumentAI.bundle.1.0.0.zip</resourceURL>
  245. // </resource>
  246. // <resource>
  247. // <maxVersion>2.0.0</maxVersion>
  248. // <minVersion>1.4.0</minVersion>
  249. // <resourceURL>https://www.pdfreaderpro.com/downloads/DocumentAI.bundle.1.2.0.zip</resourceURL>
  250. // </resource>
  251. // <resource>
  252. // <maxVersion>5.0.0</maxVersion>
  253. // <minVersion>1.4.1</minVersion>
  254. // <bundleIdentifier>com.brother.pdfreaderprobeta.mac</bundleIdentifier>
  255. // <resourceURL>https://www.pdfreaderpro.com/downloads/DocumentAI.bundle.1.3.0.zip</resourceURL>
  256. // </resource>
  257. // </resources>
  258. // </root>
  259. // """
  260. let xmlContent = content
  261. let parser = ResourceParser()
  262. let resources = parser.parse(data: xmlContent.data(using: .utf8)!)
  263. // 从捆绑包的 Info.plist 文件中获取版本号
  264. var appVersion = "1.0.0"
  265. if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String {
  266. appVersion = version
  267. print("应用程序版本号:\(appVersion)")
  268. } else {
  269. print("无法获取应用程序版本号。")
  270. }
  271. if let resourceURL = shouldDownloadResource(for: appVersion, resources: resources) {
  272. if let local = self.getDownloadedResourcePath(for: kLocalFilePath) {
  273. if local != resourceURL {
  274. //如果本地存在两个地址不相同 则下载
  275. print("No suitable resource found for the current version.")
  276. return resourceURL
  277. } else {
  278. //如果本地存在 两个地址相同 则不下载
  279. print("No suitable resource found for the current version.")
  280. return ""
  281. }
  282. } else {
  283. //如果本地不存在 则下载
  284. print("Download resource from: \(resourceURL)")
  285. return resourceURL
  286. }
  287. } else {
  288. //如果找不到下载链接 则不下载
  289. print("No suitable resource found for the current version.")
  290. return ""
  291. }
  292. }
  293. func shouldDownloadResource(for currentVersion: String, resources: [Resource]) -> String? {
  294. for resource in resources {
  295. if resource.bundleIdentifier == MainBundle.bundleIdentifier ?? kBundleIdentifier {
  296. if isVersion(currentVersion, between: resource.minVersion, and: resource.maxVersion) {
  297. return resource.resourceURL
  298. }
  299. }
  300. }
  301. for resource in resources {
  302. if isVersion(currentVersion, between: resource.minVersion, and: resource.maxVersion) {
  303. return resource.resourceURL
  304. }
  305. }
  306. return nil
  307. }
  308. func isVersion(_ version: String, between minVersion: String, and maxVersion: String) -> Bool {
  309. guard let current = Version(version),
  310. let min = Version(minVersion),
  311. let max = Version(maxVersion) else {
  312. return false
  313. }
  314. return current >= min && current <= max
  315. }
  316. func saveDownloadedResource(resourceURL: String, localPath: String) {
  317. var resources = UserDefaults.standard.dictionary(forKey: "downloadedResources") as? [String: String] ?? [:]
  318. resources[localPath] = resourceURL
  319. UserDefaults.standard.set(resources, forKey: "downloadedResources")
  320. }
  321. func getDownloadedResourcePath(for resourceURL: String) -> String? {
  322. let resources = UserDefaults.standard.dictionary(forKey: "downloadedResources") as? [String: String]
  323. return resources?[resourceURL]
  324. }
  325. func removeDownloadResourcePath() {
  326. UserDefaults.standard.removeObject(forKey: "downloadedResources")
  327. }
  328. func validateResource(for resourceURL: String) -> Bool {
  329. if let localPath = getDownloadedResourcePath(for: resourceURL) {
  330. return FileManager.default.fileExists(atPath: localPath)
  331. }
  332. return false
  333. }
  334. }
  335. extension KMResourceDownloadManager {
  336. //MARK: 解压
  337. func unzipFramework(at zipURL: URL, to destinationPath: String) {
  338. let fileManager = FileManager.default
  339. var success = false
  340. if zipURL.pathExtension == "zip" {
  341. success = SSZipArchive.unzipFile(atPath: zipURL.path, toDestination: destinationPath)
  342. } else {
  343. // 如果是其他类型的压缩文件,可以使用其他解压库
  344. // success = YourCustomUnzipLibrary.unzipFile(atPath: zipURL.path, toDestination: destinationPath)
  345. }
  346. if success {
  347. print("File unzipped successfully!")
  348. try? fileManager.removeItem(at: zipURL)
  349. } else {
  350. print("Failed to unzip file.")
  351. dealDownloadResult(isSuccess: false, state: .unzipFailed)
  352. }
  353. }
  354. }
  355. extension KMResourceDownloadManager: URLSessionDelegate, URLSessionDownloadDelegate {
  356. //MARK: 网络下载
  357. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
  358. let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
  359. print("Download progress: \(progress)")
  360. progressBlock?(progress)
  361. }
  362. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
  363. let destinationPath = kResourcePath
  364. let fileManager = FileManager.default
  365. let destinationURL = URL(fileURLWithPath: destinationPath).appendingPathComponent("DocumentAI.bundle.zip")
  366. do {
  367. try fileManager.moveItem(at: location, to: destinationURL)
  368. print("Framework downloaded and installed successfully!")
  369. unzipFramework(at: destinationURL, to: destinationPath)
  370. dealDownloadResult(isSuccess: true, state: .success)
  371. if let sourceURL = downloadTask.originalRequest?.url {
  372. saveDownloadedResource(resourceURL: sourceURL.absoluteString, localPath: kLocalFilePath)
  373. }
  374. } catch {
  375. print("Failed to move framework: \(error)")
  376. dealDownloadResult(isSuccess: false, state: .moveFailed)
  377. }
  378. }
  379. }
  380. struct Resource {
  381. let maxVersion: String
  382. let minVersion: String
  383. let resourceURL: String
  384. let bundleIdentifier: String
  385. }
  386. // 5. 定义一个 XML 解析器的代理类
  387. class ResourceParser: NSObject, XMLParserDelegate {
  388. private var resources: [Resource] = []
  389. private var currentElement = ""
  390. var currentMaxVersion = ""
  391. var currentMinVersion = ""
  392. var currentResourceURL = ""
  393. var currentBundleIdentifier = ""
  394. func parse(data: Data) -> [Resource] {
  395. let parser = XMLParser(data: data)
  396. parser.delegate = self
  397. parser.parse()
  398. return resources
  399. }
  400. func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
  401. currentElement = elementName
  402. }
  403. func parser(_ parser: XMLParser, foundCharacters string: String) {
  404. switch currentElement {
  405. case "maxVersion":
  406. currentMaxVersion += string
  407. case "minVersion":
  408. currentMinVersion += string
  409. case "resourceURL":
  410. currentResourceURL += string
  411. case "bundleIdentifier":
  412. currentBundleIdentifier += string
  413. default:
  414. break
  415. }
  416. }
  417. func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
  418. if elementName == "resource" {
  419. resources.append(
  420. Resource(
  421. maxVersion: currentMaxVersion.trimmingCharacters(in: .whitespacesAndNewlines),
  422. minVersion: currentMinVersion.trimmingCharacters(in: .whitespacesAndNewlines),
  423. resourceURL: currentResourceURL.trimmingCharacters(in: .whitespacesAndNewlines),
  424. bundleIdentifier: currentBundleIdentifier.trimmingCharacters(in: .whitespacesAndNewlines)
  425. )
  426. )
  427. currentMaxVersion = ""
  428. currentMinVersion = ""
  429. currentResourceURL = ""
  430. currentBundleIdentifier = ""
  431. }
  432. }
  433. }