CCActions.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // CCActions.swift
  3. // Nextcloud iOS
  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 deleteFileOrFolderSuccessFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  26. }
  27. @objc protocol CCActionsRenameDelegate {
  28. func renameSuccess(_ metadataNet: CCMetadataNet)
  29. func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  30. }
  31. @objc protocol CCActionsSearchDelegate {
  32. func searchSuccess(_ metadataNet: CCMetadataNet, metadatas: [Any])
  33. func searchFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  34. }
  35. @objc protocol CCActionsDownloadThumbnailDelegate {
  36. func downloadThumbnailSuccess(_ metadataNet: CCMetadataNet)
  37. }
  38. @objc protocol CCActionsSettingFavoriteDelegate {
  39. func settingFavoriteSuccess(_ metadataNet: CCMetadataNet)
  40. func settingFavoriteFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  41. }
  42. @objc protocol CCActionsListingFavoritesDelegate {
  43. func listingFavoritesSuccess(_ metadataNet: CCMetadataNet, metadatas: [Any])
  44. func listingFavoritesFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  45. }
  46. class CCActions: NSObject {
  47. //MARK: Shared Instance
  48. @objc static let sharedInstance: CCActions = {
  49. let instance = CCActions()
  50. return instance
  51. }()
  52. //MARK: Local Variable
  53. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  54. //MARK: Init
  55. override init() {
  56. }
  57. // --------------------------------------------------------------------------------------------
  58. // MARK: Delete File or Folder
  59. // --------------------------------------------------------------------------------------------
  60. @objc func deleteFileOrFolder(_ metadata: tableMetadata,delegate: AnyObject, hud: CCHud?, hudTitled: String?) {
  61. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  62. return
  63. }
  64. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  65. // fix CCActions.swift line 88 2.17.2 (00005)
  66. if (serverUrl == "") {
  67. appDelegate.messageNotification("_delete_", description: "_file_not_found_reload_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  68. return
  69. }
  70. guard let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND serverUrl = %@", self.appDelegate.activeAccount, serverUrl)) else {
  71. return
  72. }
  73. DispatchQueue.global().async {
  74. // E2EE LOCK
  75. let tableE2eEncryption = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account = %@ AND fileNameIdentifier = %@", self.appDelegate.activeAccount, metadata.fileName))
  76. if tableE2eEncryption != nil {
  77. let error = NCNetworkingSync.sharedManager().lockEnd(toEndFolderEncrypted: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl, serverUrl:serverUrl, fileID: tableDirectory.fileID)
  78. if error != nil {
  79. DispatchQueue.main.async {
  80. self.appDelegate.messageNotification("_delete_", description: error!.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  81. }
  82. return;
  83. }
  84. }
  85. metadataNet.action = actionDeleteFileDirectory
  86. metadataNet.delegate = delegate
  87. metadataNet.directory = metadata.directory
  88. metadataNet.directoryID = metadata.directoryID
  89. metadataNet.fileID = metadata.fileID
  90. metadataNet.fileName = metadata.fileName
  91. metadataNet.fileNameView = metadata.fileNameView
  92. metadataNet.selector = selectorDelete
  93. metadataNet.serverUrl = serverUrl
  94. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  95. if hud != nil {
  96. DispatchQueue.main.async {
  97. hud?.visibleHudTitle(hudTitled, mode: MBProgressHUDMode.indeterminate, color: nil)
  98. }
  99. }
  100. }
  101. }
  102. @objc func deleteFileOrFolderSuccessFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  103. if (errorCode == 0) {
  104. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", metadataNet.fileID))
  105. if let metadata = metadata {
  106. self.deleteFile(metadata: metadata, serverUrl: metadataNet.serverUrl)
  107. }
  108. guard let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND serverUrl = %@", self.appDelegate.activeAccount, metadataNet.serverUrl)) else {
  109. self.deleteFileOrFolderSuccessFailure(metadataNet, message: "Internal error, tableDirectory not found", errorCode: 0)
  110. return
  111. }
  112. // E2EE Rebuild and send Metadata
  113. if tableDirectory.e2eEncrypted {
  114. DispatchQueue.global().async {
  115. var errorUnlock: NSError?
  116. var errorRebuild: NSError?
  117. var token: String?
  118. // Send Metadata
  119. errorRebuild = NCNetworkingSync.sharedManager().rebuildAndSendEndToEndMetadata(onServerUrl: metadataNet.serverUrl, account: self.appDelegate.activeAccount, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl) as NSError?
  120. if (errorRebuild != nil) {
  121. DispatchQueue.main.async {
  122. self.deleteFileOrFolderSuccessFailure(metadataNet, message: errorRebuild!.localizedDescription as NSString, errorCode: errorRebuild!.code)
  123. }
  124. }
  125. // Unlock
  126. token = NCManageDatabase.sharedInstance.getDirectoryE2ETokenLock(serverUrl: metadataNet.serverUrl)
  127. if (token != nil) {
  128. errorUnlock = NCNetworkingSync.sharedManager().unlockEnd(toEndFolderEncrypted: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl, serverUrl: metadataNet.serverUrl,fileID: tableDirectory.fileID, token: token) as NSError?
  129. if (errorUnlock != nil) {
  130. DispatchQueue.main.async {
  131. self.deleteFileOrFolderSuccessFailure(metadataNet, message: errorUnlock!.localizedDescription as NSString, errorCode: errorUnlock!.code)
  132. }
  133. }
  134. } else {
  135. print("Error unlock not found")
  136. }
  137. if (errorRebuild == nil && errorUnlock == nil) {
  138. DispatchQueue.main.async {
  139. metadataNet.delegate?.deleteFileOrFolderSuccessFailure(metadataNet, message: "", errorCode: 0)
  140. }
  141. }
  142. }
  143. } else {
  144. metadataNet.delegate?.deleteFileOrFolderSuccessFailure(metadataNet, message: "", errorCode: 0)
  145. }
  146. } else {
  147. if errorCode == 404 {
  148. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", metadataNet.fileID))
  149. if metadata != nil {
  150. self.deleteFile(metadata: metadata!, serverUrl: metadataNet.serverUrl)
  151. }
  152. }
  153. if message.length > 0 {
  154. appDelegate.messageNotification("_delete_", description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  155. }
  156. metadataNet.delegate?.deleteFileOrFolderSuccessFailure(metadataNet, message: message, errorCode: errorCode)
  157. }
  158. }
  159. // --------------------------------------------------------------------------------------------
  160. // MARK: Rename File or Folder
  161. // --------------------------------------------------------------------------------------------
  162. @objc func renameFileOrFolder(_ metadata: tableMetadata, fileName: String, delegate: AnyObject) {
  163. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  164. let fileName = CCUtility.removeForbiddenCharactersServer(fileName)!
  165. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  166. return
  167. }
  168. if fileName.count == 0 {
  169. return
  170. }
  171. if metadata.fileNameView == fileName {
  172. return
  173. }
  174. DispatchQueue.global(qos: .userInitiated).async {
  175. // Verify if exists the fileName TO
  176. var items: NSArray?
  177. guard NCNetworkingSync.sharedManager().readFile("\(String(describing: serverUrl))/\(fileName)", user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, items: &items) != nil else {
  178. DispatchQueue.main.async {
  179. let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: UIAlertControllerStyle.alert)
  180. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
  181. (result : UIAlertAction) -> Void in
  182. }
  183. alertController.addAction(okAction)
  184. delegate.present(alertController, animated: true, completion: nil)
  185. }
  186. return;
  187. }
  188. metadataNet.action = actionMoveFileOrFolder
  189. metadataNet.delegate = delegate
  190. metadataNet.fileID = metadata.fileID
  191. metadataNet.fileName = metadata.fileName
  192. metadataNet.fileNameTo = fileName
  193. metadataNet.fileNameView = metadata.fileNameView
  194. metadataNet.selector = selectorRename
  195. metadataNet.serverUrl = serverUrl
  196. metadataNet.serverUrlTo = serverUrl
  197. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  198. }
  199. }
  200. @objc func renameSuccess(_ metadataNet: CCMetadataNet) {
  201. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID = %@", metadataNet.fileID))
  202. if metadata?.directory == true {
  203. let directory = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileName)
  204. let directoryTo = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileNameTo)
  205. guard let directoryTable = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "serverUrl = %@", directory!)) else {
  206. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: "Internal error, ServerUrl not found" as NSString, errorCode: 0)
  207. return
  208. }
  209. NCManageDatabase.sharedInstance.setDirectory(serverUrl: directory!, serverUrlTo: directoryTo!, etag: nil, fileID: nil, encrypted: directoryTable.e2eEncrypted)
  210. } else {
  211. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadataNet.fileID, date: nil, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: metadataNet.fileNameTo)
  212. }
  213. metadataNet.delegate?.renameSuccess(metadataNet)
  214. }
  215. @objc func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  216. if message.length > 0 {
  217. var title : String = ""
  218. if metadataNet.selector == selectorRename {
  219. title = "_delete_"
  220. }
  221. if metadataNet.selector == selectorMove {
  222. title = "_move_"
  223. }
  224. appDelegate.messageNotification(title, description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  225. }
  226. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: message as NSString, errorCode: errorCode)
  227. }
  228. // --------------------------------------------------------------------------------------------
  229. // MARK: Search
  230. // --------------------------------------------------------------------------------------------
  231. @objc func search(_ serverUrl: String, fileName: String, depth: String, date: Date?, selector: String, delegate: AnyObject) {
  232. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(serverUrl) else {
  233. return
  234. }
  235. // Search DAV API
  236. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  237. metadataNet.action = actionSearch
  238. metadataNet.date = date
  239. metadataNet.delegate = delegate
  240. metadataNet.directoryID = directoryID
  241. metadataNet.fileName = fileName
  242. metadataNet.depth = depth
  243. metadataNet.priority = Operation.QueuePriority.high.rawValue
  244. metadataNet.selector = selector
  245. metadataNet.serverUrl = serverUrl
  246. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  247. }
  248. @objc func searchSuccess(_ metadataNet: CCMetadataNet, metadatas: [tableMetadata]) {
  249. metadataNet.delegate?.searchSuccess(metadataNet, metadatas: metadatas)
  250. }
  251. @objc func searchFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  252. metadataNet.delegate?.searchFailure(metadataNet, message: message, errorCode: errorCode)
  253. }
  254. // --------------------------------------------------------------------------------------------
  255. // MARK: Download Tumbnail
  256. // --------------------------------------------------------------------------------------------
  257. @objc func downloadTumbnail(_ metadata: tableMetadata, delegate: AnyObject) {
  258. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  259. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  260. return
  261. }
  262. metadataNet.action = actionDownloadThumbnail
  263. metadataNet.delegate = delegate
  264. metadataNet.fileID = metadata.fileID
  265. metadataNet.fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)
  266. metadataNet.fileNameView = metadata.fileNameView
  267. metadataNet.options = "m"
  268. metadataNet.priority = Operation.QueuePriority.low.rawValue
  269. metadataNet.selector = selectorDownloadThumbnail;
  270. metadataNet.serverUrl = serverUrl;
  271. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  272. }
  273. @objc func downloadThumbnailSuccess(_ metadataNet: CCMetadataNet) {
  274. metadataNet.delegate?.downloadThumbnailSuccess(metadataNet)
  275. }
  276. @objc func downloadThumbnailFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  277. NSLog("[LOG] Thumbnail Error \(metadataNet.fileName!) \(message) error %\(errorCode))")
  278. }
  279. // --------------------------------------------------------------------------------------------
  280. // MARK: Setting Favorite
  281. // --------------------------------------------------------------------------------------------
  282. @objc func settingFavorite(_ metadata: tableMetadata, favorite: Bool, delegate: AnyObject) {
  283. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  284. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  285. return
  286. }
  287. metadataNet.action = actionSettingFavorite
  288. metadataNet.delegate = delegate
  289. metadataNet.fileID = metadata.fileID
  290. metadataNet.fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)
  291. metadataNet.options = "\(favorite)"
  292. metadataNet.selector = selectorAddFavorite
  293. metadataNet.serverUrl = serverUrl;
  294. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  295. }
  296. @objc func settingFavoriteSuccess(_ metadataNet: CCMetadataNet) {
  297. metadataNet.delegate?.settingFavoriteSuccess(metadataNet)
  298. }
  299. @objc func settingFavoriteFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  300. appDelegate.messageNotification("_favorites_", description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  301. metadataNet.delegate?.settingFavoriteFailure(metadataNet, message: message, errorCode: errorCode)
  302. }
  303. // --------------------------------------------------------------------------------------------
  304. // MARK: Linsting Favorites
  305. // --------------------------------------------------------------------------------------------
  306. @objc func listingFavorites(_ serverUrl: String, delegate: AnyObject) {
  307. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  308. metadataNet.action = actionListingFavorites
  309. metadataNet.delegate = delegate
  310. metadataNet.serverUrl = serverUrl
  311. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  312. }
  313. @objc func listingFavoritesSuccess(_ metadataNet: CCMetadataNet, metadatas: [tableMetadata]) {
  314. metadataNet.delegate?.listingFavoritesSuccess(metadataNet, metadatas: metadatas)
  315. }
  316. @objc func listingFavoritesFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  317. metadataNet.delegate?.listingFavoritesFailure(metadataNet, message: message, errorCode: errorCode)
  318. }
  319. // --------------------------------------------------------------------------------------------
  320. // MARK: Utility
  321. // --------------------------------------------------------------------------------------------
  322. @objc func deleteFile(metadata: tableMetadata, serverUrl: String) {
  323. let fileNamePath = appDelegate.directoryUser + "/" + metadata.fileID
  324. do {
  325. try FileManager.default.removeItem(atPath: fileNamePath)
  326. } catch {
  327. // handle error
  328. }
  329. do {
  330. try FileManager.default.removeItem(atPath: fileNamePath + ".ico")
  331. } catch {
  332. // handle error
  333. }
  334. if metadata.directory {
  335. let dirForDelete = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)
  336. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: dirForDelete!)
  337. }
  338. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  339. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: nil)
  340. // E2EE (if exists the record)
  341. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account = %@ AND serverUrl = %@ AND fileNameIdentifier = %@", metadata.account, serverUrl, metadata.fileName))
  342. }
  343. }