FileProviderItem.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 identifierPath = fileProviderStorageURL!.path + "/" + metadata.fileID
  91. let fileIdentifier = identifierPath + "/" + metadata.fileNameView
  92. var fileSize = 0 as Double
  93. do {
  94. try FileManager.default.createDirectory(atPath: identifierPath, withIntermediateDirectories: true, attributes: nil)
  95. } catch let error {
  96. print("error: \(error)")
  97. }
  98. // If do not exists create file with size = 0
  99. if fileManagerExtension.fileExists(atPath: fileIdentifier) == false {
  100. fileManagerExtension.createFile(atPath: fileIdentifier, contents: nil, attributes: nil)
  101. }
  102. do {
  103. let attributes = try fileManagerExtension.attributesOfItem(atPath: fileIdentifier)
  104. fileSize = attributes[FileAttributeKey.size] as! Double
  105. } catch let error {
  106. print("error: \(error)")
  107. }
  108. // Download
  109. if fileSize == 0 {
  110. self.isDownloaded = false
  111. self.isMostRecentVersionDownloaded = false
  112. } else {
  113. self.documentSize = NSNumber(value:fileSize)
  114. self.isDownloaded = true
  115. self.isMostRecentVersionDownloaded = true
  116. }
  117. // Upload
  118. /*
  119. let queue = NCManageDatabase.sharedInstance.getQueueUpload(predicate: NSPredicate(format: "account = %@ AND (path = %@ || path = %@)", account, changeDocumentPath, importDocumentPath))
  120. if queue?.count == 0 {
  121. self.isUploading = false
  122. self.isUploaded = true
  123. } else {
  124. self.isUploading = true
  125. self.isUploaded = false
  126. }
  127. */
  128. } else {
  129. /*
  130. if #available(iOSApplicationExtension 11.0, *) {
  131. let rank = listFavoriteIdentifierRank[metadata.fileID]
  132. if (rank == nil) {
  133. favoriteRank = NSNumber(value: Int64(NSFileProviderFavoriteRankUnranked))
  134. } else {
  135. favoriteRank = listFavoriteIdentifierRank[metadata.fileID]
  136. }
  137. }
  138. */
  139. }
  140. // Tag
  141. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "account = %@ AND fileID = %@", metadata.account, metadata.fileID)) {
  142. tagData = tableTag.tagIOS
  143. }
  144. // Removed (if exists) this Item from listUpdate
  145. var counter = 0
  146. for updateItem in listUpdateItems {
  147. if updateItem.itemIdentifier.rawValue == itemIdentifier.rawValue {
  148. listUpdateItems.remove(at: counter)
  149. break;
  150. }
  151. counter += 1
  152. }
  153. }
  154. }