CCActions.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. NCManageDatabase.sharedInstance.getDirectoryE2ETokenLock(serverUrl: metadataNet.serverUrl, completion: { (token) in
  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. }
  135. })
  136. if (errorRebuild == nil && errorUnlock == nil) {
  137. DispatchQueue.main.async {
  138. metadataNet.delegate?.deleteFileOrFolderSuccessFailure(metadataNet, message: "", errorCode: 0)
  139. }
  140. }
  141. }
  142. } else {
  143. metadataNet.delegate?.deleteFileOrFolderSuccessFailure(metadataNet, message: "", errorCode: 0)
  144. }
  145. } else {
  146. if errorCode == 404 {
  147. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", metadataNet.fileID))
  148. if metadata != nil {
  149. self.deleteFile(metadata: metadata!, serverUrl: metadataNet.serverUrl)
  150. }
  151. }
  152. if message.length > 0 {
  153. appDelegate.messageNotification("_delete_", description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  154. }
  155. metadataNet.delegate?.deleteFileOrFolderSuccessFailure(metadataNet, message: message, errorCode: errorCode)
  156. }
  157. }
  158. // --------------------------------------------------------------------------------------------
  159. // MARK: Rename File or Folder
  160. // --------------------------------------------------------------------------------------------
  161. @objc func renameFileOrFolder(_ metadata: tableMetadata, fileName: String, delegate: AnyObject) {
  162. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  163. let fileName = CCUtility.removeForbiddenCharactersServer(fileName)!
  164. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  165. return
  166. }
  167. if fileName.count == 0 {
  168. return
  169. }
  170. if metadata.fileNameView == fileName {
  171. return
  172. }
  173. DispatchQueue.global(qos: .userInitiated).async {
  174. // Verify if exists the fileName TO
  175. var items: NSArray?
  176. guard NCNetworkingSync.sharedManager().readFile("\(String(describing: serverUrl))/\(fileName)", user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, items: &items) != nil else {
  177. DispatchQueue.main.async {
  178. let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: UIAlertControllerStyle.alert)
  179. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
  180. (result : UIAlertAction) -> Void in
  181. }
  182. alertController.addAction(okAction)
  183. delegate.present(alertController, animated: true, completion: nil)
  184. }
  185. return;
  186. }
  187. metadataNet.action = actionMoveFileOrFolder
  188. metadataNet.delegate = delegate
  189. metadataNet.fileID = metadata.fileID
  190. metadataNet.fileName = metadata.fileName
  191. metadataNet.fileNameTo = fileName
  192. metadataNet.fileNameView = metadata.fileNameView
  193. metadataNet.selector = selectorRename
  194. metadataNet.serverUrl = serverUrl
  195. metadataNet.serverUrlTo = serverUrl
  196. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  197. }
  198. }
  199. @objc func renameSuccess(_ metadataNet: CCMetadataNet) {
  200. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID = %@", metadataNet.fileID))
  201. if metadata?.directory == true {
  202. let directory = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileName)
  203. let directoryTo = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileNameTo)
  204. guard let directoryTable = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "serverUrl = %@", directory!)) else {
  205. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: "Internal error, ServerUrl not found" as NSString, errorCode: 0)
  206. return
  207. }
  208. NCManageDatabase.sharedInstance.setDirectory(serverUrl: directory!, serverUrlTo: directoryTo!, etag: nil, fileID: nil, encrypted: directoryTable.e2eEncrypted)
  209. } else {
  210. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadataNet.fileID, date: nil, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: metadataNet.fileNameTo)
  211. }
  212. metadataNet.delegate?.renameSuccess(metadataNet)
  213. }
  214. @objc func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  215. if message.length > 0 {
  216. var title : String = ""
  217. if metadataNet.selector == selectorRename {
  218. title = "_delete_"
  219. }
  220. if metadataNet.selector == selectorMove {
  221. title = "_move_"
  222. }
  223. appDelegate.messageNotification(title, description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  224. }
  225. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: message as NSString, errorCode: errorCode)
  226. }
  227. // --------------------------------------------------------------------------------------------
  228. // MARK: Search
  229. // --------------------------------------------------------------------------------------------
  230. @objc func search(_ serverUrl: String, fileName: String, depth: String, date: Date?, selector: String, delegate: AnyObject) {
  231. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(serverUrl) else {
  232. return
  233. }
  234. // Search DAV API
  235. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  236. metadataNet.action = actionSearch
  237. metadataNet.date = date
  238. metadataNet.delegate = delegate
  239. metadataNet.directoryID = directoryID
  240. metadataNet.fileName = fileName
  241. metadataNet.depth = depth
  242. metadataNet.priority = Operation.QueuePriority.high.rawValue
  243. metadataNet.selector = selector
  244. metadataNet.serverUrl = serverUrl
  245. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  246. }
  247. @objc func searchSuccess(_ metadataNet: CCMetadataNet, metadatas: [tableMetadata]) {
  248. metadataNet.delegate?.searchSuccess(metadataNet, metadatas: metadatas)
  249. }
  250. @objc func searchFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  251. metadataNet.delegate?.searchFailure(metadataNet, message: message, errorCode: errorCode)
  252. }
  253. // --------------------------------------------------------------------------------------------
  254. // MARK: Download Tumbnail
  255. // --------------------------------------------------------------------------------------------
  256. @objc func downloadTumbnail(_ metadata: tableMetadata, delegate: AnyObject) {
  257. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  258. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  259. return
  260. }
  261. metadataNet.action = actionDownloadThumbnail
  262. metadataNet.delegate = delegate
  263. metadataNet.fileID = metadata.fileID
  264. metadataNet.fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)
  265. metadataNet.fileNameView = metadata.fileNameView
  266. metadataNet.options = "m"
  267. metadataNet.priority = Operation.QueuePriority.low.rawValue
  268. metadataNet.selector = selectorDownloadThumbnail;
  269. metadataNet.serverUrl = serverUrl;
  270. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  271. }
  272. @objc func downloadThumbnailSuccess(_ metadataNet: CCMetadataNet) {
  273. metadataNet.delegate?.downloadThumbnailSuccess(metadataNet)
  274. }
  275. @objc func downloadThumbnailFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  276. NSLog("[LOG] Thumbnail Error \(metadataNet.fileName!) \(message) error %\(errorCode))")
  277. }
  278. // --------------------------------------------------------------------------------------------
  279. // MARK: Setting Favorite
  280. // --------------------------------------------------------------------------------------------
  281. @objc func settingFavorite(_ metadata: tableMetadata, favorite: Bool, delegate: AnyObject) {
  282. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  283. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  284. return
  285. }
  286. metadataNet.action = actionSettingFavorite
  287. metadataNet.delegate = delegate
  288. metadataNet.fileID = metadata.fileID
  289. metadataNet.fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)
  290. metadataNet.options = "\(favorite)"
  291. metadataNet.selector = selectorAddFavorite
  292. metadataNet.serverUrl = serverUrl;
  293. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  294. }
  295. @objc func settingFavoriteSuccess(_ metadataNet: CCMetadataNet) {
  296. metadataNet.delegate?.settingFavoriteSuccess(metadataNet)
  297. }
  298. @objc func settingFavoriteFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  299. appDelegate.messageNotification("_favorites_", description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  300. metadataNet.delegate?.settingFavoriteFailure(metadataNet, message: message, errorCode: errorCode)
  301. }
  302. // --------------------------------------------------------------------------------------------
  303. // MARK: Linsting Favorites
  304. // --------------------------------------------------------------------------------------------
  305. @objc func listingFavorites(_ serverUrl: String, delegate: AnyObject) {
  306. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  307. metadataNet.action = actionListingFavorites
  308. metadataNet.delegate = delegate
  309. metadataNet.serverUrl = serverUrl
  310. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  311. }
  312. @objc func listingFavoritesSuccess(_ metadataNet: CCMetadataNet, metadatas: [tableMetadata]) {
  313. metadataNet.delegate?.listingFavoritesSuccess(metadataNet, metadatas: metadatas)
  314. }
  315. @objc func listingFavoritesFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  316. metadataNet.delegate?.listingFavoritesFailure(metadataNet, message: message, errorCode: errorCode)
  317. }
  318. // --------------------------------------------------------------------------------------------
  319. // MARK: Utility
  320. // --------------------------------------------------------------------------------------------
  321. @objc func deleteFile(metadata: tableMetadata, serverUrl: String) {
  322. let fileNamePath = appDelegate.directoryUser + "/" + metadata.fileID
  323. do {
  324. try FileManager.default.removeItem(atPath: fileNamePath)
  325. } catch {
  326. // handle error
  327. }
  328. do {
  329. try FileManager.default.removeItem(atPath: fileNamePath + ".ico")
  330. } catch {
  331. // handle error
  332. }
  333. if metadata.directory {
  334. let dirForDelete = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)
  335. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: dirForDelete!)
  336. }
  337. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  338. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: nil)
  339. // E2EE (if exists the record)
  340. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account = %@ AND serverUrl = %@ AND fileNameIdentifier = %@", metadata.account, serverUrl, metadata.fileName))
  341. }
  342. }