CCActions.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // CCActions.swift
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/02/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. @objc protocol CCActionsDeleteDelegate {
  25. func deleteFileOrFolderSuccess(_ metadataNet: CCMetadataNet)
  26. func deleteFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  27. }
  28. @objc protocol CCActionsRenameDelegate {
  29. func renameSuccess(_ metadataNet: CCMetadataNet)
  30. func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  31. func uploadFileSuccess(_ metadataNet: CCMetadataNet, fileID: String, serverUrl: String, selector: String, selectorPost: String)
  32. func uploadFileFailure(_ metadataNet: CCMetadataNet, fileID: String, serverUrl: String, selector: String, message: String, errorCode: NSInteger)
  33. }
  34. @objc protocol CCActionsSearchDelegate {
  35. func searchSuccess(_ metadataNet: CCMetadataNet, metadatas: [Any])
  36. func searchFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  37. }
  38. class CCActions: NSObject {
  39. //MARK: Shared Instance
  40. static let sharedInstance: CCActions = {
  41. let instance = CCActions()
  42. return instance
  43. }()
  44. //MARK: Local Variable
  45. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  46. var metadataNet: CCMetadataNet = CCMetadataNet.init()
  47. //MARK: Init
  48. override init() {
  49. }
  50. // --------------------------------------------------------------------------------------------
  51. // MARK: Delete File or Folder
  52. // --------------------------------------------------------------------------------------------
  53. func deleteFileOrFolder(_ metadata: CCMetadata, delegate: AnyObject) {
  54. let serverUrl = CCCoreData.getServerUrl(fromDirectoryID: metadata.directoryID, activeAccount: appDelegate.activeAccount)!
  55. let metadataNet = CCMetadataNet.init()
  56. if metadata.cryptated == true {
  57. metadataNet.action = actionDeleteFileDirectory
  58. metadataNet.delegate = delegate
  59. metadataNet.fileID = metadata.fileID
  60. metadataNet.fileNamePrint = metadata.fileNamePrint
  61. metadataNet.metadata = metadata
  62. metadataNet.serverUrl = serverUrl
  63. // data crypto
  64. metadataNet.fileName = metadata.fileNameData
  65. metadataNet.selector = selectorDeleteCrypto
  66. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  67. // plist
  68. metadataNet.fileName = metadata.fileName;
  69. metadataNet.selector = selectorDeletePlist
  70. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  71. } else {
  72. metadataNet.action = actionDeleteFileDirectory
  73. metadataNet.delegate = delegate
  74. metadataNet.fileID = metadata.fileID
  75. metadataNet.fileName = metadata.fileName
  76. metadataNet.fileNamePrint = metadata.fileNamePrint
  77. metadataNet.metadata = metadata
  78. metadataNet.selector = selectorDelete
  79. metadataNet.serverUrl = serverUrl
  80. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  81. }
  82. }
  83. func deleteFileOrFolderSuccess(_ metadataNet: CCMetadataNet) {
  84. CCCoreData.deleteFile(metadataNet.metadata, serverUrl: metadataNet.serverUrl, directoryUser: appDelegate.directoryUser, typeCloud: appDelegate.typeCloud, activeAccount: appDelegate.activeAccount)
  85. metadataNet.delegate?.deleteFileOrFolderSuccess(metadataNet)
  86. }
  87. func deleteFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  88. if errorCode == 404 {
  89. CCCoreData.deleteFile(metadataNet.metadata, serverUrl: metadataNet.serverUrl, directoryUser: appDelegate.directoryUser, typeCloud: appDelegate.typeCloud, activeAccount: appDelegate.activeAccount)
  90. }
  91. if message.length > 0 {
  92. appDelegate.messageNotification("_delete_", description: message as String, visible: true, delay:TimeInterval(dismissAfterSecond), type:TWMessageBarMessageType.error)
  93. }
  94. metadataNet.delegate?.deleteFileOrFolderFailure(metadataNet, message: message, errorCode: errorCode)
  95. }
  96. // --------------------------------------------------------------------------------------------
  97. // MARK: Rename File or Folder
  98. // --------------------------------------------------------------------------------------------
  99. func renameFileOrFolder(_ metadata: CCMetadata, fileName: String, delegate: AnyObject) {
  100. let crypto = CCCrypto.init()
  101. let metadataNet = CCMetadataNet.init()
  102. let fileName = CCUtility.removeForbiddenCharacters(fileName, hasServerForbiddenCharactersSupport: appDelegate.hasServerForbiddenCharactersSupport)!
  103. let serverUrl = CCCoreData.getServerUrl(fromDirectoryID: metadata.directoryID, activeAccount: appDelegate.activeAccount)!
  104. if fileName.characters.count == 0 {
  105. return
  106. }
  107. if metadata.fileNamePrint == fileName {
  108. return
  109. }
  110. if metadata.cryptated {
  111. // Encrypted
  112. let newTitle = AESCrypt.encrypt(fileName, password: crypto.getKeyPasscode(metadata.uuid))
  113. if !crypto.updateTitleFilePlist(metadata.fileName, title: newTitle, directoryUser: appDelegate.directoryUser) {
  114. print("[LOG] Rename cryptated error \(fileName)")
  115. appDelegate.messageNotification("_rename_", description: "_file_not_found_reload_", visible: true, delay: TimeInterval(dismissAfterSecond), type: TWMessageBarMessageType.error)
  116. return
  117. }
  118. if !metadata.directory {
  119. do {
  120. let file = "\(appDelegate.directoryUser!)/\(metadata.fileID!)"
  121. let dataFile = try NSData.init(contentsOfFile: file, options:[])
  122. do {
  123. let dataFileEncrypted = try RNEncryptor.encryptData(dataFile as Data!, with: kRNCryptorAES256Settings, password: crypto.getKeyPasscode(metadata.uuid))
  124. do {
  125. let fileUrl = URL(fileURLWithPath: "\(NSTemporaryDirectory())\(metadata.fileNameData!)")
  126. try dataFileEncrypted.write(to: fileUrl, options: [])
  127. } catch let error {
  128. print(error.localizedDescription)
  129. return
  130. }
  131. } catch let error {
  132. print(error.localizedDescription)
  133. return
  134. }
  135. } catch let error {
  136. print(error.localizedDescription)
  137. return
  138. }
  139. }
  140. metadataNet.action = actionUploadOnlyPlist
  141. metadataNet.delegate = delegate
  142. metadataNet.fileName = metadata.fileName
  143. metadataNet.metadata = metadata
  144. metadataNet.selectorPost = selectorReadFolderForced
  145. metadataNet.serverUrl = serverUrl
  146. metadataNet.session = upload_session_foreground
  147. metadataNet.taskStatus = Int(taskStatusResume)
  148. if CCCoreData.isOfflineLocalFileID(metadata.fileID, activeAccount: appDelegate.activeAccount) {
  149. metadataNet.selectorPost = selectorAddOffline
  150. }
  151. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  152. // delete file in filesystem
  153. CCCoreData.deleteFile(metadata, serverUrl: serverUrl, directoryUser: appDelegate.directoryUser, typeCloud: appDelegate.typeCloud, activeAccount: appDelegate.activeAccount)
  154. } else {
  155. // Plain
  156. metadataNet.action = actionMoveFileOrFolder
  157. metadataNet.delegate = delegate
  158. metadataNet.fileID = metadata.fileID
  159. metadataNet.fileName = metadata.fileName
  160. metadataNet.fileNamePrint = metadata.fileNamePrint
  161. metadataNet.fileNameTo = fileName
  162. metadataNet.metadata = metadata
  163. metadataNet.selector = selectorRename
  164. metadataNet.serverUrl = serverUrl
  165. metadataNet.serverUrlTo = serverUrl
  166. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  167. }
  168. }
  169. func renameSuccess(_ metadataNet: CCMetadataNet) {
  170. if metadataNet.metadata.directory {
  171. let directory = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addServerUrl: metadataNet.fileName)
  172. let directoryTo = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addServerUrl: metadataNet.fileNameTo)
  173. CCCoreData.renameDirectory(directory, serverUrlTo: directory, activeAccount: directoryTo)
  174. } else {
  175. CCCoreData.renameLocalFile(withFileID: metadataNet.metadata.fileID, fileNameTo: metadataNet.fileNameTo, fileNamePrintTo: metadataNet.fileNameTo, activeAccount: appDelegate.activeAccount)
  176. }
  177. metadataNet.delegate?.renameSuccess(metadataNet)
  178. }
  179. func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  180. if message.length > 0 {
  181. var title : String = ""
  182. if metadataNet.selector == selectorRename {
  183. title = "_delete_"
  184. }
  185. if metadataNet.selector == selectorMove {
  186. title = "_move_"
  187. }
  188. appDelegate.messageNotification(title, description: message as String, visible: true, delay:TimeInterval(dismissAfterSecond), type:TWMessageBarMessageType.error)
  189. }
  190. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: message as NSString, errorCode: errorCode)
  191. }
  192. func uploadFileSuccess(_ metadataNet: CCMetadataNet, fileID: String, serverUrl: String, selector: String, selectorPost: String) {
  193. metadataNet.delegate?.uploadFileSuccess(metadataNet, fileID:fileID, serverUrl: serverUrl, selector: selector, selectorPost: selectorPost)
  194. }
  195. func uploadFileFailure(_ metadataNet: CCMetadataNet, fileID: String, serverUrl: String, selector: String, message: String, errorCode: NSInteger) {
  196. metadataNet.delegate?.uploadFileFailure(metadataNet, fileID:fileID, serverUrl: serverUrl, selector: selector, message: message, errorCode: errorCode)
  197. }
  198. // --------------------------------------------------------------------------------------------
  199. // MARK: Search
  200. // --------------------------------------------------------------------------------------------
  201. func search(_ serverUrl : String?, fileName : String, delegate: AnyObject) {
  202. if (serverUrl != nil) {
  203. metadataNet.action = actionReadFolder;
  204. metadataNet.delegate = delegate
  205. metadataNet.fileName = fileName
  206. metadataNet.selector = selectorSearch
  207. metadataNet.serverUrl = serverUrl!
  208. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  209. } else {
  210. // Search DAV API
  211. }
  212. }
  213. func searchSuccess(_ metadataNet: CCMetadataNet, metadatas: [CCMetadata]) {
  214. metadataNet.delegate?.searchSuccess(metadataNet, metadatas: metadatas)
  215. }
  216. func searchFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  217. metadataNet.delegate?.searchFailure(metadataNet, message: message, errorCode: errorCode)
  218. }
  219. }