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