diff --git a/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift b/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift index 9a23931..6d8e132 100644 --- a/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift +++ b/Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift @@ -112,24 +112,26 @@ public struct NetworkFacade { var partIndex = 0 var uploadedPartsConfigs: [UploadedPartConfig] = [] - let uploadRefs = try await uploadMultipart.start(bucketId: bucketId, fileSize: fileSize, parts: Int(parts)) + let startUploadResult = try await uploadMultipart.start(bucketId: bucketId, fileSize: fileSize, parts: Int(parts)) + guard let uploadUrls = startUploadResult.urls else { + throw UploadError.MissingUploadUrl + } - print("REFS", uploadRefs) + if uploadUrls.count != Int(parts) { + throw UploadMultipartError.MorePartsThanUploadUrls + } + print("REFS", startUploadResult) func processEncryptedChunk(encryptedChunk: Data, partIndex: Int) async throws -> Void { let hash = encrypt.getFileContentHash(stream: InputStream(data: encryptedChunk)) - let uploadRef = uploadRefs[partIndex] - - guard let uploadUrl = uploadRef.urls?[partIndex] else { - throw UploadError.MissingUploadUrl - } + let uploadUrl = uploadUrls[partIndex] try await uploadMultipart.uploadPart(encryptedChunk: encryptedChunk, uploadUrl: uploadUrl, partIndex: partIndex){progress in print("UPLOAD PROGRESS FOR PART \(partIndex)", progress) } let uploadedPartConfig = UploadedPartConfig( hash: hash, - uuid: uploadRef.uuid + uuid: startUploadResult.uuid ) uploadedPartsConfigs.append(uploadedPartConfig) diff --git a/Sources/InternxtSwiftCore/Services/Network/UploadMultipart.swift b/Sources/InternxtSwiftCore/Services/Network/UploadMultipart.swift index f1685ae..5dc60ff 100644 --- a/Sources/InternxtSwiftCore/Services/Network/UploadMultipart.swift +++ b/Sources/InternxtSwiftCore/Services/Network/UploadMultipart.swift @@ -10,6 +10,7 @@ import Foundation enum UploadMultipartError: Error { case CannotOpenInputStream + case MorePartsThanUploadUrls } public struct UploadedPartConfig { @@ -58,11 +59,14 @@ public class UploadMultipart: NSObject { } } - func start(bucketId: String, fileSize: Int, parts: Int) async throws -> Array { + func start(bucketId: String, fileSize: Int, parts: Int) async throws -> StartUploadResult { let startUploadResponse = try await networkAPI.startUpload(bucketId: bucketId, uploadSize: fileSize, parts: parts) - return startUploadResponse.uploads + guard let startUploadResult = startUploadResponse.uploads.first else { + throw UploadError.MissingUploadUrl + } + return startUploadResult } func uploadPart(encryptedChunk: Data, uploadUrl: String, partIndex: Int, progressHandler: @escaping ProgressHandler) async throws -> Void {