@@ -333,6 +333,7 @@ public extension HubApi {
333
333
}
334
334
335
335
struct HubFileDownloader {
336
+ let hub : HubApi
336
337
let repo : Repo
337
338
let repoDestination : URL
338
339
let repoMetadataDestination : URL
@@ -380,14 +381,14 @@ public extension HubApi {
380
381
/// (See for example PipelineLoader in swift-coreml-diffusers)
381
382
@discardableResult
382
383
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)
385
386
386
387
let localCommitHash = localMetadata? . commitHash ?? " "
387
388
let remoteCommitHash = remoteMetadata. commitHash ?? " "
388
389
389
390
// 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 {
391
392
return destination
392
393
}
393
394
@@ -404,18 +405,18 @@ public extension HubApi {
404
405
if downloaded {
405
406
// etag matches => update metadata and return file
406
407
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)
408
409
return destination
409
410
}
410
411
411
412
// etag is a sha256
412
413
// => means it's an LFS file (large)
413
414
// => let's compute local hash and compare
414
415
// => 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)
417
418
if fileHash == remoteEtag {
418
- try HubApi . shared . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
419
+ try hub . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
419
420
return destination
420
421
}
421
422
}
@@ -435,7 +436,7 @@ public extension HubApi {
435
436
try downloader. waitUntilDone ( )
436
437
}
437
438
438
- try HubApi . shared . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
439
+ try hub . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
439
440
440
441
return destination
441
442
}
@@ -489,6 +490,7 @@ public extension HubApi {
489
490
for filename in filenames {
490
491
let fileProgress = Progress ( totalUnitCount: 100 , parent: progress, pendingUnitCount: 1 )
491
492
let downloader = HubFileDownloader (
493
+ hub: self ,
492
494
repo: repo,
493
495
repoDestination: repoDestination,
494
496
repoMetadataDestination: repoMetadataDestination,
0 commit comments