@@ -22,7 +22,7 @@ class ChunkedFileUploader {
22
22
private var _currentState : InternalUploadState = . ready
23
23
private var overallProgress : Progress = Progress ( )
24
24
private var lastSeenUpdate : InternalUploadState ? = nil
25
- private var lastReadCount : UInt64 = 0
25
+ private var lastByte : UInt64 = 0
26
26
27
27
func addDelegate( withToken token: Int , _ delegate: ChunkedFileUploaderDelegate ) {
28
28
delegates. updateValue ( delegate, forKey: token)
@@ -87,6 +87,8 @@ class ChunkedFileUploader {
87
87
let task = Task . detached { [ self ] in
88
88
do {
89
89
// It's fine if it's already open, that's handled by ignoring the call
90
+ try file. openFile ( fileURL: uploadInfo. videoFile)
91
+ try file. seekTo ( byte: lastByte)
90
92
let result = try await makeWorker ( ) . performUpload ( )
91
93
file. close ( )
92
94
@@ -101,14 +103,14 @@ class ChunkedFileUploader {
101
103
if let lastUpdate = lastSeenUpdate {
102
104
switch lastUpdate {
103
105
case . uploading( let update) : do {
104
- if lastReadCount > 0 {
105
- lastReadCount = UInt64 ( update. progress. completedUnitCount)
106
+ if lastByte > 0 {
107
+ lastByte = UInt64 ( update. progress. completedUnitCount)
106
108
}
107
109
}
108
110
default : break
109
111
}
110
112
}
111
- let uploadError = InternalUploaderError ( reason: error, lastByte: lastReadCount )
113
+ let uploadError = InternalUploaderError ( reason: error, lastByte: lastByte )
112
114
notifyStateFromWorker ( . failure( uploadError) )
113
115
}
114
116
@@ -120,8 +122,7 @@ class ChunkedFileUploader {
120
122
return Worker (
121
123
uploadInfo: uploadInfo,
122
124
chunkedFile: file,
123
- progress: overallProgress,
124
- startByte: lastReadCount
125
+ progress: overallProgress
125
126
) { progress, startTime, eventTime in
126
127
let update = Update (
127
128
progress: progress,
@@ -148,7 +149,7 @@ class ChunkedFileUploader {
148
149
149
150
if case . uploading( let update) = state {
150
151
let count = update. progress. completedUnitCount
151
- lastReadCount = UInt64 ( count)
152
+ lastByte = UInt64 ( count)
152
153
}
153
154
154
155
for delegate in delegates. values {
@@ -174,7 +175,7 @@ class ChunkedFileUploader {
174
175
init ( uploadInfo: UploadInfo , file: ChunkedFile , startingByte: UInt64 = 0 ) {
175
176
self . uploadInfo = uploadInfo
176
177
self . file = file
177
- self . lastReadCount = startingByte
178
+ self . lastByte = startingByte
178
179
}
179
180
180
181
enum InternalUploadState {
@@ -213,19 +214,14 @@ fileprivate actor Worker {
213
214
private let chunkedFile : ChunkedFile
214
215
private let overallProgress : Progress
215
216
private let progressHandler : ProgressHandler
216
- private let startingReadCount : UInt64
217
217
218
218
func performUpload( ) async throws -> ChunkedFileUploader . Update {
219
- try chunkedFile. openFile ( fileURL: uploadInfo. videoFile)
220
- try chunkedFile. seekTo ( byte: startingReadCount)
221
-
222
219
let startTime = Date ( ) . timeIntervalSince1970
223
220
224
221
let fileSize = chunkedFile. fileSize
225
222
let wideFileSize = Int64 ( fileSize)
226
223
overallProgress. totalUnitCount = wideFileSize
227
224
overallProgress. isCancellable = false
228
- overallProgress. completedUnitCount = Int64 ( startingReadCount)
229
225
230
226
var readBytes : Int
231
227
repeat {
@@ -276,13 +272,11 @@ fileprivate actor Worker {
276
272
uploadInfo: UploadInfo ,
277
273
chunkedFile: ChunkedFile ,
278
274
progress: Progress ,
279
- startByte: UInt64 ,
280
275
_ progressHandler: @escaping ProgressHandler
281
276
) {
282
277
self . uploadInfo = uploadInfo
283
278
self . chunkedFile = chunkedFile
284
279
self . progressHandler = progressHandler
285
280
self . overallProgress = progress
286
- self . startingReadCount = startByte
287
281
}
288
282
}
0 commit comments