2
0

Upload.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Uploadable {
  25. case Data(NSURLRequest, NSData)
  26. case File(NSURLRequest, NSURL)
  27. case Stream(NSURLRequest, NSInputStream)
  28. }
  29. private func upload(uploadable: Uploadable) -> Request {
  30. var uploadTask: NSURLSessionUploadTask!
  31. var HTTPBodyStream: NSInputStream?
  32. switch uploadable {
  33. case .Data(let request, let data):
  34. dispatch_sync(queue) {
  35. uploadTask = self.session.uploadTaskWithRequest(request, fromData: data)
  36. }
  37. case .File(let request, let fileURL):
  38. dispatch_sync(queue) {
  39. uploadTask = self.session.uploadTaskWithRequest(request, fromFile: fileURL)
  40. }
  41. case .Stream(let request, var stream):
  42. dispatch_sync(queue) {
  43. uploadTask = self.session.uploadTaskWithStreamedRequest(request)
  44. }
  45. HTTPBodyStream = stream
  46. }
  47. let request = Request(session: session, task: uploadTask)
  48. if HTTPBodyStream != nil {
  49. request.delegate.taskNeedNewBodyStream = { _, _ in
  50. return HTTPBodyStream
  51. }
  52. }
  53. delegate[request.delegate.task] = request.delegate
  54. if startRequestsImmediately {
  55. request.resume()
  56. }
  57. return request
  58. }
  59. // MARK: File
  60. /**
  61. Creates a request for uploading a file to the specified URL request.
  62. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  63. :param: URLRequest The URL request
  64. :param: file The file to upload
  65. :returns: The created upload request.
  66. */
  67. public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
  68. return upload(.File(URLRequest.URLRequest, file))
  69. }
  70. /**
  71. Creates a request for uploading a file to the specified URL request.
  72. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  73. :param: method The HTTP method.
  74. :param: URLString The URL string.
  75. :param: file The file to upload
  76. :returns: The created upload request.
  77. */
  78. public func upload(method: Method, _ URLString: URLStringConvertible, file: NSURL) -> Request {
  79. return upload(URLRequest(method, URLString), file: file)
  80. }
  81. // MARK: Data
  82. /**
  83. Creates a request for uploading data to the specified URL request.
  84. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  85. :param: URLRequest The URL request
  86. :param: data The data to upload
  87. :returns: The created upload request.
  88. */
  89. public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
  90. return upload(.Data(URLRequest.URLRequest, data))
  91. }
  92. /**
  93. Creates a request for uploading data to the specified URL request.
  94. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  95. :param: method The HTTP method.
  96. :param: URLString The URL string.
  97. :param: data The data to upload
  98. :returns: The created upload request.
  99. */
  100. public func upload(method: Method, _ URLString: URLStringConvertible, data: NSData) -> Request {
  101. return upload(URLRequest(method, URLString), data: data)
  102. }
  103. // MARK: Stream
  104. /**
  105. Creates a request for uploading a stream to the specified URL request.
  106. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  107. :param: URLRequest The URL request
  108. :param: stream The stream to upload
  109. :returns: The created upload request.
  110. */
  111. public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
  112. return upload(.Stream(URLRequest.URLRequest, stream))
  113. }
  114. /**
  115. Creates a request for uploading a stream to the specified URL request.
  116. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  117. :param: method The HTTP method.
  118. :param: URLString The URL string.
  119. :param: stream The stream to upload.
  120. :returns: The created upload request.
  121. */
  122. public func upload(method: Method, _ URLString: URLStringConvertible, stream: NSInputStream) -> Request {
  123. return upload(URLRequest(method, URLString), stream: stream)
  124. }
  125. }
  126. // MARK: -
  127. extension Request {
  128. // MARK: - UploadTaskDelegate
  129. class UploadTaskDelegate: DataTaskDelegate {
  130. var uploadTask: NSURLSessionUploadTask! { return task as! NSURLSessionUploadTask }
  131. var uploadProgress: ((Int64, Int64, Int64) -> Void)!
  132. // MARK: - NSURLSessionTaskDelegate
  133. func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
  134. progress.totalUnitCount = totalBytesExpectedToSend
  135. progress.completedUnitCount = totalBytesSent
  136. uploadProgress?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
  137. }
  138. }
  139. }