Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sync/DownloadManager.swift3// Description: Resumable, verified downloads: bytes are streamed to a partial file (never held in memory), resumed with Range/If-Range, hashed on the way, and placed atomically only when size and SHA-256 match.4import CryptoKit5import Foundation6import SPDriveCore78/// Streams HTTP bodies to handlers, one URLSession for all downloads of an engine.9final class StreamingSession: NSObject, URLSessionDataDelegate, @unchecked Sendable {10 final class Sink: @unchecked Sendable {11 var onResponse: (HTTPURLResponse) -> Bool = { _ in true }12 var onData: (Data) throws -> Void = { _ in }13 var continuation: CheckedContinuation<Void, Error>?14 var failure: Error?15 var errorBody = Data()16 var status = 017 }18 private var session: URLSession!19 private let lock = NSLock()20 private var sinks: [Int: Sink] = [:]21 init(configuration: URLSessionConfiguration) {22 super.init()23 let q = OperationQueue(); q.maxConcurrentOperationCount = 1; q.qualityOfService = .utility24 session = URLSession(configuration: configuration, delegate: self, delegateQueue: q)25 }26 func invalidate() { session.invalidateAndCancel() }2728 /// Runs the request; `onResponse` returns false to stop. Data blocks arrive in order on a serial queue.29 func run(_ request: URLRequest, sink: Sink, onStart: (URLSessionDataTask) -> Void = { _ in }) async throws {30 let task = session.dataTask(with: request)31 lock.withLock { sinks[task.taskIdentifier] = sink }32 onStart(task)33 try await withTaskCancellationHandler {34 try await withCheckedThrowingContinuation { (c: CheckedContinuation<Void, Error>) in sink.continuation = c; task.resume() }35 } onCancel: { task.cancel() }36 }37 private func sink(_ t: URLSessionTask) -> Sink? { lock.withLock { sinks[t.taskIdentifier] } }38 func urlSession(_ s: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {39 guard let k = sink(dataTask), let http = response as? HTTPURLResponse else { completionHandler(.cancel); return }40 k.status = http.statusCode41 if (200..<300).contains(http.statusCode) { completionHandler(k.onResponse(http) ? .allow : .cancel) } else { completionHandler(.allow) } // read the error body42 }43 func urlSession(_ s: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {44 guard let k = sink(dataTask), k.failure == nil else { return }45 if !(200..<300).contains(k.status) { if k.errorBody.count < 65536 { k.errorBody.append(data) }; return }46 do { try k.onData(data) } catch { k.failure = error; dataTask.cancel() }47 }48 func urlSession(_ s: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {49 let k: Sink? = lock.withLock { sinks.removeValue(forKey: task.taskIdentifier) }50 guard let k, let c = k.continuation else { return }51 k.continuation = nil52 if let f = k.failure { c.resume(throwing: f) }53 else if let error { c.resume(throwing: DriveError.wrap(error)) }54 else if !(200..<300).contains(k.status), let r = task.response {55 do { try APIClient.check(k.errorBody, r); c.resume() } catch { c.resume(throwing: error) }56 } else { c.resume() }57 }58}5960public final class DownloadEngine: Sendable {61 public let api: APIClient62 public let throttle: Throttle?63 private let stream: StreamingSession64 public init(api: APIClient, throttle: Throttle? = nil, configuration: URLSessionConfiguration? = nil) {65 self.api = api; self.throttle = throttle66 stream = StreamingSession(configuration: configuration ?? APIClient.defaultConfiguration())67 }68 deinit { stream.invalidate() }6970 /// Downloads a file's content (or one of its versions) to `destination`.71 /// - Parameters:72 /// - partial: where the bytes accumulate; an existing partial file is resumed when the content is still the same.73 /// - sha256: expected SHA-256; when empty, the server's ETag (the SHA-256 of the content) is used.74 /// - progress: bytes on disk so far and total, called at most a few times per second.75 /// - Returns: the SHA-256 of what was stored.76 @discardableResult77 public func download(fileID: String, versionID: String? = nil, sha256 expected: String = "", size expectedSize: Int64? = nil, to destination: URL, partial: URL,78 progress: @escaping @Sendable (Int64, Int64) -> Void = { _, _ in }) async throws -> String {79 let fm = FileManager.default80 try fm.createDirectory(at: partial.deletingLastPathComponent(), withIntermediateDirectories: true)81 if !fm.fileExists(atPath: partial.path) { fm.createFile(atPath: partial.path, contents: nil, attributes: [.posixPermissions: 0o600]) }82 var have = (try fm.attributesOfItem(atPath: partial.path)[.size] as? NSNumber)?.int64Value ?? 083 // a partial file belongs to one content: bytes of another version are never continued84 if have > 0, PartialTag.read(partial) != expected.lowercased() { try Data().write(to: partial); have = 0 }85 if !expected.isEmpty { PartialTag.write(partial, expected.lowercased()) }86 // without a known hash there is nothing to tie a partial file to: start again87 if have > 0, expected.isEmpty { try Data().write(to: partial); have = 0 }88 if let expectedSize, have > expectedSize { try Data().write(to: partial); have = 0 }8990 let state = DownloadState(partial: partial, have: have, expectedSHA: expected.lowercased())91 if have > 0 { try state.rehash() }92 // a complete partial file (interrupted just before placement): verify and place without asking for a byte93 if let expectedSize, have == expectedSize, have > 0, state.currentHash() == expected.lowercased() {94 try IntegrityVerifier.placeAtomically(partial, at: destination); progress(have, have); return expected.lowercased()95 }96 let request = try api.contentRequest(fileID: fileID, versionID: versionID, from: have, etag: expected.isEmpty ? nil : expected.lowercased())97 let sink = StreamingSession.Sink()98 let throttle = self.throttle99 let taskBox = TaskBox()100 sink.onResponse = { http in101 do { try state.begin(http) } catch { sink.failure = error; return false }102 return true103 }104 sink.onData = { data in105 try state.write(data)106 progress(state.written, state.total)107 if let throttle {108 // pace the stream: hold the session's (serial, dedicated) delegate queue while the bucket refills. Nothing more109 // is delivered meanwhile — not even the completion — so the cap holds even when the whole body already sits110 // in the loader's buffers (suspending the task did not: buffered data and the completion still came through).111 let gate = DispatchSemaphore(value: 0)112 Task.detached { try? await throttle.consume(data.count); gate.signal() }113 gate.wait()114 }115 }116 do { try await stream.run(request, sink: sink) { taskBox.task = $0 } } catch {117 state.close()118 let e = DriveError.wrap(error)119 if e.code == "content_changed" { try? fm.removeItem(at: partial) } // what was on disk belongs to a version that is no longer current120 // 416: the partial file is already as long as the content — handled by the verification below121 if e.status != 416 { throw e }122 }123 state.close()124 let final = state.finish()125 let want = expected.isEmpty ? state.etag : expected.lowercased()126 let total = expectedSize ?? (state.total > 0 ? state.total : state.written)127 guard state.written == total else { throw DriveError.offline("téléchargement interrompu à \(state.written) sur \(total) octets") } // resumable: keep the partial128 guard want.isEmpty || final == want else {129 try? fm.removeItem(at: partial) // damaged bytes are never kept nor shown130 throw DriveError.integrity("le contenu reçu ne correspond pas à son empreinte SHA-256")131 }132 try IntegrityVerifier.placeAtomically(partial, at: destination)133 progress(total, total)134 return final135 }136}137final class TaskBox: @unchecked Sendable { var task: URLSessionDataTask? }138139/// The SHA-256 a partial file is a prefix of, kept in an extended attribute of the file itself.140enum PartialTag {141 static let name = "ai.spboucher.spdrive.sha256"142 static func read(_ url: URL) -> String {143 var buffer = [UInt8](repeating: 0, count: 80)144 let n = getxattr(url.path, name, &buffer, buffer.count, 0, 0)145 return n > 0 ? String(decoding: buffer[0..<n], as: UTF8.self) : ""146 }147 static func write(_ url: URL, _ sha: String) { let b = Array(sha.utf8); _ = setxattr(url.path, name, b, b.count, 0, 0) }148}149150/// File handle, running hash and counters of one download (touched only from the session's serial delegate queue, or before/after the task).151final class DownloadState: @unchecked Sendable {152 let partial: URL153 private var handle: FileHandle?154 private var hasher = SHA256()155 private(set) var written: Int64156 private(set) var total: Int64 = 0157 private(set) var etag = ""158 private let expectedSHA: String159 private var lastReport = Date.distantPast160 init(partial: URL, have: Int64, expectedSHA: String) { self.partial = partial; written = have; self.expectedSHA = expectedSHA }161 func rehash() throws { hasher = SHA256(); _ = try Hashing.sha256Hex(of: partial, into: &hasher) }162 func currentHash() -> String { var copy = hasher; return Hashing.hex(copy.finalize()) }163 func begin(_ http: HTTPURLResponse) throws {164 etag = (http.value(forHTTPHeaderField: "ETag") ?? "").trimmingCharacters(in: CharacterSet(charactersIn: "\"")).lowercased()165 if !expectedSHA.isEmpty, !etag.isEmpty, etag != expectedSHA { throw DriveError.server(status: 409, code: "content_changed", message: "le fichier a changé sur le serveur pendant le téléchargement", extra: [:]) }166 let h = try FileHandle(forWritingTo: partial)167 if http.statusCode == 206, let range = http.value(forHTTPHeaderField: "Content-Range"), let slash = range.lastIndex(of: "/"), let all = Int64(range[range.index(after: slash)...]) {168 // "bytes N-M/total": the server continues where the partial file ends169 let startText = range.dropFirst("bytes ".count).prefix { $0 != "-" }170 guard Int64(startText) == written else { try h.close(); throw DriveError.invalidResponse("reprise à un mauvais décalage") }171 total = all; try h.seekToEnd()172 } else {173 // 200: the whole content comes again (no partial, or the content changed): start from zero174 try h.truncate(atOffset: 0); written = 0; hasher = SHA256()175 total = http.expectedContentLength > 0 ? http.expectedContentLength : 0176 }177 handle = h178 }179 func write(_ data: Data) throws {180 guard let handle else { throw DriveError.local("fichier partiel fermé") }181 try handle.write(contentsOf: data)182 hasher.update(data: data); written += Int64(data.count)183 }184 func close() { try? handle?.synchronize(); try? handle?.close(); handle = nil }185 func finish() -> String { Hashing.hex(hasher.finalize()) }186}187