Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/MuxUploadSDK/PublicAPI/MuxUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class MuxUpload : Hashable, Equatable {

private let uploadInfo: UploadInfo
private let manageBySDK: Bool
private var id: String {
var id: String {
uploadInfo.id
}
private let uploadManager: UploadManager
Expand Down Expand Up @@ -170,7 +170,7 @@ public final class MuxUpload : Hashable, Equatable {
// Use an existing globally-managed upload if desired & one exists
if self.manageBySDK && fileWorker == nil {
// See if there's anything in progress already
fileWorker = uploadManager.findUploaderFor(videoFile: videoFile)
fileWorker = uploadManager.findChunkedFileUploader(inputFileURL: videoFile)
}
if fileWorker != nil && !forceRestart {
MuxUploadSDK.logger?.warning("start() called but upload is already in progress")
Expand All @@ -193,8 +193,8 @@ public final class MuxUpload : Hashable, Equatable {
InternalUploaderDelegate { [self] state in handleStateUpdate(state) }
)
fileWorker.start()
uploadManager.registerUploader(fileWorker, withId: id)
self.fileWorker = fileWorker
uploadManager.registerUpload(self)
}

/**
Expand Down
47 changes: 26 additions & 21 deletions Sources/MuxUploadSDK/PublicAPI/UploadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation
///
public final class UploadManager {

private var uploadersByID: [String : ChunkedFileUploader] = [:]
private var uploadsByID: [String : MuxUpload] = [:]
private var uploadsUpdateDelegatesByToken: [ObjectIdentifier : any UploadsUpdatedDelegate] = [:]
private let uploadActor = UploadCacheActor()
private lazy var uploaderDelegate: FileUploaderDelegate = FileUploaderDelegate(manager: self)
Expand All @@ -37,23 +37,21 @@ public final class UploadManager {
/// to track and control its state
/// Returns nil if there was no uplod in progress for thr given file
public func findStartedUpload(ofFile url: URL) -> MuxUpload? {
if let uploader = Dictionary<URL, ChunkedFileUploader>(
uniqueKeysWithValues: uploadersByID.mapValues { value in
(value.uploadInfo.videoFile, value)
for upload in uploadsByID.values {
if upload.videoFile == url {
return upload
}
.values
)[url] {
return MuxUpload(wrapping: uploader, uploadManager: self)
} else {
return nil
}

return nil
}

/// Returns all uploads currently-managed uploads.
/// Uploads are managed while in-progress or compelted.
/// Uploads become un-managed when canceled, or if the process dies after they complete
public func allManagedUploads() -> [MuxUpload] {
return uploadersByID.compactMap { (key, value) in MuxUpload(wrapping: value, uploadManager: self) }
// Sort upload list for consistent ordering
return Array(uploadsByID.values)
}

/// Attempts to resume an upload that was previously paused or interrupted by process death
Expand Down Expand Up @@ -100,27 +98,34 @@ public final class UploadManager {
}

internal func acknowledgeUpload(id: String) {
if let uploader = uploadersByID[id] {
if let uploader = uploadsByID[id] {
uploadsByID.removeValue(forKey: id)
uploader.cancel()
}
uploadersByID.removeValue(forKey: id)
Task.detached {
await self.uploadActor.remove(uploadID: id)
self.notifyDelegates()
}
}

internal func findUploaderFor(videoFile url: URL) -> ChunkedFileUploader? {
return Dictionary<URL, ChunkedFileUploader>(
uniqueKeysWithValues: uploadersByID.mapValues { value in
(value.uploadInfo.videoFile, value)
}
.values
)[url]
internal func findChunkedFileUploader(
inputFileURL: URL
) -> ChunkedFileUploader? {
findStartedUpload(
ofFile: inputFileURL
)?.fileWorker
}

internal func registerUploader(_ fileWorker: ChunkedFileUploader, withId id: String) {
uploadersByID.updateValue(fileWorker, forKey: fileWorker.uploadInfo.id)
internal func registerUpload(_ upload: MuxUpload) {
guard let fileWorker = upload.fileWorker else {
// Only started uploads, aka uploads with a file
// worker can be registered.
// TODO: Should this throw?
MuxUploadSDK.logger?.debug("registerUpload() called for an unstarted upload")
return
}

uploadsByID.updateValue(upload, forKey: upload.id)
fileWorker.addDelegate(withToken: UUID().uuidString, uploaderDelegate)
Task.detached {
await self.uploadActor.updateUpload(
Expand Down
4 changes: 2 additions & 2 deletions apps/Test App/Upload Test App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Test App/Preview Content\"";
DEVELOPMENT_TEAM = CX6AHWLHM6;
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "This app uploads photos from your camera roll";
Expand Down Expand Up @@ -498,7 +498,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Test App/Preview Content\"";
DEVELOPMENT_TEAM = CX6AHWLHM6;
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "This app uploads photos from your camera roll";
Expand Down