NCTrash+Networking.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // NCTrash+Networking.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/03/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import Realm
  22. import UIKit
  23. import NextcloudKit
  24. extension NCTrash {
  25. @objc func loadListingTrash() {
  26. NextcloudKit.shared.listingTrash(filename: filename, showHiddenFiles: false) { task in
  27. self.dataSourceTask = task
  28. self.collectionView.reloadData()
  29. } completion: { account, items, _, error in
  30. self.refreshControl.endRefreshing()
  31. if account == self.appDelegate.account {
  32. NCManageDatabase.shared.deleteTrash(filePath: self.getFilePath(), account: account)
  33. NCManageDatabase.shared.addTrash(account: account, items: items)
  34. }
  35. self.reloadDataSource()
  36. if error != .success {
  37. NCContentPresenter().showError(error: error)
  38. }
  39. }
  40. }
  41. func restoreItem(with fileId: String) {
  42. guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else { return }
  43. let fileNameFrom = tableTrash.filePath + tableTrash.fileName
  44. let fileNameTo = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + appDelegate.userId + "/restore/" + tableTrash.fileName
  45. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true) { account, error in
  46. guard error == .success, account == self.appDelegate.account else {
  47. NCContentPresenter().showError(error: error)
  48. return
  49. }
  50. NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
  51. self.reloadDataSource()
  52. }
  53. }
  54. func emptyTrash() {
  55. let serverUrlFileName = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + appDelegate.userId + "/trash"
  56. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName) { account, error in
  57. guard error == .success, account == self.appDelegate.account else {
  58. NCContentPresenter().showError(error: error)
  59. return
  60. }
  61. NCManageDatabase.shared.deleteTrash(fileId: nil, account: self.appDelegate.account)
  62. self.reloadDataSource()
  63. }
  64. }
  65. func deleteItem(with fileId: String) {
  66. guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else { return }
  67. let serverUrlFileName = tableTrash.filePath + tableTrash.fileName
  68. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName) { account, error in
  69. guard error == .success, account == self.appDelegate.account else {
  70. NCContentPresenter().showError(error: error)
  71. return
  72. }
  73. NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
  74. self.reloadDataSource()
  75. }
  76. }
  77. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  78. let fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(tableTrash.fileId, etag: tableTrash.fileName)
  79. let fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)
  80. NextcloudKit.shared.downloadPreview(fileNamePathOrFileId: tableTrash.fileId,
  81. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  82. widthPreview: NCGlobal.shared.sizePreview,
  83. heightPreview: NCGlobal.shared.sizePreview,
  84. fileNameIconLocalPath: fileNameIconLocalPath,
  85. sizeIcon: NCGlobal.shared.sizeIcon,
  86. etag: nil,
  87. endpointTrashbin: true) { account, _, imageIcon, _, _, error in
  88. guard error == .success, let imageIcon = imageIcon, account == self.appDelegate.account,
  89. let cell = self.collectionView.cellForItem(at: indexPath) else { return }
  90. if let cell = cell as? NCTrashListCell {
  91. cell.imageItem.image = imageIcon
  92. } else if let cell = cell as? NCGridCell {
  93. cell.imageItem.image = imageIcon
  94. } // else: undefined cell
  95. }
  96. }
  97. }