FileProviderUtility.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import UIKit
  24. class fileProviderUtility: NSObject {
  25. let fileManager = FileManager()
  26. let utilityFileSystem = NCUtilityFileSystem()
  27. let database = NCManageDatabase.shared
  28. func getAccountFromItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) -> String? {
  29. let ocId = itemIdentifier.rawValue
  30. return self.database.getMetadataFromOcId(ocId)?.account
  31. }
  32. func getTableMetadataFromItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) -> tableMetadata? {
  33. let ocId = itemIdentifier.rawValue
  34. return self.database.getMetadataFromOcId(ocId)
  35. }
  36. func getItemIdentifier(metadata: tableMetadata) -> NSFileProviderItemIdentifier {
  37. return NSFileProviderItemIdentifier(metadata.ocId)
  38. }
  39. func getParentItemIdentifier(metadata: tableMetadata) -> NSFileProviderItemIdentifier? {
  40. let homeServerUrl = utilityFileSystem.getHomeServer(session: fileProviderData.shared.session)
  41. if let directory = self.database.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) {
  42. if directory.serverUrl == homeServerUrl {
  43. return NSFileProviderItemIdentifier(NSFileProviderItemIdentifier.rootContainer.rawValue)
  44. } else {
  45. // get the metadata.ocId of parent Directory
  46. if let metadata = self.database.getMetadataFromOcId(directory.ocId) {
  47. let identifier = getItemIdentifier(metadata: metadata)
  48. return identifier
  49. }
  50. }
  51. }
  52. return nil
  53. }
  54. func getTableDirectoryFromParentItemIdentifier(_ parentItemIdentifier: NSFileProviderItemIdentifier, account: String, homeServerUrl: String) -> tableDirectory? {
  55. var predicate: NSPredicate
  56. if parentItemIdentifier == .rootContainer {
  57. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@", account, homeServerUrl)
  58. } else {
  59. guard let metadata = getTableMetadataFromItemIdentifier(parentItemIdentifier) else { return nil }
  60. predicate = NSPredicate(format: "ocId == %@", metadata.ocId)
  61. }
  62. guard let directory = self.database.getTableDirectory(predicate: predicate) else { return nil }
  63. return directory
  64. }
  65. func copyFile(_ atPath: String, toPath: String) {
  66. if !fileManager.fileExists(atPath: atPath) { return }
  67. do {
  68. try fileManager.removeItem(atPath: toPath)
  69. } catch let error {
  70. print("Error: \(error.localizedDescription)")
  71. }
  72. do {
  73. try fileManager.copyItem(atPath: atPath, toPath: toPath)
  74. } catch let error {
  75. print("Error: \(error.localizedDescription)")
  76. }
  77. }
  78. func moveFile(_ atPath: String, toPath: String) {
  79. if !fileManager.fileExists(atPath: atPath) { return }
  80. do {
  81. try fileManager.removeItem(atPath: toPath)
  82. } catch let error {
  83. print("Error: \(error.localizedDescription)")
  84. }
  85. do {
  86. try fileManager.moveItem(atPath: atPath, toPath: toPath)
  87. } catch let error {
  88. print("Error: \(error.localizedDescription)")
  89. }
  90. }
  91. func getFileSize(from url: URL) -> Int64? {
  92. do {
  93. let attributes = try fileManager.attributesOfItem(atPath: url.path)
  94. if let fileSize = attributes[FileAttributeKey.size] as? Int64 {
  95. return fileSize
  96. } else {
  97. print("Failed to retrieve file size.")
  98. return nil
  99. }
  100. } catch {
  101. print("Error: \(error.localizedDescription)")
  102. return nil
  103. }
  104. }
  105. }