Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: FileProviderExtension/FileProviderItem.swift3// Description: A file or folder of SP Drive as the system sees it: stable identifiers, versions (content = server version, metadata = server revision), capabilities, offline policy.4import FileProvider5import Foundation6import SPDriveCore7import UniformTypeIdentifiers89final class FileProviderItem: NSObject, NSFileProviderItem, NSFileProviderItemDecorating {10 let item: DriveItem11 let rootID: String12 let pinned: Bool13 /// The item has an active share link (told by the application, which lists the links).14 let hasLink: Bool15 init(_ item: DriveItem, rootID: String, pinned: Bool, shared: Bool = false) { self.item = item; self.rootID = rootID; self.pinned = pinned; hasLink = shared }16 /// The system's own notion of "shared" (Finder column and badge): true while a share link is active.17 var isShared: Bool { hasLink }18 var isSharedByCurrentUser: Bool { hasLink }1920 /// Decorations declared in the extension's Info.plist (`NSFileProviderDecorations`): what the Finder shows on the icon and in its "Shared" column.21 static let decorationPrefix = "\(AppEnvironment.appBundleID).fileprovider"22 static let favoriteDecoration = NSFileProviderItemDecorationIdentifier(rawValue: "\(decorationPrefix).favorite")23 static let sharedDecoration = NSFileProviderItemDecorationIdentifier(rawValue: "\(decorationPrefix).shared")24 static let blockedDecoration = NSFileProviderItemDecorationIdentifier(rawValue: "\(decorationPrefix).blocked")25 var decorations: [NSFileProviderItemDecorationIdentifier]? {26 guard !item.isRoot, !item.isTrashed else { return nil }27 var d: [NSFileProviderItemDecorationIdentifier] = []28 if item.scan == "infected" { d.append(FileProviderItem.blockedDecoration) } else if item.starred { d.append(FileProviderItem.favoriteDecoration) }29 if hasLink { d.append(FileProviderItem.sharedDecoration) }30 return d.isEmpty ? nil : d31 }3233 /// System identifier of a server identifier (the root folder has a reserved one).34 static func identifier(_ id: String, rootID: String) -> NSFileProviderItemIdentifier { id == rootID ? .rootContainer : NSFileProviderItemIdentifier(id) }35 /// Server identifier of a system identifier.36 static func serverID(_ id: NSFileProviderItemIdentifier, rootID: String) -> String { id == .rootContainer ? rootID : id.rawValue }3738 var itemIdentifier: NSFileProviderItemIdentifier { FileProviderItem.identifier(item.id, rootID: rootID) }39 var parentItemIdentifier: NSFileProviderItemIdentifier {40 if item.isRoot { return .rootContainer }41 if item.trashRoot { return .trashContainer } // what was put in the trash itself sits in the trash; its descendants keep their parent42 return FileProviderItem.identifier(item.parentID ?? rootID, rootID: rootID)43 }44 var filename: String { item.isRoot ? "SP Drive" : item.name }45 var contentType: UTType {46 if item.isFolder { return .folder }47 return UTType(filenameExtension: (item.name as NSString).pathExtension) ?? .data48 }49 var capabilities: NSFileProviderItemCapabilities {50 if item.isRoot { return [.allowsReading, .allowsContentEnumerating, .allowsAddingSubItems] }51 if item.isTrashed { return [.allowsReading, .allowsDeleting, .allowsReparenting, .allowsContentEnumerating] }52 var c: NSFileProviderItemCapabilities = [.allowsReading, .allowsRenaming, .allowsReparenting, .allowsTrashing, .allowsDeleting, .allowsEvicting]53 if item.isFolder { c.formUnion([.allowsContentEnumerating, .allowsAddingSubItems]) } else { c.insert(.allowsWriting) }54 return c55 }56 var documentSize: NSNumber? { item.isFolder ? nil : NSNumber(value: item.size) }57 var creationDate: Date? { item.createdAt }58 var contentModificationDate: Date? { item.isFolder ? item.updatedAt : item.contentAt }59 var itemVersion: NSFileProviderItemVersion {60 // content: the server's version identifier — what an edit made here is derived from, and what the server compares on upload61 let content = item.isFolder ? "folder" : (item.versionID ?? item.sha256)62 return NSFileProviderItemVersion(contentVersion: Data(content.utf8), metadataVersion: Data("\(item.rev)|\(pinned ? 1 : 0)|\(hasLink ? 1 : 0)".utf8))63 }64 /// "Keep available offline": the system downloads the item, keeps it up to date and never evicts it.65 var contentPolicy: NSFileProviderContentPolicy { pinned ? .downloadEagerlyAndKeepDownloaded : .inherited }66 /// Read by the activation rules of the context-menu actions (Info.plist `NSExtensionFileProviderActions`).67 var userInfo: [AnyHashable: Any]? { ["scan": item.scan, "pinned": pinned ? 1 : 0, "file": item.isFolder ? 0 : 1, "live": item.isTrashed || item.isRoot ? 0 : 1] }68}69/// Server version identifier carried by a system item version ("" when unknown).70func serverVersionID(_ v: NSFileProviderItemVersion) -> String? {71 let s = String(decoding: v.contentVersion, as: UTF8.self)72 return s.count == 36 && s.contains("-") ? s : nil73}74