Skip to content

Commit ecd67b3

Browse files
authored
Merge pull request #1029 from nethraravindran/totalDownload-branch
2 parents d8f78f9 + 8185987 commit ecd67b3

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ import Dispatch
2727
open class URLSessionTask : NSObject, NSCopying {
2828
/// How many times the task has been suspended, 0 indicating a running task.
2929
internal var suspendCount = 1
30-
internal var totalDownloaded = 0
3130
internal var session: URLSessionProtocol! //change to nil when task completes
3231
internal let body: _Body
33-
internal let tempFileURL: URL
3432
fileprivate var _protocol: URLProtocol! = nil
3533

3634
/// All operations must run on this queue.
@@ -52,9 +50,6 @@ open class URLSessionTask : NSObject, NSCopying {
5250
originalRequest = nil
5351
body = .none
5452
workQueue = DispatchQueue(label: "URLSessionTask.notused.0")
55-
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
56-
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
57-
self.tempFileURL = URL(fileURLWithPath: fileName)
5853
super.init()
5954
}
6055
/// Create a data task. If there is a httpBody in the URLRequest, use that as a parameter
@@ -71,9 +66,6 @@ open class URLSessionTask : NSObject, NSCopying {
7166
self.taskIdentifier = taskIdentifier
7267
self.originalRequest = request
7368
self.body = body
74-
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
75-
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
76-
self.tempFileURL = URL(fileURLWithPath: fileName)
7769
super.init()
7870
if session.configuration.protocolClasses != nil {
7971
guard let protocolClasses = session.configuration.protocolClasses else { fatalError() }

Foundation/NSURLSession/http/HTTPURLProtocol.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ import Dispatch
1313
internal class _HTTPURLProtocol: URLProtocol {
1414

1515
fileprivate var easyHandle: _EasyHandle!
16+
fileprivate var totalDownloaded = 0
17+
fileprivate var tempFileURL: URL
1618

1719
public override required init(task: URLSessionTask, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
1820
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)
1924
super.init(request: task.originalRequest!, cachedResponse: cachedResponse, client: client)
2025
self.task = task
2126
self.easyHandle = _EasyHandle(delegate: self)
2227
}
2328

2429
public override required init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
2530
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)
2634
super.init(request: request, cachedResponse: cachedResponse, client: client)
2735
self.easyHandle = _EasyHandle(delegate: self)
2836
}
@@ -399,8 +407,8 @@ internal extension _HTTPURLProtocol {
399407
return .inMemory(nil)
400408
case .downloadCompletionHandler:
401409
// 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)
404412
}
405413
}
406414
}
@@ -453,19 +461,19 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
453461
let downloadDelegate = delegate as? URLSessionDownloadDelegate,
454462
let task = self.task as? URLSessionDownloadTask {
455463
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)
457465
_ = fileHandle.seekToEndOfFile()
458466
fileHandle.write(data)
459-
self.task?.totalDownloaded += data.count
467+
self.totalDownloaded += data.count
460468

461469
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),
463471
totalBytesExpectedToWrite: Int64(self.easyHandle.fileLength))
464472
}
465-
if Int(self.easyHandle.fileLength) == self.task?.totalDownloaded {
473+
if Int(self.easyHandle.fileLength) == self.totalDownloaded {
466474
fileHandle.closeFile()
467475
s.delegateQueue.addOperation {
468-
downloadDelegate.urlSession(s, downloadTask: task, didFinishDownloadingTo: t.tempFileURL)
476+
downloadDelegate.urlSession(s, downloadTask: task, didFinishDownloadingTo: self.tempFileURL)
469477
}
470478
}
471479
}

0 commit comments

Comments
 (0)