Skip to content

Commit

Permalink
[_]:WIP test multipart upload
Browse files Browse the repository at this point in the history
  • Loading branch information
PixoDev committed Nov 21, 2023
1 parent dd4ce58 commit 75855c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 10 additions & 8 deletions Sources/InternxtSwiftCore/Services/Network/NetworkFacade.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

enum UploadMultipartError: Error {
case CannotOpenInputStream
case MorePartsThanUploadUrls
}

public struct UploadedPartConfig {
Expand Down Expand Up @@ -58,11 +59,14 @@ public class UploadMultipart: NSObject {
}
}

func start(bucketId: String, fileSize: Int, parts: Int) async throws -> Array<StartUploadResult> {
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 {
Expand Down

0 comments on commit 75855c0

Please sign in to comment.