KMMainDocument.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. //
  2. // KMMainDocument.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/6.
  6. //
  7. import Cocoa
  8. typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> ()
  9. @objcMembers class KMMainDocument: CTTabContents {
  10. var mainViewController: KMMainViewController?
  11. var homeWindowController: KMHomeWindowController?
  12. var homeViewController: KMHomeViewController?
  13. var isNewCreated: Bool = false
  14. var closedByUserGestureFlag: Bool = false // 标记 closedByUserGesture 这个状态需要延后存储(如果需要)
  15. var cloud: Bool = false
  16. var cloudUploadHanddler: KMMainDocumentCloudUploadHanddler?
  17. var isUnlockFromKeychain: Bool = false
  18. private var _saveAsing = false
  19. private var _saveToURL: URL?
  20. var saveToURL: URL? {
  21. get {
  22. return self._saveToURL
  23. }
  24. }
  25. weak var watermarkSaveDelegate: AnyObject?
  26. override func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, delegate: Any?, didSave didSaveSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
  27. if (self.isNewCreated) {
  28. // if let data = self.mainViewController, !data.isPDFDocumentEdited && !data.needSave && !self.isDocumentEdited {
  29. self._km_save(to: url, ofType: typeName, for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  30. return
  31. // }
  32. }
  33. if (!self.needSaveWatermark()) {
  34. self._km_save(to: url, ofType: typeName, for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  35. return
  36. }
  37. var openAccessoryView = self.watermarkSaveDelegate != nil
  38. if (openAccessoryView) {
  39. if let _browser = self.watermarkSaveDelegate as? KMBrowser, _browser.isCloseAllTabViewItem {
  40. openAccessoryView = false
  41. }
  42. }
  43. self._km_saveForWatermark(openAccessoryView: openAccessoryView) { [unowned self] needSave, params in
  44. if (needSave) {
  45. self._km_save(to: url, ofType: typeName, for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  46. } else { // 水印保存
  47. if (self.watermarkSaveDelegate == nil) {
  48. if let data = params.first as? KMResult, data == .cancel {
  49. if let shouldClose = params.last as? Bool, shouldClose {
  50. DispatchQueue.main.async {
  51. self.mainViewController?.browserWindowController?.browser.windowDidBeginToClose()
  52. }
  53. }
  54. } else {
  55. DispatchQueue.main.async {
  56. self.mainViewController?.browserWindowController?.browser.windowDidBeginToClose()
  57. }
  58. }
  59. return
  60. }
  61. if let data = params.first as? KMResult, data == .cancel {
  62. if var shouldClose = params.last as? Bool {
  63. if let _browser = self.watermarkSaveDelegate as? KMBrowser, _browser.isCloseAllTabViewItem {
  64. shouldClose = true
  65. }
  66. (self.watermarkSaveDelegate as? KMBrowser)?.document(self, shouldClose: shouldClose, contextInfo: nil)
  67. }
  68. } else {
  69. (self.watermarkSaveDelegate as? KMBrowser)?.document(self, shouldClose: true, contextInfo: nil)
  70. }
  71. self.watermarkSaveDelegate = nil
  72. }
  73. }
  74. }
  75. override func makeWindowControllers() {
  76. // Returns the storyboard that contains your document window.
  77. if ((self.fileURL?.path) != nil) {
  78. if !self.fileURL!.path.isPDFValid() {
  79. let alert = NSAlert()
  80. alert.alertStyle = .critical
  81. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  82. alert.runModal()
  83. return
  84. }
  85. }
  86. let mainWindow = NSApp.mainWindow
  87. var currentWindowController: KMBrowserWindowController?
  88. if mainWindow != nil {
  89. let windowController = mainWindow!.windowController
  90. if windowController is KMBrowserWindowController {
  91. currentWindowController = (windowController as! KMBrowserWindowController)
  92. } else {
  93. for window in NSApp.windows {
  94. let windowController = window.windowController
  95. if windowController is KMBrowserWindowController {
  96. currentWindowController = (windowController as! KMBrowserWindowController)
  97. break
  98. }
  99. }
  100. }
  101. } else {
  102. for window in NSApp.windows {
  103. let windowController = window.windowController
  104. if windowController is KMBrowserWindowController {
  105. currentWindowController = (windowController as! KMBrowserWindowController)
  106. break
  107. }
  108. }
  109. }
  110. if (currentWindowController == nil) && (self.fileURL != nil) {
  111. let browser = KMBrowser.init() as KMBrowser
  112. browser.addHomeTabContents()
  113. browser.windowController = KMBrowserWindowController.init(browser: browser)
  114. currentWindowController = browser.windowController as? KMBrowserWindowController
  115. }
  116. mainViewController = KMMainViewController.init()
  117. mainViewController?.myDocument = self
  118. if ((self.fileURL?.path) != nil) {
  119. let pdfDocument = CPDFDocument.init(url: URL(fileURLWithPath: self.fileURL!.path))
  120. mainViewController?.document = pdfDocument
  121. }
  122. self.view = mainViewController?.view
  123. if currentWindowController != nil {
  124. if currentWindowController?.browser != nil {
  125. // currentWindowController?.browser.add(self, at: Int32()-1, inForeground: true)
  126. // self.addWindowController(currentWindowController!)
  127. // mainViewController.browserWindowController = currentWindowController
  128. let activeBrowser = (currentWindowController?.browser.activeTabContents())! as CTTabContents
  129. let activeIndex = Int((currentWindowController?.browser.activeTabIndex())!)
  130. self.addWindowController(currentWindowController!)
  131. self.mainViewController?.browserWindowController = currentWindowController
  132. let ishome = activeBrowser.isHome as Bool
  133. let isfirstTab = (activeIndex == 0)
  134. if ishome && !isfirstTab {
  135. // 替换 document
  136. currentWindowController?.browser.replaceTabContents(at: Int32(activeIndex), with: self)
  137. // 刷新标签
  138. currentWindowController?.browser.updateTabState(at: Int32(activeIndex))
  139. // 刷新 home icon
  140. if let tabStripController = currentWindowController?.tabStripController {
  141. if let view = tabStripController.view(at: UInt(activeIndex)) as? CTTabView {
  142. view.controller().isHome = self.isHome
  143. view.controller().isNewTab = self.isNewTab
  144. view.controller().updateUI()
  145. }
  146. }
  147. } else {
  148. currentWindowController?.browser.add(self, at: Int32()-1, inForeground: true)
  149. }
  150. }
  151. }
  152. }
  153. override func showWindows() {
  154. super.showWindows()
  155. self.setDataFromTmpData()
  156. }
  157. override func windowControllerDidLoadNib(_ aController: NSWindowController) {
  158. super.windowControllerDidLoadNib(aController)
  159. self.setDataFromTmpData()
  160. }
  161. override func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType) async throws {
  162. do {
  163. try await super.save(to: url, ofType: typeName, for: saveOperation)
  164. } catch let outError {
  165. Swift.print(outError)
  166. }
  167. }
  168. override func write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws {
  169. try self._km_write(to: url, ofType: typeName, for: saveOperation, originalContentsURL: absoluteOriginalContentsURL)
  170. }
  171. override func canClose(withDelegate delegate: Any, shouldClose shouldCloseSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
  172. let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
  173. if (isPrompt) {
  174. super.canClose(withDelegate: delegate, shouldClose: shouldCloseSelector, contextInfo: contextInfo)
  175. return
  176. }
  177. if (self.isNewCreated) {
  178. self.save(nil)
  179. } else if (self.isDocumentEdited) {
  180. self.save(nil)
  181. } else if (mainViewController != nil) {
  182. if self.mainViewController!.isPDFDocumentEdited || self.mainViewController!.needSave {
  183. self.save(nil)
  184. }
  185. }
  186. super.canClose(withDelegate: delegate, shouldClose: shouldCloseSelector, contextInfo: contextInfo)
  187. }
  188. override func saveAs(_ sender: Any?) {
  189. if (!self.needSaveWatermark()) {
  190. self._km_saveAs(sender)
  191. return
  192. }
  193. self._km_saveForWatermark { [unowned self] needSave, _ in
  194. if (needSave) {
  195. self._km_saveAs(sender)
  196. }
  197. }
  198. }
  199. override func runModalSavePanel(for saveOperation: NSDocument.SaveOperationType, delegate: Any?, didSave didSaveSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
  200. if (self.isNewCreated) {
  201. // if let data = self.mainViewController, !data.isPDFDocumentEdited && !data.needSave && !self.isDocumentEdited {
  202. self._km_runModalSavePanel(for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  203. return
  204. // }
  205. }
  206. if (!self.needSaveWatermark()) {
  207. self._km_runModalSavePanel(for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  208. return
  209. }
  210. self._km_saveForWatermark { [unowned self] needSave, _ in
  211. if (needSave) {
  212. self._km_runModalSavePanel(for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  213. }
  214. }
  215. }
  216. override func save(_ sender: Any?) {
  217. if (!self.needSaveWatermark()) {
  218. self._km_save(sender)
  219. return
  220. }
  221. self._km_saveForWatermark { [unowned self] needSave, _ in
  222. if (needSave) {
  223. self._km_save(sender)
  224. }
  225. }
  226. }
  227. override func read(from absoluteURL: URL, ofType typeName: String) throws {
  228. do {
  229. try super.read(from: absoluteURL, ofType: typeName)
  230. updateChangeCount(.changeCleared)
  231. } catch let outError {
  232. Swift.print(outError)
  233. }
  234. }
  235. override func read(from data: Data, ofType typeName: String) throws {
  236. // Insert code here to read your document from the given data of the specified type, throwing an error in case of failure.
  237. // Alternatively, you could remove this method and override read(from:ofType:) instead. If you do, you should also override isEntireFileLoaded to return false if the contents are lazily loaded.
  238. let pdfDocument = CPDFDocument.init(data: data)
  239. if pdfDocument == nil {
  240. throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
  241. }
  242. }
  243. // MARK: Autosaving
  244. override func close() {
  245. if self.isActive {
  246. if browser != nil {
  247. var activeIndex = 0
  248. let dex = browser.index(of: self)
  249. if dex == browser.tabCount() - 1 {
  250. activeIndex = Int(browser.tabCount()-2)
  251. } else {
  252. activeIndex = Int(dex + 1)
  253. }
  254. let activeContents = browser.tabContents(at: Int32(activeIndex))
  255. activeContents?.addWindowController(browser.windowController)
  256. }
  257. }
  258. super.close()
  259. }
  260. // MARK: init
  261. override init() {
  262. super.init()
  263. // Add your subclass-specific initialization here.
  264. NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFListViewAnnotationsAttributeHasChangeNotification"), object: nil)
  265. // NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFViewDocumentChangedNotification"), object: nil)
  266. // NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFViewPageChangedNotification"), object: nil)
  267. NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFListViewDidAddAnnotationNotification"), object: nil)
  268. NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFListViewDidRemoveAnnotationNotification"), object: nil)
  269. }
  270. override init?(baseTabContents baseContents: CTTabContents?) {
  271. super.init(baseTabContents: baseContents)
  272. if isHome {
  273. homeViewController = KMHomeViewController.init()
  274. homeViewController?.myDocument = self
  275. self.view = homeViewController?.view
  276. }
  277. }
  278. // MARK: Handling User Actions
  279. override var title: String? {
  280. get {
  281. if isHome {
  282. if (self.isNewTab) {
  283. return NSLocalizedString("New Tab", comment: "")
  284. } else {
  285. return NSLocalizedString("Home", comment: "")
  286. }
  287. } else {
  288. return fileURL?.lastPathComponent
  289. }
  290. }
  291. set {
  292. super.title = newValue
  293. }
  294. }
  295. func needSaveWatermark() -> Bool {
  296. if let need = self.mainViewController?.saveWatermarkFlag {
  297. return need
  298. }
  299. return false
  300. }
  301. // MARK: Private Methods
  302. func pdfChangedNotification(_ notification: Notification) -> Void {
  303. if !isHome {
  304. let mainViewController = mainViewController
  305. var document: CPDFDocument!
  306. let dic = notification.object as? NSDictionary
  307. if dic?["object"] is CPDFAnnotation {
  308. let annotation : CPDFAnnotation = dic?["object"] as? CPDFAnnotation ?? CPDFAnnotation()
  309. document = annotation.page.document
  310. } else if dic?["object"] is CPDFListView {
  311. let pdflistView = notification.object as? CPDFListView
  312. document = pdflistView?.document
  313. }
  314. if mainViewController != nil {
  315. if document == mainViewController!.document {
  316. updateChangeCount(.changeDone)
  317. }
  318. }
  319. }
  320. }
  321. override func updateChangeCount(_ change: NSDocument.ChangeType) {
  322. let mainWindow = NSApp.mainWindow
  323. var currentWindowController: KMBrowserWindowController?
  324. if mainWindow != nil {
  325. let windowController = mainWindow!.windowController
  326. if windowController is KMBrowserWindowController {
  327. currentWindowController = (windowController as! KMBrowserWindowController)
  328. } else {
  329. for window in NSApp.windows {
  330. let windowController = window.windowController
  331. if windowController is KMBrowserWindowController {
  332. currentWindowController = (windowController as! KMBrowserWindowController)
  333. break
  334. }
  335. }
  336. }
  337. } else {
  338. for window in NSApp.windows {
  339. let windowController = window.windowController
  340. if windowController is KMBrowserWindowController {
  341. currentWindowController = (windowController as! KMBrowserWindowController)
  342. break
  343. }
  344. }
  345. }
  346. if currentWindowController != nil {
  347. if currentWindowController?.browser != nil {
  348. let activeBrowser = (currentWindowController?.browser.activeTabContents())! as CTTabContents
  349. let activeIndex = Int((currentWindowController?.browser.activeTabIndex())!)
  350. if self == activeBrowser {
  351. super.updateChangeCount(change)
  352. return
  353. }
  354. }
  355. }
  356. super.updateChangeCount(.changeCleared)
  357. }
  358. func uploadToCloud(_ callback: (@escaping(Bool, String)->())) {
  359. guard let handdler = self.cloudUploadHanddler else {
  360. return
  361. }
  362. handdler(callback)
  363. }
  364. func isPDFDocument() -> Bool {
  365. return true
  366. }
  367. func setDataFromTmpData() {
  368. guard let _document = self.mainViewController?.document else {
  369. return
  370. }
  371. // self.tryToUnlockDocument(document!)
  372. if (_document.permissionsStatus != .owner) {
  373. var password: NSString? = nil
  374. let fileId = self.fileId(for: _document)
  375. if (fileId.isEmpty) {
  376. return
  377. }
  378. self.getPassword(&password, fileId: fileId)
  379. if (password != nil) {
  380. self.isUnlockFromKeychain = true
  381. // document.unlock(withPassword: password! as String)
  382. self.mainViewController?.password = password as String?
  383. }
  384. }
  385. }
  386. func tryToUnlockDocument(_ document: CPDFDocument) {
  387. if (document.permissionsStatus != .owner) {
  388. var password: NSString? = nil
  389. let fileId = self.fileId(for: document)
  390. if (fileId.isEmpty) {
  391. return
  392. }
  393. self.getPassword(&password, fileId: fileId)
  394. if (password != nil) {
  395. self.isUnlockFromKeychain = true
  396. document.unlock(withPassword: password! as String)
  397. }
  398. }
  399. }
  400. func km_updateChangeCount(_ change: NSDocument.ChangeType) {
  401. super.updateChangeCount(change)
  402. }
  403. private func _km_write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws {
  404. var success = true
  405. if !self.isHome {
  406. if mainViewController != nil {
  407. if mainViewController?.document != nil {
  408. self.mainViewController?.commitEditingIfNeed()
  409. // if mainViewController!.document!.isEncrypted {
  410. // success = mainViewController!.document!.write(to: url)
  411. // } else {
  412. if (mainViewController!.needSave) {
  413. if let options = self.mainViewController?.secureOptions, !options.isEmpty {
  414. success = self.mainViewController!.document!.write(to: url, withOptions: options)
  415. } else if let flag = self.mainViewController?.removeSecureFlag, flag {
  416. success = self.mainViewController!.document!.writeDecrypt(to: url)
  417. } else {
  418. success = mainViewController!.document!.write(to: url)
  419. }
  420. } else {
  421. success = mainViewController!.document!.write(to: url)
  422. }
  423. // }
  424. self.mainViewController?.needSave = false
  425. self.mainViewController?.clearSecureOptions()
  426. self.mainViewController?.clearRemoveSecureFlag()
  427. }
  428. }
  429. } else {
  430. success = false
  431. }
  432. if (success && self._saveAsing) {
  433. if let tabView = self.browser?.windowController?.tabStripController?.activeTabView() as? CTTabView {
  434. tabView.controller()?.title = url.lastPathComponent
  435. }
  436. self._saveAsing = false
  437. }
  438. if success && isNewCreated && NSDocument.SaveOperationType.saveAsOperation == saveOperation {
  439. isNewCreated = false
  440. }
  441. }
  442. private func _km_saveForWatermark(openAccessoryView: Bool = true, callback:@escaping (_ needSave: Bool, _ param: Any...)->Void) {
  443. Task { @MainActor in
  444. if await (KMLightMemberManager.manager.canPayFunction() == false) {
  445. let _ = KMSubscribeWaterMarkWindowController.show(window: NSApp.mainWindow!) { isSubscribeSuccess, isWaterMarkExport, isClose in
  446. if (isClose) {
  447. callback(false, KMResult.cancel, false)
  448. return
  449. }
  450. if (isSubscribeSuccess) {
  451. callback(true)
  452. return
  453. }
  454. if (isWaterMarkExport) {
  455. guard let _document = self.mainViewController?.document else {
  456. callback(false, KMResult.failure)
  457. return
  458. }
  459. // 提交文本编辑的内容
  460. self.mainViewController?.commitEditingIfNeed()
  461. DispatchQueue.main.async {
  462. NSPanel.savePanel(NSApp.mainWindow!, openAccessoryView, panel:{ panel in
  463. if (!self.isNewCreated) {
  464. panel.directoryURL = _document.documentURL.deletingLastPathComponent()
  465. }
  466. panel.nameFieldStringValue = _document.documentURL.lastPathComponent
  467. }) { response, url, isOpen in
  468. if (response == .cancel) {
  469. callback(false, KMResult.cancel, true)
  470. return
  471. }
  472. guard let _url = KMTools.saveWatermarkDocument(document: _document, to: url!, secureOptions: self.mainViewController?.secureOptions, removePWD: self.mainViewController!.removeSecureFlag) else {
  473. callback(false, KMResult.failure)
  474. return
  475. }
  476. callback(false, KMResult.success)
  477. if (isOpen) {
  478. NSDocumentController.shared.km_safe_openDocument(withContentsOf: _url, display: true) { _, _, _ in
  479. }
  480. } else {
  481. NSWorkspace.shared.activateFileViewerSelecting([_url])
  482. }
  483. }
  484. }
  485. return
  486. }
  487. callback(true)
  488. }
  489. return
  490. }
  491. callback(true)
  492. }
  493. }
  494. private func _km_save(_ sender: Any?) {
  495. super.save(sender)
  496. }
  497. private func _km_save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, delegate: Any?, didSave didSaveSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
  498. self._saveToURL = url
  499. super.save(to: url, ofType: typeName, for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  500. }
  501. private func _km_saveAs(_ sender: Any?) {
  502. super.saveAs(sender)
  503. self._saveAsing = true
  504. }
  505. private func _km_runModalSavePanel(for saveOperation: NSDocument.SaveOperationType, delegate: Any?, didSave didSaveSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
  506. super.runModalSavePanel(for: saveOperation, delegate: delegate, didSave: didSaveSelector, contextInfo: contextInfo)
  507. }
  508. }
  509. extension NSDocument {
  510. @objc class func isDamage(url: URL) -> Bool {
  511. // 文件路径是否存在
  512. if (FileManager.default.fileExists(atPath: url.path) == false) {
  513. return true
  514. }
  515. /// PDF 格式文件
  516. if (url.pathExtension.lowercased() == "pdf") {
  517. let document = PDFDocument(url: url)
  518. if (document == nil) {
  519. return true
  520. }
  521. if (document!.isLocked) { // 加锁文件不在这里判断
  522. return false
  523. }
  524. if (document!.pageCount <= 0) {
  525. return true
  526. }
  527. return false
  528. }
  529. // 支持的图片格式
  530. let imageExts = ["jpg","cur","bmp","jpeg","gif","png","tiff","tif","ico","icns","tga","psd","eps","hdr","jp2","jpc","pict","sgi","heic"]
  531. let isImage = imageExts.contains(url.pathExtension.lowercased())
  532. if (isImage == false) { // 其他格式目前返回没损坏,后续再补充(如果有需求)
  533. return false
  534. }
  535. // 图片格式
  536. let image = NSImage(contentsOf: url)
  537. let data = image?.tiffRepresentation
  538. if (data == nil) {
  539. return true
  540. }
  541. let imageRep = NSBitmapImageRep(data: data!)
  542. imageRep!.size = image!.size
  543. var imageData: NSData?
  544. if (url.pathExtension.lowercased() == "png") {
  545. imageData = imageRep?.representation(using: .png, properties: [:]) as NSData?
  546. } else {
  547. imageData = imageRep?.representation(using: .jpeg, properties: [:]) as NSData?
  548. }
  549. if (imageData == nil) {
  550. return true
  551. }
  552. let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  553. if (FileManager.default.fileExists(atPath: path!) == false) {
  554. try?FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: false)
  555. }
  556. var tagString: String = ""
  557. let dateFormatter = DateFormatter()
  558. dateFormatter.dateFormat = "yyMMddHHmmss"
  559. tagString.append(dateFormatter.string(from: Date()))
  560. tagString = tagString.appendingFormat("%04d", arc4random()%10000)
  561. let filePath = path?.appending("/\(tagString).png")
  562. if (imageData!.write(toFile: filePath!, atomically: true) == false) {
  563. return true
  564. }
  565. // 删除临时图片
  566. try?FileManager.default.removeItem(atPath: filePath!)
  567. return false
  568. }
  569. @objc class func isDamage(url: URL, needAlertIfDamage need: Bool) -> Bool {
  570. let result = self.isDamage(url: url)
  571. if (result == false) {
  572. return false
  573. }
  574. if (need == false) {
  575. return true
  576. }
  577. let alert = NSAlert()
  578. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  579. alert.runModal()
  580. return true
  581. }
  582. }
  583. // MARK: -
  584. // MARK: 保存密码
  585. extension NSDocument {
  586. func savePasswordInKeychain(_ password: String, _ document: CPDFDocument) {
  587. if (document.isLocked || password.isEmpty) {
  588. return
  589. }
  590. let fileId = self.fileId(for: document)
  591. if (fileId.isEmpty) {
  592. return
  593. }
  594. // let status: SKPasswordStatus =
  595. let label = "PDF Master: \(self.displayName!)"
  596. SKKeychain.setPassword(password, item: nil, forService: self.passwordServiceName(), account: fileId, label: label, comment: self.fileURL?.path)
  597. }
  598. func getPassword(_ password: AutoreleasingUnsafeMutablePointer<NSString?>, fileId: String) {
  599. let status = SKKeychain.getPassword(password, item: nil, forService: self.passwordServiceName(), account: fileId)
  600. // if (status == .found) {
  601. // }
  602. }
  603. fileprivate func fileId(for document: CPDFDocument) -> String {
  604. return "\(document.documentURL.path.hash)"
  605. }
  606. private func passwordServiceName() -> String {
  607. return "PDF Master password"
  608. }
  609. }