SPB Git

Repository Public

DriveItem.swift 6.7 KB 125 lines swift

1// SP Drive iOS2// File: Core/Models/DriveItem.swift3// Description: One file or folder of the Drive as this device knows it, and the device-side state attached to it.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import UniformTypeIdentifiers910public struct DriveItem: Sendable, Hashable, Identifiable {11    /// Server identifier (UUID). Items created offline carry a `local-…` identifier until the server confirms them.12    public var id: String13    public var isFolder: Bool14    /// `nil` only for the root folder.15    public var parentID: String?16    public var name: String17    public var size: Int6418    /// SHA-256 of the current content (hex); empty for folders.19    public var sha256: String20    public var versionID: String?21    public var versionSeq: Int22    public var versionCount: Int23    /// Server revision of the last change to this item (monotonic per account).24    public var rev: Int6425    public var createdAt: Date26    public var updatedAt: Date27    public var trashedAt: Date?28    /// True for the item that was put in the trash itself (its descendants follow it without being listed).29    public var trashRoot: Bool30    public var starred: Bool31    /// Antivirus state reported by the server: pending, clean, infected…32    public var scan: String33    public var hasThumbnail: Bool34    /// True while a change made on this device has not been confirmed by the server.35    public var pending: Bool3637    public init(id: String, isFolder: Bool, parentID: String?, name: String, size: Int64 = 0, sha256: String = "", versionID: String? = nil, versionSeq: Int = 1, versionCount: Int = 1,38                rev: Int64 = 0, createdAt: Date = .now, updatedAt: Date = .now, trashedAt: Date? = nil, trashRoot: Bool = false, starred: Bool = false, scan: String = "clean", hasThumbnail: Bool = false, pending: Bool = false) {39        self.id = id; self.isFolder = isFolder; self.parentID = parentID; self.name = name; self.size = size; self.sha256 = sha256; self.versionID = versionID40        self.versionSeq = versionSeq; self.versionCount = versionCount; self.rev = rev; self.createdAt = createdAt; self.updatedAt = updatedAt; self.trashedAt = trashedAt; self.trashRoot = trashRoot41        self.starred = starred; self.scan = scan; self.hasThumbnail = hasThumbnail; self.pending = pending42    }4344    public var isLocalOnly: Bool { id.hasPrefix(DriveItem.localPrefix) }45    public var isTrashed: Bool { trashedAt != nil }46    public var isInfected: Bool { scan == "infected" }47    public static let localPrefix = "local-"48    public static func newLocalID() -> String { localPrefix + UUID().uuidString.lowercased() }4950    public var fileExtension: String { isFolder ? "" : (name as NSString).pathExtension.lowercased() }51    public var contentType: UTType { isFolder ? .folder : (UTType(filenameExtension: fileExtension) ?? .data) }52    public var kind: FileKind { isFolder ? .folder : FileKind(extension: fileExtension) }53}5455/// Coarse families used for icons, filters and search.56public enum FileKind: String, Sendable, CaseIterable {57    case folder, image, pdf, document, sheet, slides, video, audio, archive, text, code, other5859    public init(extension ext: String) {60        switch ext {61        case "jpg", "jpeg", "png", "webp", "gif", "heic", "heif", "svg", "tiff", "bmp", "raw", "dng": self = .image62        case "pdf": self = .pdf63        case "doc", "docx", "odt", "rtf", "pages": self = .document64        case "xls", "xlsx", "ods", "csv", "tsv", "numbers": self = .sheet65        case "ppt", "pptx", "odp", "key": self = .slides66        case "mp4", "m4v", "mov", "webm", "mkv", "avi": self = .video67        case "mp3", "m4a", "aac", "wav", "ogg", "oga", "flac", "aiff": self = .audio68        case "zip", "tar", "gz", "tgz", "7z", "rar", "bz2", "xz": self = .archive69        case "txt", "md", "markdown", "log", "tex", "bib": self = .text70        case "json", "xml", "yaml", "yml", "toml", "html", "htm", "css", "js", "ts", "tsx", "jsx", "py", "rb", "go", "rs", "java", "kt", "swift", "c", "h", "cpp", "hpp", "cs", "php", "sh", "sql", "r", "do", "m", "jl", "lua": self = .code71        default: self = .other72        }73    }7475    public var label: String {76        switch self {77        case .folder: String(localized: "Dossier"); case .image: String(localized: "Image"); case .pdf: "PDF"; case .document: String(localized: "Document"); case .sheet: String(localized: "Feuille de calcul"); case .slides: String(localized: "Présentation")78        case .video: String(localized: "Vidéo"); case .audio: String(localized: "Audio"); case .archive: String(localized: "Archive"); case .text: String(localized: "Texte"); case .code: String(localized: "Code"); case .other: String(localized: "Fichier")79        }80    }81    public var symbol: String {82        switch self {83        case .folder: "folder.fill"; case .image: "photo.fill"; case .pdf: "doc.richtext.fill"; case .document: "doc.text.fill"; case .sheet: "tablecells.fill"; case .slides: "rectangle.on.rectangle.angled.fill"84        case .video: "film.fill"; case .audio: "waveform"; case .archive: "archivebox.fill"; case .text: "text.alignleft"; case .code: "chevron.left.forwardslash.chevron.right"; case .other: "doc.fill"85        }86    }87}8889/// What this device holds for a file: an explicit offline copy, a cached copy, local modifications waiting to be sent.90public struct LocalState: Sendable, Hashable {91    public enum Location: String, Sendable { case offline, cache }92    public var itemID: String93    /// Explicitly kept on this device by the user (directly, or through a pinned ancestor folder).94    public var pinned: Bool95    public var location: Location?96    /// File name inside the item's local directory, when a copy exists.97    public var fileName: String?98    /// SHA-256 of the content the local copy was downloaded from (the base of any local edit).99    public var baseSHA: String?100    public var baseVersionID: String?101    public var localSize: Int64102    /// The local copy was modified on this device and not yet uploaded.103    public var dirty: Bool104    public var downloadedAt: Date?105    public var openedAt: Date?106107    public var hasCopy: Bool { fileName != nil && location != nil }108}109110/// The badge shown next to an item.111public enum SyncBadge: Sendable, Equatable {112    case none            // online only, in sync113    case available       // a verified copy is on this device114    case pinned          // kept offline and up to date115    case downloading(Double)116    /// A download is queued (waiting for a slot, for Wi-Fi, for the network).117    case waitingDownload118    case uploading(Double)119    case waiting         // change queued for the next connection120    case conflict121    case error122    /// The device is offline and holds no copy of this file.123    case unavailableOffline124}125
Files main
Menu

Theme