Skip to content

Commit cccf05a

Browse files
Sendable Hub.Downloader, Hub.Hub and Hub.HubApi .4
1 parent ff6b5c8 commit cccf05a

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Sources/Hub/HubApi.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public extension HubApi {
340340
}
341341

342342
struct HubFileDownloader {
343+
let hub: HubApi
343344
let repo: Repo
344345
let revision: String
345346
let repoDestination: URL
@@ -387,14 +388,14 @@ public extension HubApi {
387388
/// (See for example PipelineLoader in swift-coreml-diffusers)
388389
@discardableResult
389390
func download(progressHandler: @escaping (Double) -> Void) async throws -> URL {
390-
let localMetadata = try HubApi.shared.readDownloadMetadata(metadataPath: metadataDestination)
391-
let remoteMetadata = try await HubApi.shared.getFileMetadata(url: source)
391+
let localMetadata = try hub.readDownloadMetadata(metadataPath: metadataDestination)
392+
let remoteMetadata = try await hub.getFileMetadata(url: source)
392393

393394
let localCommitHash = localMetadata?.commitHash ?? ""
394395
let remoteCommitHash = remoteMetadata.commitHash ?? ""
395396

396397
// Local file exists + metadata exists + commit_hash matches => return file
397-
if HubApi.shared.isValidHash(hash: remoteCommitHash, pattern: HubApi.shared.commitHashPattern), downloaded, localMetadata != nil,
398+
if hub.isValidHash(hash: remoteCommitHash, pattern: hub.commitHashPattern), downloaded, localMetadata != nil,
398399
localCommitHash == remoteCommitHash
399400
{
400401
return destination
@@ -413,18 +414,18 @@ public extension HubApi {
413414
if downloaded {
414415
// etag matches => update metadata and return file
415416
if localMetadata?.etag == remoteEtag {
416-
try HubApi.shared.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
417+
try hub.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
417418
return destination
418419
}
419420

420421
// etag is a sha256
421422
// => means it's an LFS file (large)
422423
// => let's compute local hash and compare
423424
// => if match, update metadata and return file
424-
if HubApi.shared.isValidHash(hash: remoteEtag, pattern: HubApi.shared.sha256Pattern) {
425-
let fileHash = try HubApi.shared.computeFileHash(file: destination)
425+
if hub.isValidHash(hash: remoteEtag, pattern: hub.sha256Pattern) {
426+
let fileHash = try hub.computeFileHash(file: destination)
426427
if fileHash == remoteEtag {
427-
try HubApi.shared.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
428+
try hub.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
428429
return destination
429430
}
430431
}
@@ -456,7 +457,7 @@ public extension HubApi {
456457
}
457458
}
458459

459-
try HubApi.shared.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
460+
try hub.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
460461

461462
return destination
462463
}
@@ -515,6 +516,7 @@ public extension HubApi {
515516
for filename in filenames {
516517
let fileProgress = Progress(totalUnitCount: 100, parent: progress, pendingUnitCount: 1)
517518
let downloader = HubFileDownloader(
519+
hub: self,
518520
repo: repo,
519521
revision: revision,
520522
repoDestination: repoDestination,
@@ -541,18 +543,18 @@ public extension HubApi {
541543
}
542544

543545
@discardableResult
544-
func snapshot(from repoId: String, matching globs: [String] = [], progressHandler: @escaping (Progress) -> Void = { _ in }) async throws -> URL {
545-
try await snapshot(from: Repo(id: repoId), matching: globs, progressHandler: progressHandler)
546+
func snapshot(from repoId: String, revision: String = "main", matching globs: [String] = [], progressHandler: @escaping (Progress) -> Void = { _ in }) async throws -> URL {
547+
try await snapshot(from: Repo(id: repoId), revision: revision, matching: globs, progressHandler: progressHandler)
546548
}
547549

548550
@discardableResult
549-
func snapshot(from repo: Repo, matching glob: String, progressHandler: @escaping (Progress) -> Void = { _ in }) async throws -> URL {
550-
try await snapshot(from: repo, matching: [glob], progressHandler: progressHandler)
551+
func snapshot(from repo: Repo, revision: String = "main", matching glob: String, progressHandler: @escaping (Progress) -> Void = { _ in }) async throws -> URL {
552+
try await snapshot(from: repo, revision: revision, matching: [glob], progressHandler: progressHandler)
551553
}
552554

553555
@discardableResult
554-
func snapshot(from repoId: String, matching glob: String, progressHandler: @escaping (Progress) -> Void = { _ in }) async throws -> URL {
555-
try await snapshot(from: Repo(id: repoId), matching: [glob], progressHandler: progressHandler)
556+
func snapshot(from repoId: String, revision: String = "main", matching glob: String, progressHandler: @escaping (Progress) -> Void = { _ in }) async throws -> URL {
557+
try await snapshot(from: Repo(id: repoId), revision: revision, matching: [glob], progressHandler: progressHandler)
556558
}
557559
}
558560

0 commit comments

Comments
 (0)