@@ -28,7 +28,7 @@ import Foundation
28
28
///
29
29
public final class UploadManager {
30
30
31
- private var uploadersByID : [ String : ChunkedFileUploader ] = [ : ]
31
+ private var uploadsByID : [ String : MuxUpload ] = [ : ]
32
32
private var uploadsUpdateDelegatesByToken : [ ObjectIdentifier : any UploadsUpdatedDelegate ] = [ : ]
33
33
private let uploadActor = UploadCacheActor ( )
34
34
private lazy var uploaderDelegate : FileUploaderDelegate = FileUploaderDelegate ( manager: self )
@@ -37,23 +37,21 @@ public final class UploadManager {
37
37
/// to track and control its state
38
38
/// Returns nil if there was no uplod in progress for thr given file
39
39
public func findStartedUpload( ofFile url: URL ) -> MuxUpload ? {
40
- if let uploader = Dictionary < URL , ChunkedFileUploader > (
41
- uniqueKeysWithValues : uploadersByID . mapValues { value in
42
- ( value . uploadInfo . videoFile , value )
40
+ for upload in uploadsByID . values {
41
+ if upload . videoFile == url {
42
+ return upload
43
43
}
44
- . values
45
- ) [ url] {
46
- return MuxUpload ( wrapping: uploader, uploadManager: self )
47
- } else {
48
- return nil
49
44
}
45
+
46
+ return nil
50
47
}
51
48
52
49
/// Returns all uploads currently-managed uploads.
53
50
/// Uploads are managed while in-progress or compelted.
54
51
/// Uploads become un-managed when canceled, or if the process dies after they complete
55
52
public func allManagedUploads( ) -> [ MuxUpload ] {
56
- return uploadersByID. compactMap { ( key, value) in MuxUpload ( wrapping: value, uploadManager: self ) }
53
+ // Sort upload list for consistent ordering
54
+ return Array ( uploadsByID. values)
57
55
}
58
56
59
57
/// Attempts to resume an upload that was previously paused or interrupted by process death
@@ -100,27 +98,34 @@ public final class UploadManager {
100
98
}
101
99
102
100
internal func acknowledgeUpload( id: String ) {
103
- if let uploader = uploadersByID [ id] {
101
+ if let uploader = uploadsByID [ id] {
102
+ uploadsByID. removeValue ( forKey: id)
104
103
uploader. cancel ( )
105
104
}
106
- uploadersByID. removeValue ( forKey: id)
107
105
Task . detached {
108
106
await self . uploadActor. remove ( uploadID: id)
109
107
self . notifyDelegates ( )
110
108
}
111
109
}
112
110
113
- internal func findUploaderFor( videoFile url: URL ) -> ChunkedFileUploader ? {
114
- return Dictionary < URL , ChunkedFileUploader > (
115
- uniqueKeysWithValues: uploadersByID. mapValues { value in
116
- ( value. uploadInfo. videoFile, value)
117
- }
118
- . values
119
- ) [ url]
111
+ internal func findChunkedFileUploader(
112
+ inputFileURL: URL
113
+ ) -> ChunkedFileUploader ? {
114
+ findStartedUpload (
115
+ ofFile: inputFileURL
116
+ ) ? . fileWorker
120
117
}
121
118
122
- internal func registerUploader( _ fileWorker: ChunkedFileUploader , withId id: String ) {
123
- uploadersByID. updateValue ( fileWorker, forKey: fileWorker. uploadInfo. id)
119
+ internal func registerUpload( _ upload: MuxUpload ) {
120
+ guard let fileWorker = upload. fileWorker else {
121
+ // Only started uploads, aka uploads with a file
122
+ // worker can be registered.
123
+ // TODO: Should this throw?
124
+ MuxUploadSDK . logger? . debug ( " registerUpload() called for an unstarted upload " )
125
+ return
126
+ }
127
+
128
+ uploadsByID. updateValue ( upload, forKey: upload. id)
124
129
fileWorker. addDelegate ( withToken: UUID ( ) . uuidString, uploaderDelegate)
125
130
Task . detached {
126
131
await self . uploadActor. updateUpload (
0 commit comments