@@ -13,16 +13,24 @@ import Dispatch
13
13
internal class _HTTPURLProtocol : URLProtocol {
14
14
15
15
fileprivate var easyHandle : _EasyHandle !
16
+ fileprivate var totalDownloaded = 0
17
+ fileprivate var tempFileURL : URL
16
18
17
19
public override required init ( task: URLSessionTask , cachedResponse: CachedURLResponse ? , client: URLProtocolClient ? ) {
18
20
self . internalState = _InternalState. initial
21
+ let fileName = NSTemporaryDirectory ( ) + NSUUID( ) . uuidString + " .tmp "
22
+ _ = FileManager . default. createFile ( atPath: fileName, contents: nil )
23
+ self . tempFileURL = URL ( fileURLWithPath: fileName)
19
24
super. init ( request: task. originalRequest!, cachedResponse: cachedResponse, client: client)
20
25
self . task = task
21
26
self . easyHandle = _EasyHandle ( delegate: self )
22
27
}
23
28
24
29
public override required init ( request: URLRequest , cachedResponse: CachedURLResponse ? , client: URLProtocolClient ? ) {
25
30
self . internalState = _InternalState. initial
31
+ let fileName = NSTemporaryDirectory ( ) + NSUUID( ) . uuidString + " .tmp "
32
+ _ = FileManager . default. createFile ( atPath: fileName, contents: nil )
33
+ self . tempFileURL = URL ( fileURLWithPath: fileName)
26
34
super. init ( request: request, cachedResponse: cachedResponse, client: client)
27
35
self . easyHandle = _EasyHandle ( delegate: self )
28
36
}
@@ -399,8 +407,8 @@ internal extension _HTTPURLProtocol {
399
407
return . inMemory( nil )
400
408
case . downloadCompletionHandler:
401
409
// Data needs to be written to a file (i.e. a download task).
402
- let fileHandle = try ! FileHandle ( forWritingTo: task . tempFileURL)
403
- return . toFile( task . tempFileURL, fileHandle)
410
+ let fileHandle = try ! FileHandle ( forWritingTo: self . tempFileURL)
411
+ return . toFile( self . tempFileURL, fileHandle)
404
412
}
405
413
}
406
414
}
@@ -453,19 +461,19 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
453
461
let downloadDelegate = delegate as? URLSessionDownloadDelegate ,
454
462
let task = self . task as? URLSessionDownloadTask {
455
463
guard let s = self . task? . session as? URLSession else { fatalError ( ) }
456
- let fileHandle = try ! FileHandle ( forWritingTo: task . tempFileURL)
464
+ let fileHandle = try ! FileHandle ( forWritingTo: self . tempFileURL)
457
465
_ = fileHandle. seekToEndOfFile ( )
458
466
fileHandle. write ( data)
459
- self . task ? . totalDownloaded += data. count
467
+ self . totalDownloaded += data. count
460
468
461
469
s. delegateQueue. addOperation {
462
- downloadDelegate. urlSession ( s, downloadTask: task, didWriteData: Int64 ( data. count) , totalBytesWritten: Int64 ( t . totalDownloaded) ,
470
+ downloadDelegate. urlSession ( s, downloadTask: task, didWriteData: Int64 ( data. count) , totalBytesWritten: Int64 ( self . totalDownloaded) ,
463
471
totalBytesExpectedToWrite: Int64 ( self . easyHandle. fileLength) )
464
472
}
465
- if Int ( self . easyHandle. fileLength) == self . task ? . totalDownloaded {
473
+ if Int ( self . easyHandle. fileLength) == self . totalDownloaded {
466
474
fileHandle. closeFile ( )
467
475
s. delegateQueue. addOperation {
468
- downloadDelegate. urlSession ( s, downloadTask: task, didFinishDownloadingTo: t . tempFileURL)
476
+ downloadDelegate. urlSession ( s, downloadTask: task, didFinishDownloadingTo: self . tempFileURL)
469
477
}
470
478
}
471
479
}
0 commit comments