CCActions.swift 22 KB

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