FileProviderUtility.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // FileProviderData.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 27/05/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. class fileProviderUtility: NSObject {
  24. @objc static let sharedInstance: fileProviderUtility = {
  25. let instance = fileProviderUtility()
  26. return instance
  27. }()
  28. var fileManager = FileManager()
  29. func getAccountFromItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) -> String? {
  30. let ocId = itemIdentifier.rawValue
  31. return NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId))?.account
  32. }
  33. func getTableMetadataFromItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) -> tableMetadata? {
  34. let ocId = itemIdentifier.rawValue
  35. return NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId))
  36. }
  37. func getItemIdentifier(metadata: tableMetadata) -> NSFileProviderItemIdentifier {
  38. return NSFileProviderItemIdentifier(metadata.ocId)
  39. }
  40. func createocIdentifierOnFileSystem(metadata: tableMetadata) {
  41. let itemIdentifier = getItemIdentifier(metadata: metadata)
  42. if metadata.directory {
  43. CCUtility.getDirectoryProviderStorageOcId(itemIdentifier.rawValue)
  44. } else {
  45. CCUtility.getDirectoryProviderStorageOcId(itemIdentifier.rawValue, fileNameView: metadata.fileNameView)
  46. }
  47. }
  48. func getParentItemIdentifier(metadata: tableMetadata, homeServerUrl: String) -> NSFileProviderItemIdentifier? {
  49. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) {
  50. if directory.serverUrl == homeServerUrl {
  51. return NSFileProviderItemIdentifier(NSFileProviderItemIdentifier.rootContainer.rawValue)
  52. } else {
  53. // get the metadata.ocId of parent Directory
  54. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", directory.ocId)) {
  55. let identifier = getItemIdentifier(metadata: metadata)
  56. return identifier
  57. }
  58. }
  59. }
  60. return nil
  61. }
  62. func getTableDirectoryFromParentItemIdentifier(_ parentItemIdentifier: NSFileProviderItemIdentifier, account: String, homeServerUrl: String) -> tableDirectory? {
  63. var predicate: NSPredicate
  64. if parentItemIdentifier == .rootContainer {
  65. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@", account, homeServerUrl)
  66. } else {
  67. guard let metadata = getTableMetadataFromItemIdentifier(parentItemIdentifier) else {
  68. return nil
  69. }
  70. predicate = NSPredicate(format: "ocId == %@", metadata.ocId)
  71. }
  72. guard let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: predicate) else {
  73. return nil
  74. }
  75. return directory
  76. }
  77. // MARK: -
  78. func copyFile(_ atPath: String, toPath: String) -> Error? {
  79. var errorResult: Error?
  80. if !fileManager.fileExists(atPath: atPath) {
  81. return NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo:[:])
  82. }
  83. do {
  84. try fileManager.removeItem(atPath: toPath)
  85. } catch let error {
  86. print("error: \(error)")
  87. }
  88. do {
  89. try fileManager.copyItem(atPath: atPath, toPath: toPath)
  90. } catch let error {
  91. errorResult = error
  92. }
  93. return errorResult
  94. }
  95. func moveFile(_ atPath: String, toPath: String) -> Error? {
  96. var errorResult: Error?
  97. if atPath == toPath {
  98. return nil
  99. }
  100. if !fileManager.fileExists(atPath: atPath) {
  101. return NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo:[:])
  102. }
  103. do {
  104. try fileManager.removeItem(atPath: toPath)
  105. } catch let error {
  106. print("error: \(error)")
  107. }
  108. do {
  109. try fileManager.moveItem(atPath: atPath, toPath: toPath)
  110. } catch let error {
  111. errorResult = error
  112. }
  113. return errorResult
  114. }
  115. func deleteFile(_ atPath: String) -> Error? {
  116. var errorResult: Error?
  117. do {
  118. try fileManager.removeItem(atPath: atPath)
  119. } catch let error {
  120. errorResult = error
  121. }
  122. return errorResult
  123. }
  124. func fileExists(atPath: String) -> Bool {
  125. return fileManager.fileExists(atPath: atPath)
  126. }
  127. }