Repository Public
1// SP Drive iOS2// File: Core/API/DriveRemote.swift3// Description: The server operations the sync engine, the transfer queue and the File Provider extension depend on.4// The production implementation speaks to MacLustr through SPDriveCore's API client.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public enum NameConflictPolicy: String, Sendable { case fail, rename, version }1112public struct ChangePage: Sendable {13 public var items: [DriveItem]14 /// Identifiers purged for good on the server.15 public var deleted: [String]16 public var cursor: Int6417 public var more: Bool18 public init(items: [DriveItem], deleted: [String], cursor: Int64, more: Bool) { self.items = items; self.deleted = deleted; self.cursor = cursor; self.more = more }19}2021public struct RemoteUpload: Sendable, Equatable {22 public var id: String23 public var size: Int6424 public var received: Int6425 public var status: String26 public var fileID: String?27 public init(id: String, size: Int64, received: Int64, status: String, fileID: String?) { self.id = id; self.size = size; self.received = received; self.status = status; self.fileID = fileID }28}2930public enum RemoteError: Error, Sendable, Equatable {31 /// No session, or the server no longer accepts it.32 case unauthenticated33 case notFound34 case nameConflict(name: String)35 /// The file changed on the server since the version this device started from.36 case versionConflict37 case quotaExceeded38 case fileTooLarge39 case offsetMismatch(received: Int64)40 case checksumMismatch41 case uploadClosed42 case infected43 case rateLimited(retryAfter: Int)44 /// The network is unavailable or the server unreachable: retry later, nothing was decided.45 case offline46 case server(status: Int, code: String, message: String)4748 /// Retrying the same request later can succeed.49 public var isTransient: Bool {50 switch self {51 case .offline, .rateLimited: true52 case .server(let status, _, _): status >= 50053 default: false54 }55 }56}5758public protocol DriveRemote: Sendable {59 // Change feed60 func rootID() async throws -> String61 func changes(since: Int64, limit: Int) async throws -> ChangePage62 func item(id: String, isFolder: Bool) async throws -> DriveItem6364 // Tree65 func createFolder(parentID: String, name: String, onConflict: NameConflictPolicy) async throws -> DriveItem66 func rename(id: String, isFolder: Bool, name: String) async throws -> DriveItem67 func setStarred(id: String, isFolder: Bool, starred: Bool) async throws -> DriveItem68 func move(fileIDs: [String], folderIDs: [String], to destinationID: String, onConflict: NameConflictPolicy) async throws69 func copy(fileIDs: [String], folderIDs: [String], to destinationID: String) async throws70 func trash(fileIDs: [String], folderIDs: [String]) async throws71 func restore(fileIDs: [String], folderIDs: [String]) async throws72 func purge(fileIDs: [String], folderIDs: [String]) async throws73 func emptyTrash() async throws7475 // Resumable uploads (block level, so blocks can travel through a background URLSession)76 /// `fileID` + `baseVersionID`: new version of a known file, refused with `.versionConflict` when the server's current version differs.77 func createUpload(parentID: String, name: String, size: Int64, onConflict: NameConflictPolicy, fileID: String?, baseVersionID: String?) async throws -> RemoteUpload78 func uploadStatus(id: String) async throws -> RemoteUpload79 func chunkRequest(uploadID: String, offset: Int64, sha256: String) async throws -> URLRequest80 func sendChunk(uploadID: String, offset: Int64, sha256: String, fileURL: URL) async throws -> RemoteUpload81 func completeUpload(id: String, sha256: String) async throws -> DriveItem82 func abortUpload(id: String) async throws8384 // Downloads85 /// Request for the bytes of a file from `offset` on (Range + If-Range on the content hash).86 func contentRequest(fileID: String, versionID: String?, offset: Int64, etag: String?) async throws -> URLRequest87 func thumbnailRequest(fileID: String) async throws -> URLRequest88 /// Interprets the status and body of a response obtained outside this object (background session).89 func error(status: Int, body: Data) -> RemoteError90 /// Decodes the JSON of an upload block answer obtained outside this object.91 func decodeUpload(_ body: Data) throws -> RemoteUpload92}93