KMCloudDocumentTools.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // KMCloudDocumentTools.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/2/10.
  6. //
  7. import Cocoa
  8. class KMCloudDocumentTools: NSObject {
  9. class public func canOpenDocument(_ file: KMServicesCloudFile) -> Bool {
  10. if (file.filetype != .file) {
  11. return false
  12. }
  13. let fileName = file.fileName
  14. let exe: String = (fileName?.components(separatedBy: ".").last)!
  15. if (exe.lowercased() != "pdf") {
  16. return false
  17. }
  18. return true
  19. }
  20. /// 下载文档
  21. class public func downloadDocument(_ server: KMCloudServer, _ file: KMServicesCloudFile, callback: @escaping (Bool, URL)->()) {
  22. let localPath = self.getTempPath(file.fileName)
  23. if (localPath == nil) {
  24. return
  25. }
  26. // print(localPath)
  27. if (FileManager.default.fileExists(atPath: localPath!)) {
  28. try?FileManager.default.removeItem(atPath: localPath!)
  29. }
  30. server.downloadCloudPath(file, localPath: URL(fileURLWithPath: localPath!)) { cloudFile, progress in
  31. KMPrint(progress)
  32. } completion: { cloudFile, finished in
  33. callback(finished, URL(fileURLWithPath: localPath!))
  34. }
  35. }
  36. /// 上传文档
  37. class public func uploadDocument(_ server: KMCloudServer, _ file: KMServicesCloudFile, callback: @escaping (Bool, URL)->()) {
  38. let localPath = self.getTempPath(file.fileName)
  39. if (localPath == nil) {
  40. return
  41. }
  42. server.uploadCloudPath(file, localPath: URL(fileURLWithPath: localPath!)) { cloudFile, progress in
  43. } completion: { cloudFile, finished in
  44. callback(finished, URL(fileURLWithPath: localPath!))
  45. }
  46. }
  47. /// 获取临时路径
  48. class public func getTempPath(_ fileName: String) -> String? {
  49. let folderPath: String = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("cloudDocuments"))!
  50. if (!FileManager.default.fileExists(atPath: folderPath)) {
  51. let create: ()? = try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false)
  52. if (create == nil) {
  53. return nil
  54. }
  55. }
  56. return folderPath.stringByAppendingPathComponent(fileName)
  57. }
  58. class public func getFileType(_ server: KMCloudServer, _ file: KMServicesCloudFile) -> String {
  59. var fileType: String = file.fileName.components(separatedBy: ".").last!
  60. if (server.serverType == KMGoogleDrive) {
  61. if (file.mimeType! == "application/vnd.google-apps.presentation") {
  62. fileType = "pptx"
  63. } else if (file.mimeType == "application/vnd.google-apps.document") {
  64. fileType = "docx"
  65. } else if (file.mimeType == "application/vnd.google-apps.spreadsheet") {
  66. fileType = "xlsx"
  67. } else if (file.mimeType == "application/vnd.google-apps.form" || file.mimeType == "application/vnd.google-apps.site") {
  68. fileType = "pptx"
  69. } else if (file.mimeType == "application/vnd.google-apps.map") {
  70. fileType = "kmz"
  71. } else if (file.mimeType == "application/vnd.google-apps.drawing") {
  72. fileType = "jpg"
  73. }
  74. }
  75. return fileType
  76. }
  77. class public func parseDate(_ date: Date) -> String {
  78. let currentDate = Date()
  79. let calendar = Calendar.init(identifier: .gregorian)
  80. let hour = calendar.component(.hour, from: currentDate)
  81. let minute = calendar.component(.minute, from: currentDate)
  82. let second = calendar.component(.second, from: currentDate)
  83. let weekday = calendar.component(.weekday, from: currentDate)
  84. let weekdayOrdinal = calendar.component(.weekdayOrdinal, from: currentDate)
  85. if (hour * 60 * 60 + minute * 60 + second + Int(date.timeIntervalSinceNow) > 0) { /// 当天
  86. let dateFormatter = DateFormatter()
  87. dateFormatter.dateFormat = "hh:mm"
  88. return NSLocalizedString("Today", comment: "").appending(", ").appending(dateFormatter.string(from: date))
  89. }
  90. if (weekdayOrdinal * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second + Int(date.timeIntervalSinceNow) > 0) { /// 当周
  91. let dateFormatter = DateFormatter()
  92. dateFormatter.dateFormat = "hh:mm"
  93. // dateFormatter.locale = Locale(identifier: "NL")
  94. var weekdayString: String = ""
  95. if (weekdayOrdinal == 1) {
  96. weekdayString = "Mon"
  97. } else if (weekdayOrdinal == 2) {
  98. weekdayString = "Tue"
  99. } else if (weekdayOrdinal == 3) {
  100. weekdayString = "Wed"
  101. } else if (weekdayOrdinal == 4) {
  102. weekdayString = "Thu"
  103. } else if (weekdayOrdinal == 5) {
  104. weekdayString = "Fri"
  105. } else if (weekdayOrdinal == 6) {
  106. weekdayString = "Sat"
  107. } else if (weekdayOrdinal == 7) {
  108. weekdayString = "Sun"
  109. }
  110. return NSLocalizedString(weekdayString, comment: "").appending(", ").appending(dateFormatter.string(from: date))
  111. }
  112. let dateFormatter = DateFormatter()
  113. dateFormatter.dateFormat = "yyyy/MM/dd"
  114. return dateFormatter.string(from: date)
  115. }
  116. /// 获取标记
  117. class private func tagString() -> String {
  118. var result: String = ""
  119. let dateFormatter = DateFormatter()
  120. dateFormatter.dateFormat = "yyMMddHHmmss"
  121. result.append(dateFormatter.string(from: Date()))
  122. result = result.appendingFormat("%04d", arc4random()%10000)
  123. return result
  124. }
  125. }