Skip to content

Commit

Permalink
[_]: Add v1 and legacy download methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PixoDev committed Jun 21, 2024
1 parent b4aede9 commit dde342b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Sources/InternxtSwiftCore/Extensions/Sequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Sequence.swift
//
//
// Created by Robert Garcia on 21/6/24.
//

import Foundation
extension Sequence {
func asyncForEach(
_ operation: (Element) async throws -> Void
) async rethrows {
for element in self {
try await operation(element)
}
}
}
21 changes: 20 additions & 1 deletion Sources/InternxtSwiftCore/Services/Network/Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum DownloadError: Error {
case InvalidBucketId
case MissingShards
case V1DownloadDetected
case NoMirrorsFound
}

public struct DownloadResult {
Expand Down Expand Up @@ -62,7 +63,25 @@ public class Download: NSObject {
let info = try await networkAPI.getFileInfo(bucketId: bucketId, fileId: fileId, debug: debug)

if info.version == 1 {
_ = try await networkAPI.getFileMirrors(bucketId: bucketId, fileId: fileId, debug: debug)
let mirrors = try await networkAPI.getFileMirrors(bucketId: bucketId, fileId: fileId, debug: debug)
guard let mirror = mirrors.first else {
throw DownloadError.NoMirrorsFound
}

if mirrors.count > 1 {
// Legacy download here

try await mirrors.asyncForEach{mirror in
_ = try await downloadEncryptedFile(downloadUrl: mirror.url, destinationURL: destination)
}

return DownloadResult(url: destination, expectedContentHash: "NO_CONTENT_HASH", index: info.index)
}


let url = try await downloadEncryptedFile(downloadUrl: mirror.url, destinationURL: destination, progressHandler: progressHandler)

return DownloadResult(url: url, expectedContentHash: mirror.hash, index: info.index)
}

guard let shards = info.shards else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public struct NetworkFacade {
return decryptedFileURL
}

public func decryptFile(bucketId: String, destinationURL: URL, progressHandler: ProgressHandler, encryptedFileDownloadResult: DownloadResult) async throws -> URL {
public func decryptFile(bucketId: String, destinationURL: URL, progressHandler: ProgressHandler, encryptedFileDownloadResult: DownloadResult, ignoreHashMissmatchCheck: Bool = false) async throws -> URL {

if encryptedFileDownloadResult.url.fileSize == 0 {
throw NetworkFacadeError.FileIsEmpty
Expand All @@ -234,7 +234,7 @@ public struct NetworkFacade {


let hashMatch = encryptedContentHash.toHexString() == encryptedFileDownloadResult.expectedContentHash
if hashMatch == false {
if hashMatch == false && ignoreHashMissmatchCheck != false {
throw NetworkFacadeError.HashMissmatch
}

Expand Down

0 comments on commit dde342b

Please sign in to comment.