O2CloudFileManager.swift 7.3 KB

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