FileProviderUtility.swift 4.5 KB

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