Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Models/Models.swift3// Description: Value types of SP Drive as the server describes them: items (files and folders), account, share links, deposit portals, versions, sessions.4import Foundation56public enum ItemKind: String, Codable, Sendable, Hashable { case file, folder }78/// A file or a folder of the owner's tree, as the server last described it.9public struct DriveItem: Sendable, Identifiable, Hashable, Codable {10 public var id: String11 public var kind: ItemKind12 public var name: String13 /// Parent folder; nil for the root folder only.14 public var parentID: String?15 public var size: Int6416 public var sha256: String17 /// Current content version (files); a new one is created each time the content changes.18 public var versionID: String?19 public var versionSeq: Int20 public var versionCount: Int21 /// When the current content was stored (files); `updatedAt` also moves on renames and moves.22 public var contentAt: Date23 public var createdAt: Date24 public var updatedAt: Date25 public var starred: Bool26 public var trashedAt: Date?27 /// True for the item that was put in the trash itself (its descendants follow it).28 public var trashRoot: Bool29 /// Server revision: grows with every committed change of the owner's tree.30 public var rev: Int6431 /// Antivirus state of the content: pending, clean, infected, error.32 public var scan: String33 public var hasThumbnail: Bool34 /// How the server previews it (pdf, image, video, text, office…), decided from the name.35 public var previewKind: String3637 public var isFolder: Bool { kind == .folder }38 public var isTrashed: Bool { trashedAt != nil }39 public var isRoot: Bool { kind == .folder && parentID == nil }40 public var fileExtension: String { kind == .file ? NameRules.fileExtension(name) : "" }4142 public init(id: String, kind: ItemKind, name: String, parentID: String?, size: Int64 = 0, sha256: String = "", versionID: String? = nil, versionSeq: Int = 1,43 versionCount: Int = 1, contentAt: Date = Date(), createdAt: Date = Date(), updatedAt: Date = Date(), starred: Bool = false, trashedAt: Date? = nil,44 trashRoot: Bool = false, rev: Int64 = 0, scan: String = "clean", hasThumbnail: Bool = false, previewKind: String = "none") {45 self.id = id; self.kind = kind; self.name = name; self.parentID = parentID; self.size = size; self.sha256 = sha256; self.versionID = versionID46 self.versionSeq = versionSeq; self.versionCount = versionCount; self.contentAt = contentAt; self.createdAt = createdAt; self.updatedAt = updatedAt47 self.starred = starred; self.trashedAt = trashedAt; self.trashRoot = trashRoot; self.rev = rev; self.scan = scan; self.hasThumbnail = hasThumbnail; self.previewKind = previewKind48 }4950 private enum K: String, CodingKey {51 case id, type, name, parentId, folderId, size, sha256, versionId, versionSeq, versions, contentAt, createdAt, updatedAt, starred, trashedAt, trashRoot, rev, scan, thumb, kind52 }53 public init(from decoder: Decoder) throws {54 let c = try decoder.container(keyedBy: K.self)55 id = try c.decode(String.self, forKey: .id)56 kind = try c.decode(ItemKind.self, forKey: .type)57 name = try c.decode(String.self, forKey: .name)58 parentID = kind == .folder ? try c.decodeIfPresent(String.self, forKey: .parentId) : try c.decode(String.self, forKey: .folderId)59 size = try c.decodeIfPresent(Int64.self, forKey: .size) ?? 060 sha256 = try c.decodeIfPresent(String.self, forKey: .sha256) ?? ""61 versionID = try c.decodeIfPresent(String.self, forKey: .versionId)62 versionSeq = try c.decodeIfPresent(Int.self, forKey: .versionSeq) ?? 163 versionCount = try c.decodeIfPresent(Int.self, forKey: .versions) ?? 164 createdAt = try c.decode(Date.self, forKey: .createdAt)65 updatedAt = try c.decode(Date.self, forKey: .updatedAt)66 contentAt = try c.decodeIfPresent(Date.self, forKey: .contentAt) ?? updatedAt67 starred = try c.decodeIfPresent(Bool.self, forKey: .starred) ?? false68 trashedAt = try c.decodeIfPresent(Date.self, forKey: .trashedAt)69 trashRoot = try c.decodeIfPresent(Bool.self, forKey: .trashRoot) ?? false70 rev = try c.decodeIfPresent(Int64.self, forKey: .rev) ?? 071 scan = try c.decodeIfPresent(String.self, forKey: .scan) ?? "clean"72 hasThumbnail = try c.decodeIfPresent(Bool.self, forKey: .thumb) ?? false73 previewKind = try c.decodeIfPresent(String.self, forKey: .kind) ?? "none"74 }75 public func encode(to encoder: Encoder) throws {76 var c = encoder.container(keyedBy: K.self)77 try c.encode(id, forKey: .id); try c.encode(kind, forKey: .type); try c.encode(name, forKey: .name)78 if kind == .folder { try c.encodeIfPresent(parentID, forKey: .parentId) } else { try c.encodeIfPresent(parentID, forKey: .folderId) }79 try c.encode(size, forKey: .size); try c.encode(sha256, forKey: .sha256); try c.encodeIfPresent(versionID, forKey: .versionId)80 try c.encode(versionSeq, forKey: .versionSeq); try c.encode(versionCount, forKey: .versions); try c.encode(contentAt, forKey: .contentAt)81 try c.encode(createdAt, forKey: .createdAt); try c.encode(updatedAt, forKey: .updatedAt); try c.encode(starred, forKey: .starred)82 try c.encodeIfPresent(trashedAt, forKey: .trashedAt); try c.encode(trashRoot, forKey: .trashRoot); try c.encode(rev, forKey: .rev)83 try c.encode(scan, forKey: .scan); try c.encode(hasThumbnail, forKey: .thumb); try c.encode(previewKind, forKey: .kind)84 }85}8687public struct Crumb: Codable, Sendable, Hashable, Identifiable { public var id: String; public var name: String }8889/// A folder with its direct children, as listed by the server.90public struct FolderListing: Decodable, Sendable {91 public var folder: DriveItem92 public var breadcrumbs: [Crumb]93 public var folders: [DriveItem]94 public var files: [DriveItem]95 public var children: [DriveItem] { folders + files }96}97public struct ItemList: Decodable, Sendable {98 public var folders: [DriveItem]99 public var files: [DriveItem]100 public var all: [DriveItem] { folders + files }101}102103public struct Account: Codable, Sendable, Hashable {104 public var id: String105 public var email: String106 public var displayName: String107 public var role: String108 public var locale: String109 private enum CodingKeys: String, CodingKey { case id, email, displayName = "display_name", role, locale }110}111public struct Quota: Codable, Sendable, Hashable { public var limit: Int64; public var used: Int64; public var free: Int64 { max(0, limit - used) } }112public struct AuthState: Decodable, Sendable {113 public var authenticated: Bool114 public var recovery: Bool?115 public var kind: String?116 public var user: Account?117 public var quota: Quota?118 public var freshAuth: Bool?119}120121public struct FileVersion: Decodable, Sendable, Identifiable, Hashable {122 public var id: String123 public var seq: Int124 public var size: Int64125 public var sha256: String126 public var createdAt: Date127 public var note: String128 public var current: Bool129}130public struct FileDetails: Decodable, Sendable {131 public var file: DriveItem132 public var path: [Crumb]133 public var shares: Int134 public var versions: [FileVersion]135}136137public struct ShareLink: Decodable, Sendable, Identifiable, Hashable {138 public var id: String139 public var type: ItemKind140 public var targetId: String?141 public var targetName: String142 public var label: String143 public var canDownload: Bool144 public var hasPassword: Bool145 public var expiresAt: Date?146 public var maxViews: Int?147 public var viewCount: Int148 public var maxDownloads: Int?149 public var downloadCount: Int150 public var singleUse: Bool151 public var revokedAt: Date?152 public var createdAt: Date153 public var lastAccessAt: Date?154 /// active, revoked, expired, exhausted, target_missing155 public var status: String156 public var isActive: Bool { status == "active" }157}158public struct ShareAccessEvent: Decodable, Sendable, Identifiable, Hashable {159 public var id: String; public var at: Date; public var kind: String; public var ok: Bool; public var ip: String; public var userAgent: String; public var detail: String160}161/// What the owner chooses for a share link. `nil` leaves a setting untouched on update; `.some(nil)` clears it.162public struct ShareSettings: Encodable, Sendable {163 public var label: String?164 public var canDownload: Bool?165 public var password: String??166 public var expiresAt: Date??167 public var maxViews: Int??168 public var maxDownloads: Int??169 public var singleUse: Bool?170 public init(label: String? = nil, canDownload: Bool? = nil, password: String?? = nil, expiresAt: Date?? = nil, maxViews: Int?? = nil, maxDownloads: Int?? = nil, singleUse: Bool? = nil) {171 self.label = label; self.canDownload = canDownload; self.password = password; self.expiresAt = expiresAt; self.maxViews = maxViews; self.maxDownloads = maxDownloads; self.singleUse = singleUse172 }173 private enum K: String, CodingKey { case label, canDownload, password, expiresAt, maxViews, maxDownloads, singleUse, fileId, folderId }174 public func encode(to encoder: Encoder) throws { try encode(to: encoder, target: nil) }175 func encode(to encoder: Encoder, target: (kind: ItemKind, id: String)?) throws {176 var c = encoder.container(keyedBy: K.self)177 try c.encodeIfPresent(label, forKey: .label); try c.encodeIfPresent(canDownload, forKey: .canDownload); try c.encodeIfPresent(singleUse, forKey: .singleUse)178 if let password { try c.encode(password, forKey: .password) }179 if let expiresAt { try c.encode(expiresAt, forKey: .expiresAt) }180 if let maxViews { try c.encode(maxViews, forKey: .maxViews) }181 if let maxDownloads { try c.encode(maxDownloads, forKey: .maxDownloads) }182 if let target { try c.encode(target.id, forKey: target.kind == .file ? .fileId : .folderId) }183 }184}185186/// A deposit portal: a link through which other people send files into one folder, and see nothing else.187public struct DepositPortal: Decodable, Sendable, Identifiable, Hashable {188 public var id: String189 public var folderId: String190 public var folderName: String?191 public var title: String192 public var description: String193 public var instructions: String194 public var hasPassword: Bool195 public var expiresAt: Date?196 public var active: Bool197 public var revokedAt: Date?198 public var maxFileBytes: Int64199 public var maxTotalBytes: Int64200 public var maxFilesPerSubmission: Int201 public var maxSubmissions: Int?202 public var allowedExtensions: [String]203 public var subfolderPerSubmission: Bool204 public var notifyOwner: Bool205 public var bytesReceived: Int64206 public var submissions: Int207 public var createdAt: Date208 /// open, paused, revoked, expired, full, target_missing209 public var state: String210}211public struct PortalSettings: Encodable, Sendable {212 public var folderId: String?213 public var title: String?214 public var description: String?215 public var instructions: String?216 public var password: String??217 public var expiresAt: Date??218 public var active: Bool?219 public var maxFileBytes: Int64?220 public var maxTotalBytes: Int64?221 public var maxFilesPerSubmission: Int?222 public var allowedExtensions: [String]?223 public var subfolderPerSubmission: Bool?224 public var notifyOwner: Bool?225 public init() {}226 private enum K: String, CodingKey { case folderId, title, description, instructions, password, expiresAt, active, maxFileBytes, maxTotalBytes, maxFilesPerSubmission, allowedExtensions, subfolderPerSubmission, notifyOwner }227 public func encode(to encoder: Encoder) throws {228 var c = encoder.container(keyedBy: K.self)229 try c.encodeIfPresent(folderId, forKey: .folderId); try c.encodeIfPresent(title, forKey: .title); try c.encodeIfPresent(description, forKey: .description)230 try c.encodeIfPresent(instructions, forKey: .instructions); try c.encodeIfPresent(active, forKey: .active); try c.encodeIfPresent(maxFileBytes, forKey: .maxFileBytes)231 try c.encodeIfPresent(maxTotalBytes, forKey: .maxTotalBytes); try c.encodeIfPresent(maxFilesPerSubmission, forKey: .maxFilesPerSubmission)232 try c.encodeIfPresent(allowedExtensions, forKey: .allowedExtensions); try c.encodeIfPresent(subfolderPerSubmission, forKey: .subfolderPerSubmission); try c.encodeIfPresent(notifyOwner, forKey: .notifyOwner)233 if let password { try c.encode(password, forKey: .password) }234 if let expiresAt { try c.encode(expiresAt, forKey: .expiresAt) }235 }236}237public struct PortalSubmission: Decodable, Sendable, Identifiable, Hashable {238 public var id: String; public var ref: String; public var status: String; public var name: String; public var email: String; public var reference: String239 public var createdAt: Date; public var completedAt: Date?; public var folderId: String?; public var files: Int; public var placed: Int; public var rejected: Int; public var bytes: Int64240}241242public struct DeviceSession: Decodable, Sendable, Identifiable, Hashable {243 public var id: String; public var createdAt: Date; public var lastSeenAt: Date; public var ip: String; public var userAgent: String; public var method: String244 public var kind: String?; public var deviceName: String?; public var current: Bool245}246public struct ServerNotification: Decodable, Sendable, Identifiable, Hashable {247 public var id: String; public var kind: String; public var data: [String: JSONValue]; public var read: Bool; public var createdAt: Date248}249250/// Page of the change feed.251public struct ChangePage: Decodable, Sendable {252 public struct Deleted: Decodable, Sendable, Hashable { public var id: String; public var type: ItemKind; public var rev: Int64 }253 public var items: [DriveItem]254 public var deleted: [Deleted]255 public var cursor: Int64256 public var more: Bool257 public var head: Int64258 public var rootId: String259}260public struct SyncCursor: Decodable, Sendable { public var head: Int64; public var rootId: String }261262/// Loosely typed JSON (error details, notification payloads).263public enum JSONValue: Codable, Sendable, Hashable {264 case null, bool(Bool), number(Double), string(String), array([JSONValue]), object([String: JSONValue])265 public init(from decoder: Decoder) throws {266 let c = try decoder.singleValueContainer()267 if c.decodeNil() { self = .null }268 else if let b = try? c.decode(Bool.self) { self = .bool(b) }269 else if let n = try? c.decode(Double.self) { self = .number(n) }270 else if let s = try? c.decode(String.self) { self = .string(s) }271 else if let a = try? c.decode([JSONValue].self) { self = .array(a) }272 else { self = .object(try c.decode([String: JSONValue].self)) }273 }274 public func encode(to encoder: Encoder) throws {275 var c = encoder.singleValueContainer()276 switch self {277 case .null: try c.encodeNil()278 case .bool(let b): try c.encode(b)279 case .number(let n): try c.encode(n)280 case .string(let s): try c.encode(s)281 case .array(let a): try c.encode(a)282 case .object(let o): try c.encode(o)283 }284 }285 public var string: String? { if case .string(let s) = self { return s }; return nil }286 public var int: Int64? { if case .number(let n) = self { return Int64(n) }; return nil }287}288