FileProviderData.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // FileProviderData.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 27/05/18.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 FileProvider
  24. class FileProviderData: NSObject {
  25. var account = ""
  26. var accountUser = ""
  27. var accountUserID = ""
  28. var accountPassword = ""
  29. var accountUrl = ""
  30. var homeServerUrl = ""
  31. var directoryUser = ""
  32. // Directory
  33. var groupURL: URL?
  34. var fileProviderStorageURL: URL?
  35. //var listFavoriteIdentifierRank = [String:NSNumber]()
  36. func setupActiveAccount() -> Bool {
  37. groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.sharedInstance.capabilitiesGroups)
  38. fileProviderStorageURL = groupURL!.appendingPathComponent(k_assetLocalIdentifierFileProviderStorage)
  39. // Create dir File Provider Storage
  40. do {
  41. try FileManager.default.createDirectory(atPath: fileProviderStorageURL!.path, withIntermediateDirectories: true, attributes: nil)
  42. } catch let error as NSError {
  43. NSLog("Unable to create directory \(error.debugDescription)")
  44. }
  45. guard let activeAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
  46. return false
  47. }
  48. account = activeAccount.account
  49. accountUser = activeAccount.user
  50. accountUserID = activeAccount.userID
  51. accountPassword = activeAccount.password
  52. accountUrl = activeAccount.url
  53. homeServerUrl = CCUtility.getHomeServerUrlActiveUrl(activeAccount.url)
  54. directoryUser = CCUtility.getDirectoryActiveUser(activeAccount.user, activeUrl: activeAccount.url)
  55. return true
  56. }
  57. func getTableMetadataFromItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) -> tableMetadata? {
  58. let fileID = itemIdentifier.rawValue
  59. return NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID))
  60. }
  61. func getItemIdentifier(metadata: tableMetadata) -> NSFileProviderItemIdentifier {
  62. return NSFileProviderItemIdentifier(metadata.fileID)
  63. }
  64. func createFileIdentifierOnFileSystem(metadata: tableMetadata) {
  65. let itemIdentifier = getItemIdentifier(metadata: metadata)
  66. let identifierPath = fileProviderStorageURL!.path + "/" + itemIdentifier.rawValue
  67. let fileIdentifier = identifierPath + "/" + metadata.fileName
  68. do {
  69. try FileManager.default.createDirectory(atPath: identifierPath, withIntermediateDirectories: true, attributes: nil)
  70. } catch { }
  71. // If do not exists create file with size = 0
  72. if FileManager.default.fileExists(atPath: fileIdentifier) == false {
  73. FileManager.default.createFile(atPath: fileIdentifier, contents: nil, attributes: nil)
  74. }
  75. }
  76. func getParentItemIdentifier(metadata: tableMetadata) -> NSFileProviderItemIdentifier? {
  77. /* ONLY iOS 11*/
  78. guard #available(iOS 11, *) else { return NSFileProviderItemIdentifier("") }
  79. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND directoryID = %@", account, metadata.directoryID)) {
  80. if directory.serverUrl == homeServerUrl {
  81. return NSFileProviderItemIdentifier(NSFileProviderItemIdentifier.rootContainer.rawValue)
  82. } else {
  83. // get the metadata.FileID of parent Directory
  84. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, directory.fileID)) {
  85. let identifier = getItemIdentifier(metadata: metadata)
  86. return identifier
  87. }
  88. }
  89. }
  90. return nil
  91. }
  92. func getTableDirectoryFromParentItemIdentifier(_ parentItemIdentifier: NSFileProviderItemIdentifier) -> tableDirectory? {
  93. /* ONLY iOS 11*/
  94. guard #available(iOS 11, *) else { return nil }
  95. var predicate: NSPredicate
  96. if parentItemIdentifier == .rootContainer {
  97. predicate = NSPredicate(format: "account = %@ AND serverUrl = %@", account, homeServerUrl)
  98. } else {
  99. guard let metadata = getTableMetadataFromItemIdentifier(parentItemIdentifier) else {
  100. return nil
  101. }
  102. predicate = NSPredicate(format: "account = %@ AND fileID = %@", account, metadata.fileID)
  103. }
  104. guard let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: predicate) else {
  105. return nil
  106. }
  107. return directory
  108. }
  109. }