O2CloudFileManager.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. // v3 网盘
  22. private let cFileV3API = { return OOMoyaProvider<OOCloudFileV3API>() }()
  23. // 是否使用v3版本的api 就是x_pan_assemble_control模块
  24. private let useV3Api: Bool = {
  25. let value = StandDefaultUtil.share.userDefaultGetValue(key: O2.O2CloudFileVersionKey) as? Bool
  26. return value == true
  27. }()
  28. // MARK: - 工具服务 获取url 本地文件夹路径等等
  29. //本地文件存储路径
  30. func cloudFileV3LocalPath(file: OOAttachmentV3) -> URL {
  31. let fileName = "\(file.name!).\(file.`extension`!)"
  32. if let id = file.id {
  33. return O2.cloudFileLocalFolder()
  34. .appendingPathComponent(id)
  35. .appendingPathComponent(fileName)
  36. }
  37. return O2.cloudFileLocalFolder()
  38. .appendingPathComponent(fileName)
  39. }
  40. //本地文件存储路径
  41. func cloudFileLocalPath(file: OOAttachment) -> URL {
  42. let fileName = "\(file.name!).\(file.`extension`!)"
  43. if let id = file.id {
  44. return O2.cloudFileLocalFolder()
  45. .appendingPathComponent(id)
  46. .appendingPathComponent(fileName)
  47. }
  48. return O2.cloudFileLocalFolder()
  49. .appendingPathComponent(fileName)
  50. }
  51. //获取图片地址 根据传入的大小进行比例缩放
  52. func scaleImageUrl(id: String, width: Int = 200, height: Int = 200) -> String {
  53. // let model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  54. var model: O2APIServerModel?
  55. if useV3Api {
  56. model = O2AuthSDK.shared.o2APIServer(context: .x_pan_assemble_control)
  57. } else {
  58. model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  59. }
  60. var baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)\(model?.context ?? "")"
  61. if let trueUrl = O2AuthSDK.shared.bindUnitTransferUrl2Mapping(url: baseURLString) {
  62. baseURLString = trueUrl
  63. }
  64. return baseURLString + "/jaxrs/attachment2/\(id)/download/image/width/\(width)/height/\(height)"
  65. }
  66. //获取源文件下载地址
  67. func originFileUrl(id: String) -> String {
  68. // let model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  69. var model: O2APIServerModel?
  70. if useV3Api {
  71. model = O2AuthSDK.shared.o2APIServer(context: .x_pan_assemble_control)
  72. } else {
  73. model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  74. }
  75. var baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)\(model?.context ?? "")"
  76. if let trueUrl = O2AuthSDK.shared.bindUnitTransferUrl2Mapping(url: baseURLString) {
  77. baseURLString = trueUrl
  78. }
  79. return baseURLString + "/jaxrs/attachment2/\(id)/download"
  80. }
  81. // MARK: - 文件获取相关操作
  82. // 下载的是attachment3的文件
  83. func getFileUrlAttachment3(fileId: String) -> Promise<URL> {
  84. return Promise { fulfill, reject in
  85. self.getFileURLFromDB(id: fileId).then({ (path) in
  86. fulfill(path)
  87. }).catch({ (error) in
  88. DDLogInfo("本地没有这个文件,去服务器获取一次")
  89. DDLogError(error.localizedDescription)
  90. self.getFileURLFromDownloadAttachment3(fileId: fileId).then({ (path) in
  91. fulfill(path)
  92. }).catch({ (err) in
  93. reject(err)
  94. })
  95. })
  96. }
  97. }
  98. // 下载的是attachment2的文件
  99. func getFileUrl(fileId: String) -> Promise<URL> {
  100. return Promise { fulfill, reject in
  101. self.getFileURLFromDB(id: fileId).then({ (path) in
  102. fulfill(path)
  103. }).catch({ (error) in
  104. DDLogInfo("本地没有这个文件,去服务器获取一次")
  105. DDLogError(error.localizedDescription)
  106. self.getFileURLFromDownload(fileId: fileId).then({ (path) in
  107. fulfill(path)
  108. }).catch({ (err) in
  109. reject(err)
  110. })
  111. })
  112. }
  113. }
  114. //获取图片 先从本地文件查找 没找到再从网络下载
  115. func getImage(imageId: String) -> Promise<UIImage> {
  116. return Promise {fulfill, reject in
  117. self.getFileURLFromDB(id: imageId).then({ (path) in
  118. if let image = UIImage(contentsOfFile: path.path) {
  119. fulfill(image)
  120. }else {
  121. DDLogError("没有找到本地文件。。。。\(path.path)")
  122. reject(O2DBError.EmptyResultError)
  123. }
  124. }).catch({ error in
  125. DDLogError(error.localizedDescription)
  126. self.getFileURLFromDownload(fileId: imageId).then({ (path) in
  127. if let image = UIImage(contentsOfFile: path.path) {
  128. fulfill(image)
  129. }else {
  130. DDLogError("没有找到本地文件。。。。\(path.path)")
  131. reject(O2DBError.EmptyResultError)
  132. }
  133. }).catch({ (err) in
  134. reject(err)
  135. })
  136. })
  137. }
  138. }
  139. // MARK: - private method
  140. // attachment3 下载文件
  141. private func getFileURLFromDownloadAttachment3(fileId: String) -> Promise<URL> {
  142. return Promise {fulfill, reject in
  143. //本地没有 去网络下载
  144. self.downloadFile3(id: fileId).then({ (file) -> Promise<O2CloudFileInfo> in
  145. return self.storageFile2DBAttachment3(file: file)
  146. }).then({ (dbFile) in
  147. if let filePath = dbFile.filePath, !filePath.isBlank {
  148. DDLogDebug("查询到数据 文件路径:\(filePath)")
  149. let url = O2.cloudFileLocalFolder().appendingPathComponent(filePath)
  150. fulfill(url)
  151. }else {
  152. reject(O2DBError.EmptyResultError)
  153. }
  154. }).catch({error in
  155. reject(error)
  156. })
  157. }
  158. }
  159. //下载文件 存储本地数据 返回本地文件路径
  160. private func getFileURLFromDownload(fileId: String) -> Promise<URL> {
  161. return Promise {fulfill, reject in
  162. //本地没有 去网络下载
  163. self.downdloadFile(id: fileId).then({ (file) -> Promise<O2CloudFileInfo> in
  164. return self.storageFile2DB(file: file)
  165. }).then({ (dbFile) in
  166. if let filePath = dbFile.filePath, !filePath.isBlank {
  167. DDLogDebug("查询到数据 文件路径:\(filePath)")
  168. let url = O2.cloudFileLocalFolder().appendingPathComponent(filePath)
  169. fulfill(url)
  170. }else {
  171. reject(O2DBError.EmptyResultError)
  172. }
  173. }).catch({error in
  174. reject(error)
  175. })
  176. }
  177. }
  178. //从数据库获取 本地文件路径
  179. private func getFileURLFromDB(id: String) -> Promise<URL> {
  180. return Promise { fulfill, reject in
  181. DBManager.shared.queryCloudFile(fileId: id).then({ (dbFile) in
  182. if let filePath = dbFile.filePath, !filePath.isBlank {
  183. DDLogDebug("查询到数据 文件路径:\(filePath)")
  184. let url = O2.cloudFileLocalFolder().appendingPathComponent(filePath)
  185. fulfill(url)
  186. }else {
  187. reject(O2DBError.EmptyResultError)
  188. }
  189. }).catch({ (error) in
  190. reject(error)
  191. })
  192. }
  193. }
  194. //存储附件对象到数据库
  195. private func storageFile2DB(file: OOAttachment) -> Promise<O2CloudFileInfo> {
  196. return Promise { fulfill, reject in
  197. let info = O2CloudFileInfo()
  198. info.fileId = file.id!
  199. info.fileName = file.name!
  200. let fileName = "\(file.name!).\(file.`extension`!)"
  201. let path = "\(file.id!)/\(fileName)"
  202. DDLogDebug("保存数据库 path:\(path)")
  203. info.filePath = path
  204. info.fileExt = file.`extension` ?? ""
  205. DBManager.shared.insertCloudFile(info: info).then({ (result) in
  206. if result {
  207. fulfill(info)
  208. }else {
  209. reject(O2DBError.UnkownError)
  210. }
  211. }).catch({ (error) in
  212. DDLogError(error.localizedDescription)
  213. reject(error)
  214. })
  215. }
  216. }
  217. //存储附件对象到数据库
  218. private func storageFile2DBAttachment3(file: OOAttachmentV3) -> Promise<O2CloudFileInfo> {
  219. return Promise { fulfill, reject in
  220. let info = O2CloudFileInfo()
  221. info.fileId = file.id!
  222. info.fileName = file.name!
  223. let fileName = "\(file.name!).\(file.`extension`!)"
  224. let path = "\(file.id!)/\(fileName)"
  225. DDLogDebug("保存数据库 path:\(path)")
  226. info.filePath = path
  227. info.fileExt = file.`extension` ?? ""
  228. DBManager.shared.insertCloudFile(info: info).then({ (result) in
  229. if result {
  230. fulfill(info)
  231. }else {
  232. reject(O2DBError.UnkownError)
  233. }
  234. }).catch({ (error) in
  235. DDLogError(error.localizedDescription)
  236. reject(error)
  237. })
  238. }
  239. }
  240. //网络下载附件
  241. private func downdloadFile(id: String) -> Promise<OOAttachment>{
  242. return Promise { fulfill, reject in
  243. if self.useV3Api {
  244. self.cFileV3API.request(.getFile(id)) { (result) in
  245. let response = OOResult<BaseModelClass<OOAttachment>>(result)
  246. if response.isResultSuccess() {
  247. if let file = response.model?.data {
  248. self.cFileV3API.request(.downloadFile(file), completion: { (downloadResult) in
  249. switch downloadResult {
  250. case .success(_):
  251. //下载文件成功 返回附件对象 需要附件的地方根据固定的文件位置去查找
  252. fulfill(file)
  253. break
  254. case .failure(let err):
  255. reject(err)
  256. break
  257. }
  258. })
  259. }else {
  260. reject(O2APIError.o2ResponseError("没有查询到附件对象, id: \(id)"))
  261. }
  262. }else {
  263. reject(response.error!)
  264. }
  265. }
  266. } else {
  267. self.cloudFileApi.request(.getFile(id)) { (result) in
  268. let response = OOResult<BaseModelClass<OOAttachment>>(result)
  269. if response.isResultSuccess() {
  270. if let file = response.model?.data {
  271. self.cloudFileApi.request(.downloadFile(file), completion: { (downloadResult) in
  272. switch downloadResult {
  273. case .success(_):
  274. //下载文件成功 返回附件对象 需要附件的地方根据固定的文件位置去查找
  275. fulfill(file)
  276. break
  277. case .failure(let err):
  278. reject(err)
  279. break
  280. }
  281. })
  282. }else {
  283. reject(O2APIError.o2ResponseError("没有查询到附件对象, id: \(id)"))
  284. }
  285. }else {
  286. reject(response.error!)
  287. }
  288. }
  289. }
  290. }
  291. }
  292. private func downloadFile3(id: String) -> Promise<OOAttachmentV3> {
  293. return Promise { fulfill, reject in
  294. self.cFileV3API.request(.getFileV3(id)) { (result) in
  295. let response = OOResult<BaseModelClass<OOAttachmentV3>>(result)
  296. if response.isResultSuccess() {
  297. if let file = response.model?.data {
  298. self.cFileV3API.request(.downloadFileV3(file), completion: { (downloadResult) in
  299. switch downloadResult {
  300. case .success(_):
  301. //下载文件成功 返回附件对象 需要附件的地方根据固定的文件位置去查找
  302. fulfill(file)
  303. break
  304. case .failure(let err):
  305. reject(err)
  306. break
  307. }
  308. })
  309. }else {
  310. reject(O2APIError.o2ResponseError("没有查询到附件对象, id: \(id)"))
  311. }
  312. }else {
  313. reject(response.error!)
  314. }
  315. }
  316. }
  317. }
  318. }