This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
/
DownloadQueue.swift
259 lines (202 loc) · 7.86 KB
/
DownloadQueue.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import WebKit
private let downloadOperationQueue = OperationQueue()
protocol DownloadDelegate {
func download(_ download: Download, didCompleteWithError error: Error?)
func download(_ download: Download, didDownloadBytes bytesDownloaded: Int64)
func download(_ download: Download, didFinishDownloadingTo location: URL)
}
class Download: NSObject {
var delegate: DownloadDelegate?
fileprivate(set) var filename: String
fileprivate(set) var mimeType: String
fileprivate(set) var isComplete = false
fileprivate(set) var totalBytesExpected: Int64?
fileprivate(set) var bytesDownloaded: Int64
override init() {
self.filename = "unknown"
self.mimeType = "application/octet-stream"
self.bytesDownloaded = 0
super.init()
}
func cancel() {}
func pause() {}
func resume() {}
fileprivate func uniqueDownloadPathForFilename(_ filename: String) throws -> URL {
let downloadsPath = try FileManager.default.downloadsPath()
let basePath = downloadsPath.appendingPathComponent(filename)
let fileExtension = basePath.pathExtension
let filenameWithoutExtension = !fileExtension.isEmpty ? String(filename.dropLast(fileExtension.count + 1)) : filename
var proposedPath = basePath
var count = 0
while FileManager.default.fileExists(atPath: proposedPath.path) {
count += 1
let proposedFilenameWithoutExtension = "\(filenameWithoutExtension) (\(count))"
proposedPath = downloadsPath.appendingPathComponent(proposedFilenameWithoutExtension).appendingPathExtension(fileExtension)
}
return proposedPath
}
}
class HTTPDownload: Download {
let preflightResponse: URLResponse
let request: URLRequest
var state: URLSessionTask.State {
return task?.state ?? .suspended
}
fileprivate(set) var session: URLSession?
fileprivate(set) var task: URLSessionDownloadTask?
fileprivate(set) var cookieStore: WKHTTPCookieStore
private var resumeData: Data?
// Used to avoid name spoofing using Unicode RTL char to change file extension
public static func stripUnicode(fromFilename string: String) -> String {
let validFilenameSet = CharacterSet(charactersIn: ":/")
.union(.newlines)
.union(.controlCharacters)
.union(.illegalCharacters)
return string.components(separatedBy: validFilenameSet).joined()
}
init(cookieStore: WKHTTPCookieStore, preflightResponse: URLResponse, request: URLRequest) {
self.cookieStore = cookieStore
self.preflightResponse = preflightResponse
self.request = request
super.init()
if let filename = preflightResponse.suggestedFilename {
self.filename = HTTPDownload.stripUnicode(fromFilename: filename)
}
if let mimeType = preflightResponse.mimeType {
self.mimeType = mimeType
}
self.totalBytesExpected = preflightResponse.expectedContentLength > 0 ? preflightResponse.expectedContentLength : nil
self.session = URLSession(configuration: .ephemeral, delegate: self, delegateQueue: downloadOperationQueue)
self.task = session?.downloadTask(with: request)
}
override func cancel() {
task?.cancel()
}
override func pause() {
task?.cancel(byProducingResumeData: { resumeData in
self.resumeData = resumeData
})
}
override func resume() {
cookieStore.getAllCookies { [self] cookies in
cookies.forEach { cookie in
session?.configuration.httpCookieStorage?.setCookie(cookie)
}
guard let resumeData = self.resumeData else {
self.task?.resume()
return
}
self.task = session?.downloadTask(withResumeData: resumeData)
self.task?.resume()
}
}
}
extension HTTPDownload: URLSessionTaskDelegate, URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
// Don't bubble up cancellation as an error if the
// error is `.cancelled` and we have resume data.
if let urlError = error as? URLError,
urlError.code == .cancelled,
resumeData != nil {
return
}
delegate?.download(self, didCompleteWithError: error)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
bytesDownloaded = totalBytesWritten
totalBytesExpected = totalBytesExpectedToWrite
delegate?.download(self, didDownloadBytes: bytesWritten)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let destination = try uniqueDownloadPathForFilename(filename)
try FileManager.default.moveItem(at: location, to: destination)
isComplete = true
delegate?.download(self, didFinishDownloadingTo: destination)
} catch {
delegate?.download(self, didCompleteWithError: error)
}
}
}
protocol DownloadQueueDelegate {
func downloadQueue(_ downloadQueue: DownloadQueue, didStartDownload download: Download)
func downloadQueue(_ downloadQueue: DownloadQueue, didDownloadCombinedBytes combinedBytesDownloaded: Int64, combinedTotalBytesExpected: Int64?)
func downloadQueue(_ downloadQueue: DownloadQueue, download: Download, didFinishDownloadingTo location: URL)
func downloadQueue(_ downloadQueue: DownloadQueue, didCompleteWithError error: Error?)
}
class DownloadQueue {
var downloads: [Download]
var delegate: DownloadQueueDelegate?
var isEmpty: Bool {
return downloads.isEmpty
}
fileprivate var combinedBytesDownloaded: Int64 = 0
fileprivate var combinedTotalBytesExpected: Int64?
fileprivate var lastDownloadError: Error?
init() {
self.downloads = []
}
func enqueue(_ download: Download) {
// Clear the download stats if the queue was empty at the start.
if downloads.isEmpty {
combinedBytesDownloaded = 0
combinedTotalBytesExpected = 0
lastDownloadError = nil
}
downloads.append(download)
download.delegate = self
if let totalBytesExpected = download.totalBytesExpected, combinedTotalBytesExpected != nil {
combinedTotalBytesExpected! += totalBytesExpected
} else {
combinedTotalBytesExpected = nil
}
download.resume()
delegate?.downloadQueue(self, didStartDownload: download)
}
func cancelAll() {
for download in downloads where !download.isComplete {
download.cancel()
}
}
func pauseAll() {
for download in downloads where !download.isComplete {
download.pause()
}
}
func resumeAll() {
for download in downloads where !download.isComplete {
download.resume()
}
}
}
extension DownloadQueue: DownloadDelegate {
func download(_ download: Download, didCompleteWithError error: Error?) {
guard let error = error, let index = downloads.firstIndex(of: download) else {
return
}
lastDownloadError = error
downloads.remove(at: index)
if downloads.isEmpty {
delegate?.downloadQueue(self, didCompleteWithError: lastDownloadError)
}
}
func download(_ download: Download, didDownloadBytes bytesDownloaded: Int64) {
combinedBytesDownloaded += bytesDownloaded
delegate?.downloadQueue(self, didDownloadCombinedBytes: combinedBytesDownloaded, combinedTotalBytesExpected: combinedTotalBytesExpected)
}
func download(_ download: Download, didFinishDownloadingTo location: URL) {
guard let index = downloads.firstIndex(of: download) else {
return
}
downloads.remove(at: index)
delegate?.downloadQueue(self, download: download, didFinishDownloadingTo: location)
NotificationCenter.default.post(name: .fileDidDownload, object: location)
if downloads.isEmpty {
delegate?.downloadQueue(self, didCompleteWithError: lastDownloadError)
}
}
}