CCActions.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. @objc protocol CCActionsDownloadThumbnailDelegate {
  39. func downloadThumbnailSuccess(_ metadataNet: CCMetadataNet)
  40. }
  41. @objc protocol CCActionsSettingFavoriteDelegate {
  42. func settingFavoriteSuccess(_ metadataNet: CCMetadataNet)
  43. func settingFavoriteFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  44. }
  45. @objc protocol CCActionsListingFavoritesDelegate {
  46. func listingFavoritesSuccess(_ metadataNet: CCMetadataNet, metadatas: [Any])
  47. func listingFavoritesFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  48. }
  49. class CCActions: NSObject {
  50. //MARK: Shared Instance
  51. static let sharedInstance: CCActions = {
  52. let instance = CCActions()
  53. return instance
  54. }()
  55. //MARK: Local Variable
  56. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  57. //MARK: Init
  58. override init() {
  59. }
  60. // --------------------------------------------------------------------------------------------
  61. // MARK: Delete File or Folder
  62. // --------------------------------------------------------------------------------------------
  63. func deleteFileOrFolder(_ metadata: CCMetadata, delegate: AnyObject) {
  64. let serverUrl = CCCoreData.getServerUrl(fromDirectoryID: metadata.directoryID, activeAccount: appDelegate.activeAccount)!
  65. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  66. if metadata.cryptated == true {
  67. metadataNet.action = actionDeleteFileDirectory
  68. metadataNet.delegate = delegate
  69. metadataNet.fileID = metadata.fileID
  70. metadataNet.fileNamePrint = metadata.fileNamePrint
  71. metadataNet.metadata = metadata
  72. metadataNet.serverUrl = serverUrl
  73. // data crypto
  74. metadataNet.fileName = metadata.fileNameData
  75. metadataNet.selector = selectorDeleteCrypto
  76. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  77. // plist
  78. metadataNet.fileName = metadata.fileName;
  79. metadataNet.selector = selectorDeletePlist
  80. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  81. } else {
  82. metadataNet.action = actionDeleteFileDirectory
  83. metadataNet.delegate = delegate
  84. metadataNet.fileID = metadata.fileID
  85. metadataNet.fileName = metadata.fileName
  86. metadataNet.fileNamePrint = metadata.fileNamePrint
  87. metadataNet.metadata = metadata
  88. metadataNet.selector = selectorDelete
  89. metadataNet.serverUrl = serverUrl
  90. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  91. }
  92. }
  93. func deleteFileOrFolderSuccess(_ metadataNet: CCMetadataNet) {
  94. CCCoreData.deleteFile(metadataNet.metadata, serverUrl: metadataNet.serverUrl, directoryUser: appDelegate.directoryUser, activeAccount: appDelegate.activeAccount)
  95. metadataNet.delegate?.deleteFileOrFolderSuccess(metadataNet)
  96. }
  97. func deleteFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  98. if errorCode == 404 {
  99. CCCoreData.deleteFile(metadataNet.metadata, serverUrl: metadataNet.serverUrl, directoryUser: appDelegate.directoryUser, activeAccount: appDelegate.activeAccount)
  100. }
  101. if message.length > 0 {
  102. appDelegate.messageNotification("_delete_", description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error)
  103. }
  104. metadataNet.delegate?.deleteFileOrFolderFailure(metadataNet, message: message, errorCode: errorCode)
  105. }
  106. // --------------------------------------------------------------------------------------------
  107. // MARK: Rename File or Folder
  108. // --------------------------------------------------------------------------------------------
  109. func renameFileOrFolder(_ metadata: CCMetadata, fileName: String, delegate: AnyObject) {
  110. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  111. let fileName = CCUtility.removeForbiddenCharacters(fileName, hasServerForbiddenCharactersSupport: appDelegate.hasServerForbiddenCharactersSupport)!
  112. let serverUrl = CCCoreData.getServerUrl(fromDirectoryID: metadata.directoryID, activeAccount: appDelegate.activeAccount)!
  113. if fileName.characters.count == 0 {
  114. return
  115. }
  116. if metadata.fileNamePrint == fileName {
  117. return
  118. }
  119. if metadata.cryptated {
  120. let crypto = CCCrypto.sharedManager() as! CCCrypto
  121. // Encrypted
  122. let newTitle = AESCrypt.encrypt(fileName, password: crypto.getKeyPasscode(metadata.uuid))
  123. if !crypto.updateTitleFilePlist(metadata.fileName, title: newTitle, directoryUser: appDelegate.directoryUser) {
  124. print("[LOG] Rename cryptated error \(fileName)")
  125. appDelegate.messageNotification("_rename_", description: "_file_not_found_reload_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error)
  126. return
  127. }
  128. if !metadata.directory {
  129. do {
  130. let file = "\(appDelegate.directoryUser!)/\(metadata.fileID!)"
  131. let dataFile = try NSData.init(contentsOfFile: file, options:[])
  132. do {
  133. let dataFileEncrypted = try RNEncryptor.encryptData(dataFile as Data!, with: kRNCryptorAES256Settings, password: crypto.getKeyPasscode(metadata.uuid))
  134. do {
  135. let fileUrl = URL(fileURLWithPath: "\(NSTemporaryDirectory())\(metadata.fileNameData!)")
  136. try dataFileEncrypted.write(to: fileUrl, options: [])
  137. } catch let error {
  138. print(error.localizedDescription)
  139. return
  140. }
  141. } catch let error {
  142. print(error.localizedDescription)
  143. return
  144. }
  145. } catch let error {
  146. print(error.localizedDescription)
  147. return
  148. }
  149. }
  150. metadataNet.action = actionUploadOnlyPlist
  151. metadataNet.delegate = delegate
  152. metadataNet.fileName = metadata.fileName
  153. metadataNet.metadata = metadata
  154. metadataNet.selectorPost = selectorReadFolderForced
  155. metadataNet.serverUrl = serverUrl
  156. metadataNet.session = k_upload_session_foreground
  157. metadataNet.taskStatus = Int(k_taskStatusResume)
  158. if CCCoreData.isOfflineLocalFileID(metadata.fileID, activeAccount: appDelegate.activeAccount) {
  159. metadataNet.selectorPost = selectorAddOffline
  160. }
  161. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  162. // delete file in filesystem
  163. CCCoreData.deleteFile(metadata, serverUrl: serverUrl, directoryUser: appDelegate.directoryUser, activeAccount: appDelegate.activeAccount)
  164. } else {
  165. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: appDelegate.activeUser, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl, isCryptoCloudMode: false);
  166. let error = ocNetworking?.readFileSync("\(serverUrl)/\(fileName)");
  167. // Verify if exists the fileName TO
  168. if error == nil {
  169. let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: UIAlertControllerStyle.alert)
  170. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
  171. (result : UIAlertAction) -> Void in
  172. }
  173. alertController.addAction(okAction)
  174. delegate.present(alertController, animated: true, completion: nil)
  175. return;
  176. }
  177. // Plain
  178. metadataNet.action = actionMoveFileOrFolder
  179. metadataNet.delegate = delegate
  180. metadataNet.fileID = metadata.fileID
  181. metadataNet.fileName = metadata.fileName
  182. metadataNet.fileNamePrint = metadata.fileNamePrint
  183. metadataNet.fileNameTo = fileName
  184. metadataNet.metadata = metadata
  185. metadataNet.selector = selectorRename
  186. metadataNet.serverUrl = serverUrl
  187. metadataNet.serverUrlTo = serverUrl
  188. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  189. }
  190. }
  191. func renameSuccess(_ metadataNet: CCMetadataNet) {
  192. if metadataNet.metadata.directory {
  193. let directory = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileName)
  194. let directoryTo = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileNameTo)
  195. CCCoreData.renameDirectory(directory, serverUrlTo: directoryTo, activeAccount: appDelegate.activeAccount)
  196. } else {
  197. CCCoreData.renameLocalFile(withFileID: metadataNet.metadata.fileID, fileNameTo: metadataNet.fileNameTo, fileNamePrintTo: metadataNet.fileNameTo, activeAccount: appDelegate.activeAccount)
  198. }
  199. metadataNet.delegate?.renameSuccess(metadataNet)
  200. }
  201. func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  202. if message.length > 0 {
  203. var title : String = ""
  204. if metadataNet.selector == selectorRename {
  205. title = "_delete_"
  206. }
  207. if metadataNet.selector == selectorMove {
  208. title = "_move_"
  209. }
  210. appDelegate.messageNotification(title, description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error)
  211. }
  212. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: message as NSString, errorCode: errorCode)
  213. }
  214. func uploadFileSuccess(_ metadataNet: CCMetadataNet, fileID: String, serverUrl: String, selector: String, selectorPost: String) {
  215. metadataNet.delegate?.uploadFileSuccess(metadataNet, fileID:fileID, serverUrl: serverUrl, selector: selector, selectorPost: selectorPost)
  216. }
  217. func uploadFileFailure(_ metadataNet: CCMetadataNet, fileID: String, serverUrl: String, selector: String, message: String, errorCode: NSInteger) {
  218. metadataNet.delegate?.uploadFileFailure(metadataNet, fileID:fileID, serverUrl: serverUrl, selector: selector, message: message, errorCode: errorCode)
  219. }
  220. // --------------------------------------------------------------------------------------------
  221. // MARK: Search
  222. // --------------------------------------------------------------------------------------------
  223. func search(_ serverUrl : String, fileName : String, depth : String, delegate: AnyObject) {
  224. // Search DAV API
  225. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  226. metadataNet.action = actionSearch
  227. metadataNet.delegate = delegate
  228. metadataNet.fileName = fileName
  229. metadataNet.options = depth
  230. metadataNet.selector = selectorSearch
  231. metadataNet.serverUrl = serverUrl
  232. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  233. }
  234. func searchSuccess(_ metadataNet: CCMetadataNet, metadatas: [CCMetadata]) {
  235. metadataNet.delegate?.searchSuccess(metadataNet, metadatas: metadatas)
  236. }
  237. func searchFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  238. metadataNet.delegate?.searchFailure(metadataNet, message: message, errorCode: errorCode)
  239. }
  240. // --------------------------------------------------------------------------------------------
  241. // MARK: Download Tumbnail
  242. // --------------------------------------------------------------------------------------------
  243. func downloadTumbnail(_ metadata: CCMetadata, delegate: AnyObject) {
  244. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  245. let serverUrl = CCCoreData.getServerUrl(fromDirectoryID: metadata.directoryID, activeAccount: appDelegate.activeAccount)
  246. metadataNet.action = actionDownloadThumbnail
  247. metadataNet.delegate = delegate
  248. metadataNet.fileID = metadata.fileID
  249. metadataNet.fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)
  250. metadataNet.fileNameLocal = metadata.fileID
  251. metadataNet.fileNamePrint = metadata.fileNamePrint
  252. metadataNet.metadata = metadata
  253. metadataNet.options = "m"
  254. metadataNet.priority = Operation.QueuePriority.low.rawValue
  255. metadataNet.selector = selectorDownloadThumbnail;
  256. metadataNet.serverUrl = serverUrl;
  257. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  258. }
  259. func downloadThumbnailSuccess(_ metadataNet: CCMetadataNet) {
  260. metadataNet.delegate?.downloadThumbnailSuccess(metadataNet)
  261. }
  262. func downloadThumbnailFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  263. NSLog("[LOG] Thumbnail Error \(metadataNet.fileName!) \(message) error %\(errorCode))")
  264. }
  265. // --------------------------------------------------------------------------------------------
  266. // MARK: Setting Favorite
  267. // --------------------------------------------------------------------------------------------
  268. func settingFavorite(_ metadata: CCMetadata, favorite: Bool, delegate: AnyObject) {
  269. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  270. let serverUrl = CCCoreData.getServerUrl(fromDirectoryID: metadata.directoryID, activeAccount: appDelegate.activeAccount)
  271. metadataNet.action = actionSettingFavorite
  272. metadataNet.delegate = delegate
  273. metadataNet.fileID = metadata.fileID
  274. metadataNet.fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)
  275. metadataNet.metadata = metadata
  276. metadataNet.options = "\(favorite)"
  277. metadataNet.priority = Operation.QueuePriority.normal.rawValue
  278. metadataNet.selector = selectorAddFavorite
  279. metadataNet.serverUrl = serverUrl;
  280. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  281. }
  282. func settingFavoriteSuccess(_ metadataNet: CCMetadataNet) {
  283. metadataNet.delegate?.settingFavoriteSuccess(metadataNet)
  284. }
  285. func settingFavoriteFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  286. appDelegate.messageNotification("_favorites_", description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error)
  287. metadataNet.delegate?.settingFavoriteFailure(metadataNet, message: message, errorCode: errorCode)
  288. }
  289. // --------------------------------------------------------------------------------------------
  290. // MARK: Linsting Favorites
  291. // --------------------------------------------------------------------------------------------
  292. func listingFavorites(_ serverUrl : String, delegate: AnyObject) {
  293. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  294. metadataNet.action = actionListingFavorites
  295. metadataNet.delegate = delegate
  296. metadataNet.priority = Operation.QueuePriority.normal.rawValue
  297. metadataNet.serverUrl = serverUrl
  298. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  299. }
  300. func listingFavoritesSuccess(_ metadataNet: CCMetadataNet, metadatas: [CCMetadata]) {
  301. metadataNet.delegate?.listingFavoritesSuccess(metadataNet, metadatas: metadatas)
  302. }
  303. func listingFavoritesFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  304. metadataNet.delegate?.listingFavoritesFailure(metadataNet, message: message, errorCode: errorCode)
  305. }
  306. }