SPB Git

Repository Public

DownloadManager.swift 18.7 KB 335 lines swift

1// SP Drive iOS2// File: Sync/DownloadManager.swift3// Description: Downloads with resumption (HTTP Range guarded by the content hash) and verification: a file only becomes the local copy4//              of an item once its SHA-256 matches the one the server announced.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910extension SyncEngine {11    public enum DownloadPriority: Int, Sendable { case background = 0, pin = 10, user = 100 }1213    /// Queues the download of a file's current content unless a verified copy of that content is already here. Returns the transfer, if one is needed.14    @discardableResult15    func enqueueDownload(_ item: DriveItem, priority: DownloadPriority) -> TransferRecord? {16        guard !item.isFolder, !item.isLocalOnly, !item.isTrashed else { return nil }17        if let ls = store.localState(item.id), ls.hasCopy, ls.dirty || ls.baseSHA == item.sha256 { return nil }18        if var existing = store.activeTransfer(itemID: item.id, kind: .download) {19            if existing.sha256 != item.sha256 {20                // the file changed again: what was fetched so far belongs to an older version21                files.removeTransferFiles(existing.id)22                existing.sha256 = item.sha256; existing.size = item.size; existing.done = 023            }24            if existing.priority < priority.rawValue { existing.priority = priority.rawValue }25            if existing.state == .failed || (existing.state == .paused && priority == .user) { existing.state = .queued; existing.attempts = 0; existing.errorCode = nil; notBefore[existing.id] = nil }26            try? store.saveTransfer(existing)27            return existing28        }29        let t = TransferRecord(id: UUID().uuidString.lowercased(), kind: .download, itemID: item.id, opID: nil, name: item.name, size: item.size, done: 0, state: .queued, priority: priority.rawValue,30                               uploadID: nil, localPath: nil, sha256: item.sha256, errorCode: nil, attempts: 0, createdAt: .now, updatedAt: .now)31        try? store.saveTransfer(t)32        emit(.transfersChanged)33        return t34    }3536    /// URL of a verified local copy of the file, downloading it first if needed. Offline, only files already on the device can be opened.37    public func localCopy(of id: String) async throws -> URL {38        guard let item = store.item(id), !item.isFolder else { throw EngineError.notFound }39        if var ls = store.localState(id), let url = files.url(for: ls), FileManager.default.fileExists(atPath: url.path), ls.dirty || ls.baseSHA == item.sha256 || item.isLocalOnly {40            ls.openedAt = .now41            try? store.saveLocalState(ls)42            return url43        }44        guard let t = enqueueDownload(item, priority: .user) else { throw EngineError.notAvailableOffline }45        await pump()46        // offline, the transfer stays queued for later; the caller is told now that the file is not on this device47        if case .offline = status { throw EngineError.notAvailableOffline }48        do { try await wait(for: t.id) } catch EngineError.transferFailed(let code) where code == "network" { throw EngineError.notAvailableOffline }49        guard var ls = store.localState(id), let url = files.url(for: ls) else { throw EngineError.notFound }50        ls.openedAt = .now51        try? store.saveLocalState(ls)52        return url53    }5455    func startDownload(_ t: TransferRecord) async {56        var t = t57        guard let item = store.item(t.itemID), !item.isFolder, !item.isTrashed else { return await fail(t, "not_found") }58        if let ls = store.localState(item.id), ls.dirty { return await succeedWithoutInstall(t) } // never overwrite local edits59        let partial = files.partial(t.id)60        if t.sha256 != item.sha256 {61            files.removeTransferFiles(t.id)62            t.sha256 = item.sha256; t.size = item.size; t.done = 063        }64        var resume = try? Data(contentsOf: files.resumeData(t.id))65        var offset = IntegrityVerifier.size(of: partial)66        if offset > item.size { try? FileManager.default.removeItem(at: partial); offset = 0 }67        if item.size == 0 {68            FileManager.default.createFile(atPath: partial.path, contents: nil)69            return await verifyAndInstall(t, item)70        }71        if offset == item.size { return await verifyAndInstall(t, item) }72        if resume != nil, offset == 0 { /* the system holds the partial bytes */ } else { resume = nil; try? FileManager.default.removeItem(at: files.resumeData(t.id)) }73        t.done = offset74        guard saveIfRunning(t) else { return }75        progressBase[t.id] = offset76        do {77            let plain = try await remote.contentRequest(fileID: item.id, versionID: nil, offset: offset, etag: item.sha256)78            guard store.transfer(t.id)?.state == .running else { inFlight.remove(t.id); return }79            var request = shape(plain, for: t)80            var expected = item.size - offset81            let segment = policy().segment(max: .max)82            if segment < expected {83                // bandwidth cap: one paced range at a time (the partial file grows segment by segment, verified as a whole at the end)84                request.setValue("bytes=\(offset)-\(offset + segment - 1)", forHTTPHeaderField: "Range")85                expected = segment86                resume = nil87            }88            noteStepStarted(t.id, bytes: expected)89            driver.download(request, tag: t.id, resumeData: resume, expectedBytes: expected)90        } catch {91            let e = classify(error)92            if e.isTransient { await retryLater(t, countAttempt: e != .offline) } else { await fail(t, code(for: e)) }93        }94    }9596    func succeedWithoutInstall(_ t: TransferRecord) async {97        files.removeTransferFiles(t.id)98        await succeed(t)99    }100101    func downloadFinished(_ t: TransferRecord, _ r: BackgroundResult) async {102        let fm = FileManager.default103        if let e = r.error {104            noteStepFailed(t.id, error: e)105            if let data = r.resumeData { try? data.write(to: files.resumeData(t.id)) }106            return await retryLater(t, countAttempt: false)107        }108        try? fm.removeItem(at: files.resumeData(t.id))109        guard let status = r.status else { return await retryLater(t) }110        let step = stepStarted[t.id]111        if (200..<300).contains(status) { noteStepSucceeded(t.id) } else if status >= 500 || status == 429 { noteStepFailed(t.id, error: nil) }112        guard (200..<300).contains(status), let file = r.file else {113            if let f = r.file { try? fm.removeItem(at: f) }114            let e = remote.error(status: status, body: r.body)115            if status == 416 { try? fm.removeItem(at: files.partial(t.id)); return await retryLater(t) }116            return e.isTransient ? await retryLater(t) : await fail(t, code(for: e))117        }118        let partial = files.partial(t.id)119        do {120            if status == 206, fm.fileExists(atPath: partial.path), IntegrityVerifier.size(of: partial) > 0 {121                let out = try FileHandle(forWritingTo: partial)122                defer { try? out.close() }123                try out.seekToEnd()124                let input = try FileHandle(forReadingFrom: file)125                defer { try? input.close() }126                while let chunk = try input.read(upToCount: 1 << 20), !chunk.isEmpty { try out.write(contentsOf: chunk) }127                try? fm.removeItem(at: file)128            } else {129                try? fm.removeItem(at: partial)130                try fm.moveItem(at: file, to: partial)131            }132        } catch {133            try? fm.removeItem(at: file)134            return await fail(t, "disk")135        }136        guard let item = store.item(t.itemID), !item.isTrashed else { files.removeTransferFiles(t.id); return await fail(t, "not_found") }137        if IntegrityVerifier.size(of: partial) < t.size {138            // a paced segment under the bandwidth cap: wait what the cap asks, then fetch the next range139            if policy().bandwidthLimit > 0 {140                var next = t141                next.done = IntegrityVerifier.size(of: partial)142                guard saveIfRunning(next) else { return }143                if await paceIfCapped(next, bytes: step?.bytes ?? 0, started: step?.at) { return }144                return await startDownload(next)145            }146            return await retryLater(t, countAttempt: false) // connection cut cleanly mid-body: continue from here147        }148        await verifyAndInstall(t, item)149    }150151    func verifyAndInstall(_ t: TransferRecord, _ item: DriveItem) async {152        let partial = files.partial(t.id)153        let expected = t.sha256 ?? item.sha256154        let ok = await Task.detached(priority: .utility) { IntegrityVerifier.verify(partial, sha256: expected) }.value155        guard ok else {156            // corrupted in transit, or the file changed on the server while it was travelling: start over on the current version157            try? FileManager.default.removeItem(at: partial)158            Log.transfer.notice("download failed verification, restarting")159            var t = t160            t.done = 0161            if let fresh = store.item(t.itemID), fresh.sha256 != expected { t.sha256 = fresh.sha256; t.size = fresh.size; return await retryLater(t, countAttempt: false) }162            return t.attempts >= 2 ? await fail(t, "checksum") : await retryLater(t)163        }164        var ls = store.localState(item.id) ?? LocalState(itemID: item.id, pinned: false, location: nil, fileName: nil, baseSHA: nil, baseVersionID: nil, localSize: 0, dirty: false, downloadedAt: nil, openedAt: nil)165        if ls.dirty { return await succeedWithoutInstall(t) }166        let location: LocalState.Location = isEffectivelyPinned(item.id) ? .offline : .cache167        do {168            if let old = ls.location, old != location { files.removeCopy(itemID: item.id, location: old) }169            ls.fileName = try files.install(partial, itemID: item.id, name: item.name, location: location, move: true)170            ls.location = location171            ls.baseSHA = expected172            // the version this copy corresponds to: only trusted when the mirror still describes the same content173            ls.baseVersionID = store.item(item.id).flatMap { $0.sha256 == expected ? $0.versionID : nil }174            ls.localSize = t.size175            ls.downloadedAt = .now176            try store.saveLocalState(ls)177        } catch { return await fail(t, "disk") }178        await succeed(t)179    }180181    // MARK: Offline availability182183    /// Keeps a file, or a whole folder (present and future content), on this device.184    public func pin(_ id: String) async throws {185        guard let item = store.item(id) else { throw EngineError.notFound }186        var ls = store.localState(id) ?? LocalState(itemID: id, pinned: false, location: nil, fileName: nil, baseSHA: nil, baseVersionID: nil, localSize: 0, dirty: false, downloadedAt: nil, openedAt: nil)187        ls.pinned = true188        try store.saveLocalState(ls)189        let targets = item.isFolder ? store.descendants(of: id, filesOnly: true) : [item]190        for f in targets {191            promoteToOffline(f.id)192            enqueueDownload(f, priority: .pin)193        }194        emit(.itemsChanged)195        await pump()196    }197198    public struct PinStatus: Sendable, Equatable {199        public var files = 0, availableFiles = 0200        public var bytes: Int64 = 0, availableBytes: Int64 = 0201        public var isComplete: Bool { files == availableFiles }202    }203204    /// A cached copy of a file that is now pinned moves to persistent storage.205    func promoteToOffline(_ id: String) {206        guard var ls = store.localState(id), ls.location == .cache, let url = files.url(for: ls), let name = ls.fileName else { return }207        guard (try? files.install(url, itemID: id, name: name, location: .offline, move: true)) != nil else { return }208        files.removeCopy(itemID: id, location: .cache)209        ls.location = .offline210        try? store.saveLocalState(ls)211    }212213    /// Stops keeping an item offline and frees its local copies. The Drive itself is untouched; copies holding edits not yet sent are kept.214    public func unpin(_ id: String) async {215        guard let item = store.item(id) else { return }216        if var ls = store.localState(id) { ls.pinned = false; try? store.saveLocalState(ls) }217        let targets = item.isFolder ? store.descendants(of: id, filesOnly: true) : [item]218        for f in targets where !isEffectivelyPinned(f.id) { await removeLocalCopy(f.id) }219        enforcePinConsistency() // a folder unpinned inside a pinned folder stays covered: what it lost comes back220        emit(.itemsChanged)221        await pump()222    }223224    /// « Remove from this phone only »: deletes the local copy, never the file on the Drive. Unsent edits are never discarded this way.225    @discardableResult226    public func removeLocalCopy(_ id: String) async -> Bool {227        guard var ls = store.localState(id) else { return false }228        if ls.dirty || id.hasPrefix(DriveItem.localPrefix) { return false }229        if let t = store.activeTransfer(itemID: id, kind: .download) { await cancel(t.id) }230        if let loc = ls.location { files.removeCopy(itemID: id, location: loc) }231        ls.location = nil; ls.fileName = nil; ls.baseSHA = nil; ls.baseVersionID = nil; ls.localSize = 0; ls.downloadedAt = nil232        if ls.pinned { try? store.saveLocalState(ls) } else { try? store.deleteLocalState(id) }233        emit(.itemsChanged)234        return true235    }236237    /// Frees the purgeable cache (copies opened once). Pinned files and unsent edits are not touched. Returns the bytes freed.238    @discardableResult239    public func clearCache() async -> Int64 {240        var freed: Int64 = 0241        for ls in store.allLocalStates() where ls.location == .cache && !ls.dirty {242            freed += ls.localSize243            await removeLocalCopy(ls.itemID)244        }245        try? FileManager.default.removeItem(at: files.thumbnails)246        try? FileManager.default.createDirectory(at: files.thumbnails, withIntermediateDirectories: true)247        return freed248    }249250    /// Checks every local copy against the device's records: copies the system reclaimed are forgotten, pinned files missing are fetched again.251    public func reconcileLocalCopies() async {252        var changed = false253        for var ls in store.allLocalStates() where ls.hasCopy {254            guard let url = files.url(for: ls), !FileManager.default.fileExists(atPath: url.path) else { continue }255            ls.location = nil; ls.fileName = nil; ls.baseSHA = nil; ls.baseVersionID = nil; ls.localSize = 0; ls.dirty = false256            if ls.pinned { try? store.saveLocalState(ls) } else { try? store.deleteLocalState(ls.itemID) }257            changed = true258        }259        let pinned = store.pinnedIDs()260        for id in pinned {261            guard let item = store.item(id), !item.isTrashed else { continue }262            for f in item.isFolder ? store.descendants(of: id, filesOnly: true) : [item] { enqueueDownload(f, priority: .pin) }263        }264        if changed { emit(.itemsChanged) }265        await pump()266    }267268    /// Selective offline, kept consistent: every live file under a pinned folder has (or is fetching) a persistent copy, and a copy that is269    /// no longer covered by any pin (the file was moved out, the pin's folder was trashed) goes back to being an ordinary cached copy.270    /// Copies holding unsent edits are never touched. Returns true when something changed.271    @discardableResult272    func enforcePinConsistency() -> Bool {273        var changed = false274        var covered = Set<String>()275        for id in store.pinnedIDs() {276            guard let item = store.item(id) else { try? store.deleteLocalState(id); changed = true; continue } // the pinned item is gone for good277            guard !item.isTrashed else { continue }278            for f in item.isFolder ? store.descendants(of: id, filesOnly: true) : [item] where !f.isLocalOnly {279                covered.insert(f.id)280                if store.localState(f.id)?.location == .cache { promoteToOffline(f.id); changed = true }281                if enqueueDownload(f, priority: .pin) != nil { changed = true }282            }283        }284        for var ls in store.allLocalStates() where ls.location == .offline && !ls.dirty && !ls.pinned && !covered.contains(ls.itemID) && !ls.itemID.hasPrefix(DriveItem.localPrefix) {285            // content still on its way to the Drive stays in persistent storage286            guard !store.ops().contains(where: { $0.itemID == ls.itemID && ($0.kind == .uploadNew || $0.kind == .uploadVersion) }),287                  let url = files.url(for: ls), let name = ls.fileName else { continue }288            if (try? files.install(url, itemID: ls.itemID, name: name, location: .cache, move: true)) != nil {289                files.removeCopy(itemID: ls.itemID, location: .offline)290                ls.location = .cache291                try? store.saveLocalState(ls)292                changed = true293            }294        }295        return changed296    }297298    /// What a pin covers and how much of it is on this device, for the interface (« 12 fichiers sur 14 »).299    public nonisolated func pinStatus(of id: String) -> PinStatus {300        guard let item = store.item(id) else { return PinStatus() }301        var out = PinStatus()302        for f in item.isFolder ? store.descendants(of: id, filesOnly: true) : [item] {303            out.files += 1; out.bytes += f.size304            if let ls = store.localState(f.id), ls.hasCopy, ls.dirty || ls.baseSHA == f.sha256 { out.availableFiles += 1; out.availableBytes += f.size }305        }306        return out307    }308309    /// Badge of an item, from the device's records. `online: false` adds « unavailable offline » for files that have no copy here.310    public nonisolated func badge(for item: DriveItem, online: Bool) -> SyncBadge {311        let b = badge(for: item)312        if !online, !item.isFolder, b == .none || b == .waitingDownload { return .unavailableOffline }313        return b314    }315316    /// Badge of an item, from the device's records.317    public nonisolated func badge(for item: DriveItem) -> SyncBadge {318        if store.conflict(itemID: item.id) != nil { return .conflict }319        let ls = store.localState(item.id)320        if item.isFolder { return ls?.pinned == true ? .pinned : (item.pending ? .waiting : .none) }321        if let t = store.activeTransfer(itemID: item.id, kind: .upload) {322            if t.state == .failed { return .error }323            return t.state == .running ? .uploading(t.fraction) : .waiting324        }325        if ls?.dirty == true || item.pending { return .waiting }326        if let t = store.activeTransfer(itemID: item.id, kind: .download) {327            if t.state == .failed { return .error }328            if t.state == .running { return .downloading(t.fraction) }329            if t.state == .queued { return .waitingDownload }330        }331        guard let ls, ls.hasCopy, ls.baseSHA == item.sha256 else { return .none }332        return ls.location == .offline ? .pinned : .available333    }334}335
Files main
Menu

Theme