func download()

in Sources/XCRemoteCache/Network/CachedNetworkClient.swift [62:97]


    func download(_ url: URL, to location: URL, completion: @escaping (Result<Void, NetworkClientError>) -> Void) {
        let localURL = localURLBuilder.location(for: url)
        if fileManager.fileExists(atPath: localURL.path) {
            do {
                try fileManager.spt_forceLinkItem(at: localURL, to: location)
                completion(.success(()))
            } catch {
                errorLog("Couldn't link cached file to the expected location with error: \(error)")
                completion(.failure(.other(error)))
            }
            return
        }
        do {
            try fileManager.createDirectory(
                at: localURL.deletingLastPathComponent(),
                withIntermediateDirectories: true,
                attributes: nil
            )
        } catch {
            completion(.failure(.other(error)))
            errorLog("Couldn't create a directory for CachedNetworkClient with error: \(error)")
            return
        }

        client.download(url, to: localURL) { [fileManager] result in
            do {
                if case .success = result {
                    try fileManager.spt_forceLinkItem(at: localURL, to: location)
                }
                completion(result)
            } catch {
                errorLog("Couldn't link downloaded file to the expected location with error: \(error)")
                completion(.failure(.other(error)))
            }
        }
    }