Download.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Alamofire.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. extension Manager {
  24. private enum Downloadable {
  25. case Request(NSURLRequest)
  26. case ResumeData(NSData)
  27. }
  28. private func download(downloadable: Downloadable, destination: Request.DownloadFileDestination) -> Request {
  29. var downloadTask: NSURLSessionDownloadTask!
  30. switch downloadable {
  31. case .Request(let request):
  32. dispatch_sync(queue) {
  33. downloadTask = self.session.downloadTaskWithRequest(request)
  34. }
  35. case .ResumeData(let resumeData):
  36. dispatch_sync(queue) {
  37. downloadTask = self.session.downloadTaskWithResumeData(resumeData)
  38. }
  39. }
  40. let request = Request(session: session, task: downloadTask)
  41. if let downloadDelegate = request.delegate as? Request.DownloadTaskDelegate {
  42. downloadDelegate.downloadTaskDidFinishDownloadingToURL = { session, downloadTask, URL in
  43. return destination(URL, downloadTask.response as! NSHTTPURLResponse)
  44. }
  45. }
  46. delegate[request.delegate.task] = request.delegate
  47. if startRequestsImmediately {
  48. request.resume()
  49. }
  50. return request
  51. }
  52. // MARK: Request
  53. /**
  54. Creates a download request using the shared manager instance for the specified method and URL string.
  55. :param: method The HTTP method.
  56. :param: URLString The URL string.
  57. :param: headers The HTTP headers. `nil` by default.
  58. :param: destination The closure used to determine the destination of the downloaded file.
  59. :returns: The created download request.
  60. */
  61. public func download(method: Method, _ URLString: URLStringConvertible, headers: [String: String]? = nil, destination: Request.DownloadFileDestination) -> Request {
  62. let mutableURLRequest = URLRequest(method, URLString, headers: headers)
  63. return download(mutableURLRequest, destination: destination)
  64. }
  65. /**
  66. Creates a request for downloading from the specified URL request.
  67. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  68. :param: URLRequest The URL request
  69. :param: destination The closure used to determine the destination of the downloaded file.
  70. :returns: The created download request.
  71. */
  72. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  73. return download(.Request(URLRequest.URLRequest), destination: destination)
  74. }
  75. // MARK: Resume Data
  76. /**
  77. Creates a request for downloading from the resume data produced from a previous request cancellation.
  78. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  79. :param: resumeData The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask` when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional information.
  80. :param: destination The closure used to determine the destination of the downloaded file.
  81. :returns: The created download request.
  82. */
  83. public func download(resumeData: NSData, destination: Request.DownloadFileDestination) -> Request {
  84. return download(.ResumeData(resumeData), destination: destination)
  85. }
  86. }
  87. // MARK: -
  88. extension Request {
  89. /**
  90. A closure executed once a request has successfully completed in order to determine where to move the temporary file written to during the download process. The closure takes two arguments: the temporary file URL and the URL response, and returns a single argument: the file URL where the temporary file should be moved.
  91. */
  92. public typealias DownloadFileDestination = (NSURL, NSHTTPURLResponse) -> NSURL
  93. /**
  94. Creates a download file destination closure which uses the default file manager to move the temporary file to a file URL in the first available directory with the specified search path directory and search path domain mask.
  95. :param: directory The search path directory. `.DocumentDirectory` by default.
  96. :param: domain The search path domain mask. `.UserDomainMask` by default.
  97. :returns: A download file destination closure.
  98. */
  99. public class func suggestedDownloadDestination(directory: NSSearchPathDirectory = .DocumentDirectory, domain: NSSearchPathDomainMask = .UserDomainMask) -> DownloadFileDestination {
  100. return { temporaryURL, response -> NSURL in
  101. if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(directory, inDomains: domain)[0] as? NSURL {
  102. return directoryURL.URLByAppendingPathComponent(response.suggestedFilename!)
  103. }
  104. return temporaryURL
  105. }
  106. }
  107. // MARK: - DownloadTaskDelegate
  108. class DownloadTaskDelegate: TaskDelegate, NSURLSessionDownloadDelegate {
  109. var downloadTask: NSURLSessionDownloadTask? { return task as? NSURLSessionDownloadTask }
  110. var downloadProgress: ((Int64, Int64, Int64) -> Void)?
  111. var resumeData: NSData?
  112. override var data: NSData? { return resumeData }
  113. // MARK: - NSURLSessionDownloadDelegate
  114. // MARK: Override Closures
  115. var downloadTaskDidFinishDownloadingToURL: ((NSURLSession, NSURLSessionDownloadTask, NSURL) -> NSURL)?
  116. var downloadTaskDidWriteData: ((NSURLSession, NSURLSessionDownloadTask, Int64, Int64, Int64) -> Void)?
  117. var downloadTaskDidResumeAtOffset: ((NSURLSession, NSURLSessionDownloadTask, Int64, Int64) -> Void)?
  118. // MARK: Delegate Methods
  119. func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
  120. if let downloadTaskDidFinishDownloadingToURL = self.downloadTaskDidFinishDownloadingToURL {
  121. let destination = downloadTaskDidFinishDownloadingToURL(session, downloadTask, location)
  122. var fileManagerError: NSError?
  123. NSFileManager.defaultManager().moveItemAtURL(location, toURL: destination, error: &fileManagerError)
  124. if fileManagerError != nil {
  125. error = fileManagerError
  126. }
  127. }
  128. }
  129. func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
  130. if let downloadTaskDidWriteData = self.downloadTaskDidWriteData {
  131. downloadTaskDidWriteData(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
  132. } else {
  133. progress.totalUnitCount = totalBytesExpectedToWrite
  134. progress.completedUnitCount = totalBytesWritten
  135. downloadProgress?(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
  136. }
  137. }
  138. func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
  139. if let downloadTaskDidResumeAtOffset = self.downloadTaskDidResumeAtOffset {
  140. downloadTaskDidResumeAtOffset(session, downloadTask, fileOffset, expectedTotalBytes)
  141. } else {
  142. progress.totalUnitCount = expectedTotalBytes
  143. progress.completedUnitCount = fileOffset
  144. }
  145. }
  146. }
  147. }