FileProviderItem.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 ]
  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 isDirectory = false
  58. init(metadata: tableMetadata, serverUrl: String) {
  59. self.contentModificationDate = metadata.date as Date
  60. self.creationDate = metadata.date as Date
  61. self.documentSize = NSNumber(value: metadata.size)
  62. self.filename = metadata.fileNameView
  63. self.itemIdentifier = NSFileProviderItemIdentifier("\(metadata.fileID)")
  64. self.isDirectory = metadata.directory
  65. // parentItemIdentifier
  66. if #available(iOSApplicationExtension 11.0, *) {
  67. self.parentItemIdentifier = NSFileProviderItemIdentifier.rootContainer
  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. } else {
  77. // < iOS 11
  78. self.parentItemIdentifier = NSFileProviderItemIdentifier("")
  79. }
  80. // typeIdentifier
  81. if let fileType = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata) {
  82. self.typeIdentifier = fileType
  83. }
  84. self.versionIdentifier = metadata.etag.data(using: .utf8)
  85. // Verify file exists on cache
  86. if (!metadata.directory) {
  87. let identifierPathUrl = fileProviderStorageURL!.appendingPathComponent(metadata.fileID)
  88. let fileIdentifier = "\(identifierPathUrl.path)/\(metadata.fileNameView)"
  89. let fileDirectoryUser = "\(directoryUser)/\(metadata.fileID)"
  90. let changeDocumentPath = changeDocumentURL!.path + "/" + metadata.fileNameView
  91. var fileSize = 0 as Double
  92. do {
  93. try FileManager.default.createDirectory(atPath: identifierPathUrl.path, withIntermediateDirectories: true, attributes: nil)
  94. } catch let error {
  95. print("error: \(error)")
  96. }
  97. if FileManager.default.fileExists(atPath: fileIdentifier) == false {
  98. if FileManager.default.fileExists(atPath: fileDirectoryUser) {
  99. do {
  100. try FileManager.default.copyItem(atPath: fileDirectoryUser, toPath: fileIdentifier)
  101. } catch {
  102. FileManager.default.createFile(atPath: fileIdentifier, contents: nil, attributes: nil)
  103. }
  104. } else {
  105. FileManager.default.createFile(atPath: fileIdentifier, contents: nil, attributes: nil)
  106. }
  107. }
  108. do {
  109. let attributes = try FileManager.default.attributesOfItem(atPath: fileIdentifier)
  110. fileSize = attributes[FileAttributeKey.size] as! Double
  111. } catch let error {
  112. print("error: \(error)")
  113. }
  114. // Download
  115. if fileSize == 0 {
  116. self.isDownloaded = false
  117. self.isMostRecentVersionDownloaded = false
  118. } else {
  119. self.documentSize = NSNumber(value:fileSize)
  120. self.isDownloaded = true
  121. self.isMostRecentVersionDownloaded = true
  122. }
  123. // Upload
  124. let queue = NCManageDatabase.sharedInstance.getQueueUpload(predicate: NSPredicate(format: "account = %@ AND path = %@", account, changeDocumentPath))
  125. if queue?.count == 0 {
  126. self.isUploading = false
  127. self.isUploaded = true
  128. } else {
  129. self.isUploading = true
  130. self.isUploaded = false
  131. }
  132. }
  133. // Tag
  134. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "account = %@ AND fileID = %@", metadata.account, metadata.fileID)) {
  135. tagData = tableTag.tagIOS
  136. }
  137. }
  138. }