FileProviderItem.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // FileProviderItem.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 26/03/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. if isUpload {
  34. return [ ]
  35. } else {
  36. return [ .allowsWriting, .allowsReading, .allowsDeleting, .allowsRenaming, .allowsReparenting ]
  37. }
  38. }
  39. }
  40. // Managing Content
  41. var childItemCount: NSNumber? // The number of items contained by this item
  42. var documentSize: NSNumber? // The document's size, in bytes
  43. // Specifying Content Location
  44. var parentItemIdentifier: NSFileProviderItemIdentifier // The persistent identifier of the item's parent folder
  45. var isTrashed: Bool = false // A Boolean value that indicates whether an item is in the trash
  46. // Tracking Usage
  47. var contentModificationDate: Date? // The date the item was last modified
  48. var creationDate: Date? // The date the item was created
  49. var lastUsedDate: Date? = Date() // The date the item was last used, default to the moment when the item is created
  50. // Tracking Versions
  51. var versionIdentifier: Data? // A data value used to determine when the item changes
  52. var isMostRecentVersionDownloaded: Bool = false // A Boolean value that indicates whether the item is the most recent version downloaded from the server
  53. // Monitoring File Transfers
  54. var isUploading: Bool = false // A Boolean value that indicates whether the item is currently uploading to your remote server
  55. var isUploaded: Bool = true // A Boolean value that indicates whether the item has been uploaded to your remote server
  56. var uploadingError: Error? // An error that occurred while uploading to your remote server
  57. var isDownloading: Bool = false // A Boolean value that indicates whether the item is currently downloading from your remote server
  58. var isDownloaded: Bool = true // A Boolean value that indicates whether the item has been downloaded from your remote server
  59. var downloadingError: Error? // An error that occurred while downloading the item
  60. var tagData: Data? // Tag
  61. var favoriteRank: NSNumber? // Favorite
  62. var isDirectory = false
  63. var isDownload = false
  64. var isUpload = false
  65. init(metadata: tableMetadata, parentItemIdentifier: NSFileProviderItemIdentifier, providerData: FileProviderData) {
  66. self.parentItemIdentifier = parentItemIdentifier
  67. self.itemIdentifier = providerData.getItemIdentifier(metadata: metadata)
  68. self.contentModificationDate = metadata.date as Date
  69. self.creationDate = metadata.date as Date
  70. self.documentSize = NSNumber(value: metadata.size)
  71. self.filename = metadata.fileNameView
  72. self.isDirectory = metadata.directory
  73. self.typeIdentifier = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata)
  74. self.versionIdentifier = metadata.etag.data(using: .utf8)
  75. // This is a file
  76. if (!metadata.directory) {
  77. let fileIdentifier = CCUtility.getDirectoryProviderStorageFileID(self.itemIdentifier.rawValue, fileNameView: metadata.fileNameView)!
  78. var fileSize = 0 as Double
  79. do {
  80. let attributes = try FileManager.default.attributesOfItem(atPath: fileIdentifier)
  81. fileSize = attributes[FileAttributeKey.size] as! Double
  82. } catch let error {
  83. print("error: \(error)")
  84. }
  85. // Download
  86. if fileSize == 0 {
  87. self.isDownloaded = false
  88. self.isMostRecentVersionDownloaded = false
  89. } else {
  90. self.documentSize = NSNumber(value:fileSize)
  91. self.isDownloaded = true
  92. self.isMostRecentVersionDownloaded = true
  93. }
  94. // Upload
  95. if (metadata.session == k_upload_session_extension && metadata.status != k_metadataStatusUploadError) {
  96. isUpload = true
  97. self.isDownloaded = true
  98. self.isMostRecentVersionDownloaded = true
  99. self.isUploading = true
  100. self.isUploaded = false
  101. }
  102. // Error ?
  103. if metadata.sessionError != "" {
  104. uploadingError = NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:])
  105. }
  106. } else {
  107. // Favorite directory
  108. let rank = providerData.listFavoriteIdentifierRank[metadata.fileID]
  109. if (rank == nil) {
  110. favoriteRank = nil
  111. } else {
  112. favoriteRank = providerData.listFavoriteIdentifierRank[metadata.fileID]
  113. }
  114. }
  115. // Tag
  116. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "fileID == %@", metadata.fileID)) {
  117. tagData = tableTag.tagIOS
  118. }
  119. }
  120. }