@@ -340,6 +340,7 @@ public extension HubApi {
340
340
}
341
341
342
342
struct HubFileDownloader {
343
+ let hub : HubApi
343
344
let repo : Repo
344
345
let revision : String
345
346
let repoDestination : URL
@@ -387,14 +388,14 @@ public extension HubApi {
387
388
/// (See for example PipelineLoader in swift-coreml-diffusers)
388
389
@discardableResult
389
390
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)
392
393
393
394
let localCommitHash = localMetadata? . commitHash ?? " "
394
395
let remoteCommitHash = remoteMetadata. commitHash ?? " "
395
396
396
397
// 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 ,
398
399
localCommitHash == remoteCommitHash
399
400
{
400
401
return destination
@@ -413,18 +414,18 @@ public extension HubApi {
413
414
if downloaded {
414
415
// etag matches => update metadata and return file
415
416
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)
417
418
return destination
418
419
}
419
420
420
421
// etag is a sha256
421
422
// => means it's an LFS file (large)
422
423
// => let's compute local hash and compare
423
424
// => 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)
426
427
if fileHash == remoteEtag {
427
- try HubApi . shared . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
428
+ try hub . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
428
429
return destination
429
430
}
430
431
}
@@ -456,7 +457,7 @@ public extension HubApi {
456
457
}
457
458
}
458
459
459
- try HubApi . shared . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
460
+ try hub . writeDownloadMetadata ( commitHash: remoteCommitHash, etag: remoteEtag, metadataPath: metadataDestination)
460
461
461
462
return destination
462
463
}
@@ -515,6 +516,7 @@ public extension HubApi {
515
516
for filename in filenames {
516
517
let fileProgress = Progress ( totalUnitCount: 100 , parent: progress, pendingUnitCount: 1 )
517
518
let downloader = HubFileDownloader (
519
+ hub: self ,
518
520
repo: repo,
519
521
revision: revision,
520
522
repoDestination: repoDestination,
@@ -541,18 +543,18 @@ public extension HubApi {
541
543
}
542
544
543
545
@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)
546
548
}
547
549
548
550
@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)
551
553
}
552
554
553
555
@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)
556
558
}
557
559
}
558
560
0 commit comments