FileProviderItem.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // FileProviderItem.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 26/03/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 FileProviderItem: NSObject, NSFileProviderItem {
  25. // TODO: implement an initializer to create an item from your extension's backing model
  26. // TODO: implement the accessors to return the values from your extension's backing model
  27. // Providing Required Properties
  28. var itemIdentifier: NSFileProviderItemIdentifier // The item's persistent identifier
  29. var filename: String = "" // The item's filename
  30. var typeIdentifier: String = "" // The item's uniform type identifiers
  31. var capabilities: NSFileProviderItemCapabilities { // The item's capabilities
  32. if (self.isDirectory) {
  33. return [ .allowsAddingSubItems, .allowsContentEnumerating, .allowsReading, .allowsDeleting ]
  34. } else {
  35. return [ .allowsReading, .allowsDeleting, .allowsWriting ]
  36. }
  37. }
  38. // Managing Content
  39. var childItemCount: NSNumber? // The number of items contained by this item
  40. var documentSize: NSNumber? // The document's size, in bytes
  41. // Specifying Content Location
  42. var parentItemIdentifier: NSFileProviderItemIdentifier // The persistent identifier of the item's parent folder
  43. var isTrashed: Bool = false // A Boolean value that indicates whether an item is in the trash
  44. // Tracking Usage
  45. var contentModificationDate: Date? // The date the item was last modified
  46. var creationDate: Date? // The date the item was created
  47. //var lastUsedDate: Date? // The date the item was last used
  48. // Tracking Versions
  49. var versionIdentifier: Data? // A data value used to determine when the item changes
  50. var isMostRecentVersionDownloaded: Bool = false // A Boolean value that indicates whether the item is the most recent version downloaded from the server
  51. // Monitoring File Transfers
  52. var isUploading: Bool = false // A Boolean value that indicates whether the item is currently uploading to your remote server
  53. var isUploaded: Bool = true // A Boolean value that indicates whether the item has been uploaded to your remote server
  54. var uploadingError: Error? // An error that occurred while uploading to your remote server
  55. var isDownloading: Bool = false // A Boolean value that indicates whether the item is currently downloading from your remote server
  56. var isDownloaded: Bool = true // A Boolean value that indicates whether the item has been downloaded from your remote server
  57. var downloadingError: Error? // An error that occurred while downloading the item
  58. var tagData: Data? // Tag
  59. var isDirectory = false
  60. init(metadata: tableMetadata, serverUrl: String) {
  61. self.contentModificationDate = metadata.date as Date
  62. self.creationDate = metadata.date as Date
  63. self.documentSize = NSNumber(value: metadata.size)
  64. self.filename = metadata.fileNameView
  65. self.itemIdentifier = NSFileProviderItemIdentifier("\(metadata.fileID)")
  66. self.isDirectory = metadata.directory
  67. //
  68. if uploadingIdentifier.contains(metadata.fileID) {
  69. self.isUploading = true
  70. self.isUploaded = false
  71. }
  72. // parentItemIdentifier
  73. if #available(iOSApplicationExtension 11.0, *) {
  74. self.parentItemIdentifier = NSFileProviderItemIdentifier.rootContainer
  75. // NOT .rootContainer
  76. if (serverUrl != homeServerUrl) {
  77. if let directoryParent = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND directoryID = %@", metadata.account, metadata.directoryID)) {
  78. if let metadataParent = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", metadata.account, directoryParent.fileID)) {
  79. self.parentItemIdentifier = NSFileProviderItemIdentifier(metadataParent.fileID)
  80. }
  81. }
  82. }
  83. } else {
  84. // < iOS 11
  85. self.parentItemIdentifier = NSFileProviderItemIdentifier("")
  86. }
  87. // typeIdentifier
  88. if let fileType = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata) {
  89. self.typeIdentifier = fileType
  90. }
  91. self.versionIdentifier = metadata.etag.data(using: .utf8)
  92. // Verify file exists on cache
  93. if (!metadata.directory) {
  94. let identifierPathUrl = groupURL!.appendingPathComponent("File Provider Storage").appendingPathComponent(metadata.fileID)
  95. let filePath = "\(identifierPathUrl.path)/\(metadata.fileNameView)"
  96. var fileSize = 0 as Double
  97. do {
  98. let attributes = try FileManager.default.attributesOfItem(atPath: filePath)
  99. fileSize = attributes[FileAttributeKey.size] as! Double
  100. } catch let error {
  101. print("error: \(error)")
  102. }
  103. if fileSize == 0 {
  104. self.isDownloaded = false
  105. self.isMostRecentVersionDownloaded = false
  106. } else {
  107. self.documentSize = NSNumber(value:fileSize)
  108. self.isDownloaded = true
  109. self.isMostRecentVersionDownloaded = true
  110. }
  111. }
  112. // Tag
  113. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "account = %@ AND fileID = %@", metadata.account, metadata.fileID)) {
  114. tagData = tableTag.tagIOS
  115. }
  116. }
  117. }