@@ -275,10 +275,12 @@ import Foundation
275
275
let callbackQueue = fetcherService. callbackQueue ?? DispatchQueue . main
276
276
277
277
task. observe ( . success) { snapshot in
278
+ let error = self . checkSizeOverflow ( task: snapshot. task, maxSize: maxSize)
278
279
callbackQueue. async {
279
280
if !completed {
280
281
completed = true
281
- completion ( task. downloadData, nil )
282
+ let data = error == nil ? task. downloadData : nil
283
+ completion ( data, error)
282
284
}
283
285
}
284
286
}
@@ -291,14 +293,8 @@ import Foundation
291
293
}
292
294
}
293
295
task. observe ( . progress) { snapshot in
294
- let task = snapshot. task
295
- if task. progress. totalUnitCount > maxSize || task. progress. completedUnitCount > maxSize {
296
- let error = StorageErrorCode . error ( withCode: . downloadSizeExceeded,
297
- infoDictionary: [
298
- " totalSize " : task. progress. totalUnitCount,
299
- " maxAllowedSize " : maxSize,
300
- ] )
301
- ( task as? StorageDownloadTask ) ? . cancel ( withError: error)
296
+ if let error = self . checkSizeOverflow ( task: snapshot. task, maxSize: maxSize) {
297
+ task. cancel ( withError: error)
302
298
}
303
299
}
304
300
task. enqueue ( )
@@ -695,4 +691,18 @@ import Foundation
695
691
self . storage = storage
696
692
self . path = path
697
693
}
694
+
695
+ /**
696
+ * For maxSize API, return an error if the size is exceeded.
697
+ */
698
+ private func checkSizeOverflow( task: StorageTask , maxSize: Int64 ) -> NSError ? {
699
+ if task. progress. totalUnitCount > maxSize || task. progress. completedUnitCount > maxSize {
700
+ return StorageErrorCode . error ( withCode: . downloadSizeExceeded,
701
+ infoDictionary: [
702
+ " totalSize " : task. progress. totalUnitCount,
703
+ " maxAllowedSize " : maxSize,
704
+ ] )
705
+ }
706
+ return nil
707
+ }
698
708
}
0 commit comments