FileProviderUtility.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 { return nil }
  68. predicate = NSPredicate(format: "ocId == %@", metadata.ocId)
  69. }
  70. guard let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: predicate) else { return nil }
  71. return directory
  72. }
  73. // MARK: -
  74. func copyFile(_ atPath: String, toPath: String) -> Error? {
  75. var errorResult: Error?
  76. if !fileManager.fileExists(atPath: atPath) { return NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo:[:]) }
  77. do {
  78. try fileManager.removeItem(atPath: toPath)
  79. } catch let error {
  80. print("error: \(error)")
  81. }
  82. do {
  83. try fileManager.copyItem(atPath: atPath, toPath: toPath)
  84. } catch let error {
  85. errorResult = error
  86. }
  87. return errorResult
  88. }
  89. func moveFile(_ atPath: String, toPath: String) -> Error? {
  90. var errorResult: Error?
  91. if atPath == toPath { return nil }
  92. if !fileManager.fileExists(atPath: atPath) { return NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo:[:]) }
  93. do {
  94. try fileManager.removeItem(atPath: toPath)
  95. } catch let error {
  96. print("error: \(error)")
  97. }
  98. do {
  99. try fileManager.moveItem(atPath: atPath, toPath: toPath)
  100. } catch let error {
  101. errorResult = error
  102. }
  103. return errorResult
  104. }
  105. func deleteFile(_ atPath: String) -> Error? {
  106. var errorResult: Error?
  107. do {
  108. try fileManager.removeItem(atPath: atPath)
  109. } catch let error {
  110. errorResult = error
  111. }
  112. return errorResult
  113. }
  114. func fileExists(atPath: String) -> Bool {
  115. return fileManager.fileExists(atPath: atPath)
  116. }
  117. }