OOCloudStorageAPI.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // OOCloudStorageAPI.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/12/7.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. // MARK:- 所有调用的API枚举
  11. enum OOCloudStorageAPI {
  12. //新版
  13. case listTop
  14. case listFolderTop
  15. case listByFolder(String)
  16. case listFolderByFolder(String)
  17. case createFolder(String, String)
  18. case getFile(String)
  19. // folderId, fileName , file
  20. case uploadFile(String, String, Data)
  21. //fileId, file
  22. case updateFile(String, OOAttachment)
  23. //folderId folder
  24. case updateFolder(String, OOFolder)
  25. case deleteFolder(String)
  26. case deleteFile(String)
  27. //分享
  28. case share(OOShareForm)
  29. //分类查询 分页 type: String, page: Int, count: Int
  30. case listTypeByPage(String, Int, Int)
  31. case downloadFile(OOAttachment)
  32. //fileType = attachment | folder
  33. case shareToMe(String)
  34. //fileType = attachment | folder
  35. case myShareList(String)
  36. case shareFileListWithFolderId(String, String)
  37. case shareFolderListWithFolderId(String, String)
  38. case shieldShare(String)
  39. case deleteMyShare(String)
  40. // 上传文件 fileName referencetype reference scale file
  41. case uploadImageWithReferencetype(String, String, String, Int, Data)
  42. //老版
  43. //获取当前人员顶层文件夹 - jaxrs/complex/folder/##id##
  44. case listFolder(String)
  45. //jaxrs/share/list
  46. case listMyShare
  47. //jaxrs/editor/list
  48. case listMyEditor
  49. //jaxrs/attachment/{id}
  50. case listMyShareByPerson(String)
  51. case listMyEditorByPerson(String)
  52. case getPicItemURL(String)
  53. case getAttachment(String)
  54. // jaxrs/attachment/{id}/download/stream
  55. case downloadAttachment(OOAttachment)
  56. //*
  57. case deleteAttachement(String)
  58. case renameAttachment(String)
  59. case uploadAttachment(String?)
  60. }
  61. // MARK:- 上下文实现
  62. extension OOCloudStorageAPI:OOAPIContextCapable {
  63. var apiContextKey: String {
  64. return "x_file_assemble_control"
  65. }
  66. }
  67. // MARK: - 是否需要加入x-token访问头
  68. extension OOCloudStorageAPI:OOAccessTokenAuthorizable {
  69. public var shouldAuthorize: Bool {
  70. return true
  71. }
  72. }
  73. // MARK: - MoyaAPI实现
  74. extension OOCloudStorageAPI:TargetType{
  75. var baseURL: URL {
  76. let model = O2AuthSDK.shared.o2APIServer(context: .x_file_assemble_control)
  77. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)\(model?.context ?? "")"
  78. if let trueUrl = O2AuthSDK.shared.bindUnitTransferUrl2Mapping(url: baseURLString) {
  79. return URL(string: trueUrl)!
  80. }
  81. return URL(string: baseURLString)!
  82. }
  83. var path: String {
  84. switch self {
  85. case .listTop:
  86. return "/jaxrs/attachment2/list/top"
  87. case .listFolderTop:
  88. return "/jaxrs/folder2/list/top"
  89. case .listByFolder(let folderId):
  90. return "/jaxrs/attachment2/list/folder/\(folderId)"
  91. case .listFolderByFolder(let folderId):
  92. return "/jaxrs/folder2/list/\(folderId)"
  93. case .createFolder(_, _):
  94. return "/jaxrs/folder2"
  95. case .uploadFile(let folderId, _, _):
  96. return "/jaxrs/attachment2/upload/folder/\(folderId)"
  97. case .updateFile(let fileId, _), .deleteFile(let fileId):
  98. return "/jaxrs/attachment2/\(fileId)"
  99. case .getFile(let fileId):
  100. return "jaxrs/attachment2/\(fileId)"
  101. case .updateFolder(let folderId, _), .deleteFolder(let folderId):
  102. return "/jaxrs/folder2/\(folderId)"
  103. case .share(_):
  104. return "/jaxrs/share"
  105. case .listTypeByPage(_, let page, let count):
  106. return "/jaxrs/attachment2/list/type/\(page)/size/\(count)"
  107. case .downloadFile(let file):
  108. return "/jaxrs/attachment2/\(file.id!)/download/stream"
  109. case .shareToMe(let fileType):
  110. return "/jaxrs/share/list/to/me2/\(fileType)"
  111. case .myShareList(let fileType):
  112. return "/jaxrs/share/list/my2/member/\(fileType)"
  113. case .shareFolderListWithFolderId(let shareId, let folderId):
  114. return "/jaxrs/share/list/folder/share/\(shareId)/folder/\(folderId)/"
  115. case .shareFileListWithFolderId(let shareId, let folderId):
  116. return "/jaxrs/share/list/att/share/\(shareId)/folder/\(folderId)/"
  117. case .shieldShare(let shareId):
  118. return "/jaxrs/share/shield/\(shareId)"
  119. case .deleteMyShare(let shareId):
  120. return "/jaxrs/share/\(shareId)"
  121. case .uploadImageWithReferencetype(_, let referencetype, let reference, let scale, _):
  122. return "/jaxrs/file/upload/referencetype/\(referencetype)/reference/\(reference)/scale/\(scale)"
  123. case .listFolder(let folderId):
  124. return "/jaxrs/complex/folder/\(folderId)"
  125. case .listMyShare:
  126. return "/jaxrs/share/list"
  127. case .listMyEditor:
  128. return "/jaxrs/editor/list"
  129. case .listMyShareByPerson(let personId):
  130. return "/jaxrs/attachment/list/share/\(personId)"
  131. case .listMyEditorByPerson(let personId):
  132. return "/jaxrs/attachment/list/editor/\(personId)"
  133. case .getAttachment(let attachmentId),.deleteAttachement(let attachmentId),.renameAttachment(let attachmentId):
  134. return "/jaxrs/attachment/\(attachmentId)"
  135. case .getPicItemURL(let id):
  136. return "\(self.baseURL.absoluteString)/jaxrs/file/\(id)/download/stream"
  137. case .downloadAttachment(let attachment):
  138. return "/jaxrs/attachment/\(attachment.id!)/download/stream"
  139. case .uploadAttachment(let folderId):
  140. if folderId == nil {
  141. return "jaxrs/attachment/upload"
  142. }else{
  143. return "jaxrs/attachment/upload/folder/\(folderId!)"
  144. }
  145. }
  146. }
  147. var method: Moya.Method {
  148. switch self {
  149. case .listTop, .listFolderTop, .listByFolder(_), .listFolderByFolder(_), .downloadFile(_), .getFile(_):
  150. return .get
  151. case .listFolder(_), .shareToMe(_), .myShareList(_), .shareFileListWithFolderId(_, _), .shareFolderListWithFolderId(_, _), .shieldShare(_):
  152. return .get
  153. case .listMyShare:
  154. return .get
  155. case .listMyEditor:
  156. return .get
  157. case .listMyShareByPerson(_):
  158. return .get
  159. case .listMyEditorByPerson(_):
  160. return .get
  161. case .getAttachment(_):
  162. return .get
  163. case .getPicItemURL(_):
  164. return .get
  165. case .deleteAttachement(_), .updateFolder(_, _), .updateFile(_, _), .uploadImageWithReferencetype(_, _, _, _, _):
  166. return .put
  167. case .uploadAttachment(_), .downloadAttachment(_),
  168. .renameAttachment(_), .createFolder(_, _), .uploadFile(_,_,_), .share(_),.listTypeByPage(_, _, _):
  169. return .post
  170. case .deleteFolder(_), .deleteFile(_), .deleteMyShare(_):
  171. return .delete
  172. }
  173. }
  174. var sampleData: Data {
  175. return "".data(using: String.Encoding.utf8)!
  176. }
  177. var task: Task {
  178. switch self {
  179. case .listFolderByFolder(_), .listByFolder(_), .listTop,.listFolderTop,.listFolder(_),.listMyEditorByPerson(_),.listMyShareByPerson(_),
  180. .listMyEditor,.listMyShare,.getAttachment(_),.deleteAttachement(_),.getPicItemURL(_), .deleteFolder(_), .deleteFile(_),
  181. .getFile(_), .shareFileListWithFolderId(_, _), .shareFolderListWithFolderId(_, _):
  182. return .requestPlain
  183. case .shareToMe(_):
  184. return .requestPlain
  185. case .myShareList(_), .deleteMyShare(_), .shieldShare(_):
  186. return .requestPlain
  187. case .downloadFile(let attachment):
  188. let myDest = getDownDest(attachment)
  189. return .downloadDestination(myDest)
  190. case .downloadAttachment(let attachment):
  191. let myDest = getDownDest(attachment)
  192. return .downloadDestination(myDest)
  193. case .renameAttachment(_):
  194. return .requestPlain
  195. case .uploadAttachment(_):
  196. return .requestPlain
  197. //新接口
  198. case .createFolder(let name, let superior):
  199. return .requestParameters(parameters: ["name":name, "superior": superior], encoding: JSONEncoding.default)
  200. case .uploadFile(_, let fileName, let data):
  201. //字符串类型 文件名
  202. let strData = fileName.data(using: .utf8)
  203. let fileNameData = MultipartFormData(provider: .data(strData!), name: "fileName")
  204. //文件类型
  205. let fileData = MultipartFormData(provider: .data(data), name: "file", fileName: fileName)
  206. return .uploadMultipart([fileData, fileNameData])
  207. case .updateFolder(_, let folder):
  208. return .requestParameters(parameters: folder.toJSON()!, encoding: JSONEncoding.default)
  209. case .updateFile(_, let file):
  210. return .requestParameters(parameters: file.toJSON()!, encoding: JSONEncoding.default)
  211. case .share(let form):
  212. return .requestParameters(parameters: form.toJSON()!, encoding: JSONEncoding.default)
  213. case .listTypeByPage(let type, _, _):
  214. return .requestParameters(parameters: ["fileType": type], encoding: JSONEncoding.default)
  215. case .uploadImageWithReferencetype(let fileName, _, _, _, let data):
  216. //字符串类型 文件名
  217. let strData = fileName.data(using: .utf8)
  218. let fileNameData = MultipartFormData(provider: .data(strData!), name: "fileName")
  219. //文件类型
  220. let fileData = MultipartFormData(provider: .data(data), name: "file", fileName: fileName)
  221. return .uploadMultipart([fileData, fileNameData])
  222. }
  223. }
  224. var headers: [String : String]? {
  225. return nil
  226. }
  227. func getDownDest(_ attachment:OOAttachment) -> DownloadDestination {
  228. let myDest:DownloadDestination = { temporaryURL, response in
  229. let fileURL = O2CloudFileManager.shared.cloudFileLocalPath(file: attachment)
  230. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  231. }
  232. return myDest
  233. }
  234. }