Skip to content

Commit ebee621

Browse files
Use injected HubApi for downloads instead of shared (#196)
1 parent 41f26a3 commit ebee621

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Sources/Hub/HubApi.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ public extension HubApi {
333333
}
334334

335335
struct HubFileDownloader {
336+
let hub: HubApi
336337
let repo: Repo
337338
let repoDestination: URL
338339
let repoMetadataDestination: URL
@@ -380,14 +381,14 @@ public extension HubApi {
380381
/// (See for example PipelineLoader in swift-coreml-diffusers)
381382
@discardableResult
382383
func download(progressHandler: @escaping (Double) -> Void) async throws -> URL {
383-
let localMetadata = try HubApi.shared.readDownloadMetadata(metadataPath: metadataDestination)
384-
let remoteMetadata = try await HubApi.shared.getFileMetadata(url: source)
384+
let localMetadata = try hub.readDownloadMetadata(metadataPath: metadataDestination)
385+
let remoteMetadata = try await hub.getFileMetadata(url: source)
385386

386387
let localCommitHash = localMetadata?.commitHash ?? ""
387388
let remoteCommitHash = remoteMetadata.commitHash ?? ""
388389

389390
// Local file exists + metadata exists + commit_hash matches => return file
390-
if HubApi.shared.isValidHash(hash: remoteCommitHash, pattern: HubApi.shared.commitHashPattern), downloaded, localMetadata != nil, localCommitHash == remoteCommitHash {
391+
if hub.isValidHash(hash: remoteCommitHash, pattern: hub.commitHashPattern), downloaded, localMetadata != nil, localCommitHash == remoteCommitHash {
391392
return destination
392393
}
393394

@@ -404,18 +405,18 @@ public extension HubApi {
404405
if downloaded {
405406
// etag matches => update metadata and return file
406407
if localMetadata?.etag == remoteEtag {
407-
try HubApi.shared.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
408+
try hub.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
408409
return destination
409410
}
410411

411412
// etag is a sha256
412413
// => means it's an LFS file (large)
413414
// => let's compute local hash and compare
414415
// => if match, update metadata and return file
415-
if HubApi.shared.isValidHash(hash: remoteEtag, pattern: HubApi.shared.sha256Pattern) {
416-
let fileHash = try HubApi.shared.computeFileHash(file: destination)
416+
if hub.isValidHash(hash: remoteEtag, pattern: hub.sha256Pattern) {
417+
let fileHash = try hub.computeFileHash(file: destination)
417418
if fileHash == remoteEtag {
418-
try HubApi.shared.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
419+
try hub.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
419420
return destination
420421
}
421422
}
@@ -435,7 +436,7 @@ public extension HubApi {
435436
try downloader.waitUntilDone()
436437
}
437438

438-
try HubApi.shared.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
439+
try hub.writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
439440

440441
return destination
441442
}
@@ -489,6 +490,7 @@ public extension HubApi {
489490
for filename in filenames {
490491
let fileProgress = Progress(totalUnitCount: 100, parent: progress, pendingUnitCount: 1)
491492
let downloader = HubFileDownloader(
493+
hub: self,
492494
repo: repo,
493495
repoDestination: repoDestination,
494496
repoMetadataDestination: repoMetadataDestination,

0 commit comments

Comments
 (0)