O2CloudFileManager.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // O2CloudFileManager.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/30.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Moya
  10. import O2OA_Auth_SDK
  11. import Promises
  12. import CocoaLumberjack
  13. class O2CloudFileManager {
  14. static let shared: O2CloudFileManager = {
  15. return O2CloudFileManager()
  16. }()
  17. private init() {
  18. }
  19. private let cloudFileApi = {
  20. return OOMoyaProvider<OOCloudStorageAPI>()
  21. }()
  22. // MARK: - 工具服务 获取url 本地文件夹路径等等
  23. //文件存储目录
  24. func cloudFileLocalFolder() -> URL {
  25. let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  26. return documentsURL
  27. .appendingPathComponent("O2")
  28. .appendingPathComponent("cloud")
  29. }
  30. //本地文件存储路径
  31. func cloudFileLocalPath(file: OOAttachment) -> URL {
  32. let fileName = "\(file.name!).\(file.`extension`!)"
  33. if let id = file.id {
  34. return self.cloudFileLocalFolder()
  35. .appendingPathComponent(id)
  36. .appendingPathComponent(fileName)
  37. }
  38. return self.cloudFileLocalFolder()
  39. .appendingPathComponent(fileName)
  40. }
  41. //获取图片地址 根据传入的大小进行比例缩放
  42. func scaleImageUrl(id: String, width: Int = 200, height: Int = 200) -> String {
  43. let model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  44. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 0)\(model?.context ?? "")"
  45. return baseURLString + "/jaxrs/attachment2/\(id)/download/image/width/\(width)/height/\(height)"
  46. }
  47. //获取源文件下载地址
  48. func originFileUrl(id: String) -> String {
  49. let model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  50. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 0)\(model?.context ?? "")"
  51. return baseURLString + "/jaxrs/attachment2/\(id)/download"
  52. }
  53. // MARK: - 文件获取相关操作
  54. func getFileUrl(fileId: String) -> Promise<URL> {
  55. return Promise { fulfill, reject in
  56. self.getFileURLFromDB(id: fileId).then({ (path) in
  57. fulfill(path)
  58. }).catch({ (error) in
  59. DDLogInfo("本地没有这个文件,去服务器获取一次")
  60. DDLogError(error.localizedDescription)
  61. self.getFileURLFromDownload(fileId: fileId).then({ (path) in
  62. fulfill(path)
  63. }).catch({ (err) in
  64. reject(err)
  65. })
  66. })
  67. }
  68. }
  69. //获取图片 先从本地文件查找 没找到再从网络下载
  70. func getImage(imageId: String) -> Promise<UIImage> {
  71. return Promise {fulfill, reject in
  72. self.getFileURLFromDB(id: imageId).then({ (path) in
  73. if let image = UIImage(contentsOfFile: path.path) {
  74. fulfill(image)
  75. }else {
  76. DDLogError("没有找到本地文件。。。。\(path.path)")
  77. reject(O2DBError.EmptyResultError)
  78. }
  79. }).catch({ error in
  80. DDLogError(error.localizedDescription)
  81. self.getFileURLFromDownload(fileId: imageId).then({ (path) in
  82. if let image = UIImage(contentsOfFile: path.path) {
  83. fulfill(image)
  84. }else {
  85. DDLogError("没有找到本地文件。。。。\(path.path)")
  86. reject(O2DBError.EmptyResultError)
  87. }
  88. }).catch({ (err) in
  89. reject(err)
  90. })
  91. })
  92. }
  93. }
  94. // MARK: - private method
  95. //下载文件 存储本地数据 返回本地文件路径
  96. private func getFileURLFromDownload(fileId: String) -> Promise<URL> {
  97. return Promise {fulfill, reject in
  98. //本地没有 去网络下载
  99. self.downdloadFile(id: fileId).then({ (file) -> Promise<O2CloudFileInfo> in
  100. return self.storageFile2DB(file: file)
  101. }).then({ (dbFile) in
  102. if let filePath = dbFile.filePath, !filePath.isBlank {
  103. DDLogDebug("查询到数据 文件路径:\(filePath)")
  104. let url = self.cloudFileLocalFolder().appendingPathComponent(filePath)
  105. fulfill(url)
  106. }else {
  107. reject(O2DBError.EmptyResultError)
  108. }
  109. }).catch({error in
  110. reject(error)
  111. })
  112. }
  113. }
  114. //从数据库获取 本地文件路径
  115. private func getFileURLFromDB(id: String) -> Promise<URL> {
  116. return Promise { fulfill, reject in
  117. DBManager.shared.queryCloudFile(fileId: id).then({ (dbFile) in
  118. if let filePath = dbFile.filePath, !filePath.isBlank {
  119. DDLogDebug("查询到数据 文件路径:\(filePath)")
  120. let url = self.cloudFileLocalFolder().appendingPathComponent(filePath)
  121. fulfill(url)
  122. }else {
  123. reject(O2DBError.EmptyResultError)
  124. }
  125. }).catch({ (error) in
  126. reject(error)
  127. })
  128. }
  129. }
  130. //存储附件对象到数据库
  131. private func storageFile2DB(file: OOAttachment) -> Promise<O2CloudFileInfo> {
  132. return Promise { fulfill, reject in
  133. let info = O2CloudFileInfo()
  134. info.fileId = file.id!
  135. info.fileName = file.name!
  136. let fileName = "\(file.name!).\(file.`extension`!)"
  137. let path = "\(file.id!)/\(fileName)"
  138. DDLogDebug("保存数据库 path:\(path)")
  139. info.filePath = path
  140. info.fileExt = file.extension ?? ""
  141. DBManager.shared.insertCloudFile(info: info).then({ (result) in
  142. if result {
  143. fulfill(info)
  144. }else {
  145. reject(O2DBError.UnkownError)
  146. }
  147. }).catch({ (error) in
  148. DDLogError(error.localizedDescription)
  149. reject(error)
  150. })
  151. }
  152. }
  153. //网络下载附件
  154. private func downdloadFile(id: String) -> Promise<OOAttachment>{
  155. return Promise { fulfill, reject in
  156. self.cloudFileApi.request(.getFile(id)) { (result) in
  157. let response = OOResult<BaseModelClass<OOAttachment>>(result)
  158. if response.isResultSuccess() {
  159. if let file = response.model?.data {
  160. self.cloudFileApi.request(.downloadFile(file), completion: { (downloadResult) in
  161. switch downloadResult {
  162. case .success(_):
  163. //下载文件成功 返回附件对象 需要附件的地方根据固定的文件位置去查找
  164. fulfill(file)
  165. break
  166. case .failure(let err):
  167. reject(err)
  168. break
  169. }
  170. })
  171. }else {
  172. reject(O2APIError.o2ResponseError("没有查询到附件对象, id: \(id)"))
  173. }
  174. }else {
  175. reject(response.error!)
  176. }
  177. }
  178. }
  179. }
  180. }