FileProviderItem.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // Providing Required Properties
  26. var itemIdentifier: NSFileProviderItemIdentifier // The item's persistent identifier
  27. var filename: String = "" // The item's filename
  28. var typeIdentifier: String = "" // The item's uniform type identifiers
  29. var capabilities: NSFileProviderItemCapabilities { // The item's capabilities
  30. if (self.isDirectory) {
  31. return [ .allowsAddingSubItems, .allowsContentEnumerating, .allowsReading, .allowsDeleting, .allowsRenaming ]
  32. } else {
  33. return [ .allowsWriting, .allowsReading, .allowsDeleting, .allowsRenaming, .allowsReparenting ]
  34. }
  35. }
  36. // Managing Content
  37. var childItemCount: NSNumber? // The number of items contained by this item
  38. var documentSize: NSNumber? // The document's size, in bytes
  39. // Specifying Content Location
  40. var parentItemIdentifier: NSFileProviderItemIdentifier // The persistent identifier of the item's parent folder
  41. var isTrashed: Bool = false // A Boolean value that indicates whether an item is in the trash
  42. // Tracking Usage
  43. var contentModificationDate: Date? // The date the item was last modified
  44. var creationDate: Date? // The date the item was created
  45. //var lastUsedDate: Date? // The date the item was last used
  46. // Tracking Versions
  47. var versionIdentifier: Data? // A data value used to determine when the item changes
  48. var isMostRecentVersionDownloaded: Bool = false // A Boolean value that indicates whether the item is the most recent version downloaded from the server
  49. // Monitoring File Transfers
  50. var isUploading: Bool = false // A Boolean value that indicates whether the item is currently uploading to your remote server
  51. var isUploaded: Bool = true // A Boolean value that indicates whether the item has been uploaded to your remote server
  52. var uploadingError: Error? // An error that occurred while uploading to your remote server
  53. var isDownloading: Bool = false // A Boolean value that indicates whether the item is currently downloading from your remote server
  54. var isDownloaded: Bool = true // A Boolean value that indicates whether the item has been downloaded from your remote server
  55. var downloadingError: Error? // An error that occurred while downloading the item
  56. var tagData: Data? // Tag
  57. var favoriteRank: NSNumber? // Favorite
  58. var isDirectory = false
  59. init(metadata: tableMetadata, serverUrl: String) {
  60. self.contentModificationDate = metadata.date as Date
  61. self.creationDate = metadata.date as Date
  62. self.documentSize = NSNumber(value: metadata.size)
  63. self.filename = metadata.fileNameView
  64. self.isDirectory = metadata.directory
  65. // parentItemIdentifier
  66. if #available(iOSApplicationExtension 11.0, *) {
  67. self.parentItemIdentifier = NSFileProviderItemIdentifier(NSFileProviderItemIdentifier.rootContainer.rawValue)
  68. // NOT .rootContainer
  69. if (serverUrl != homeServerUrl) {
  70. if let directoryParent = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND directoryID = %@", metadata.account, metadata.directoryID)) {
  71. if let metadataParent = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", metadata.account, directoryParent.fileID)) {
  72. self.parentItemIdentifier = NSFileProviderItemIdentifier(metadataParent.fileID)
  73. }
  74. }
  75. }
  76. // itemIdentifier
  77. self.itemIdentifier = NSFileProviderItemIdentifier(metadata.fileID)
  78. } else {
  79. // < iOS 11
  80. self.parentItemIdentifier = NSFileProviderItemIdentifier("")
  81. self.itemIdentifier = NSFileProviderItemIdentifier("")
  82. }
  83. // typeIdentifier
  84. if let fileType = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata) {
  85. self.typeIdentifier = fileType
  86. }
  87. self.versionIdentifier = metadata.etag.data(using: .utf8)
  88. // Verify file exists on cache
  89. if (!metadata.directory) {
  90. let fileIdentifier = fileProviderStorageURL!.path + "/" + metadata.fileID + "/" + metadata.fileNameView
  91. var fileSize = 0 as Double
  92. do {
  93. let attributes = try fileManagerExtension.attributesOfItem(atPath: fileIdentifier)
  94. fileSize = attributes[FileAttributeKey.size] as! Double
  95. } catch let error {
  96. print("error: \(error)")
  97. }
  98. // Download
  99. if fileSize == 0 {
  100. self.isDownloaded = false
  101. self.isMostRecentVersionDownloaded = false
  102. } else {
  103. self.documentSize = NSNumber(value:fileSize)
  104. self.isDownloaded = true
  105. self.isMostRecentVersionDownloaded = true
  106. }
  107. // Upload
  108. /*
  109. let queue = NCManageDatabase.sharedInstance.getQueueUpload(predicate: NSPredicate(format: "account = %@ AND (path = %@ || path = %@)", account, changeDocumentPath, importDocumentPath))
  110. if queue?.count == 0 {
  111. self.isUploading = false
  112. self.isUploaded = true
  113. } else {
  114. self.isUploading = true
  115. self.isUploaded = false
  116. }
  117. */
  118. } else {
  119. /*
  120. if #available(iOSApplicationExtension 11.0, *) {
  121. let rank = listFavoriteIdentifierRank[metadata.fileID]
  122. if (rank == nil) {
  123. favoriteRank = NSNumber(value: Int64(NSFileProviderFavoriteRankUnranked))
  124. } else {
  125. favoriteRank = listFavoriteIdentifierRank[metadata.fileID]
  126. }
  127. }
  128. */
  129. }
  130. // Tag
  131. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "account = %@ AND fileID = %@", metadata.account, metadata.fileID)) {
  132. tagData = tableTag.tagIOS
  133. }
  134. // Removed (if exists) this Item from listUpdate
  135. var counter = 0
  136. for updateItem in listUpdateItems {
  137. if updateItem.itemIdentifier.rawValue == itemIdentifier.rawValue {
  138. listUpdateItems.remove(at: counter)
  139. break;
  140. }
  141. counter += 1
  142. }
  143. }
  144. }