提交 9dc80efb 编写于 作者: C Christian Noon

Refactored upload APIs for MultipartFormData to support custom boundaries

上级 218561de
......@@ -337,6 +337,7 @@ public enum AF {
/// - Parameters:
/// - multipartFormData: The closure used to append body parts to the `MultipartFormData`.
/// - encodingMemoryThreshold: The encoding memory threshold in bytes. `10_000_000` bytes by default.
/// - fileManager: The `FileManager` instance to use to manage streaming and encoding.
/// - url: The `URLConvertible` value.
/// - method: The `HTTPMethod`, `.post` by default.
/// - headers: The `HTTPHeaders`, `nil` by default.
......@@ -345,12 +346,14 @@ public enum AF {
/// - Returns: The created `UploadRequest`.
public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
usingThreshold encodingMemoryThreshold: UInt64 = MultipartUpload.encodingMemoryThreshold,
fileManager: FileManager = .default,
to url: URLConvertible,
method: HTTPMethod = .post,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil) -> UploadRequest {
return Session.default.upload(multipartFormData: multipartFormData,
usingThreshold: encodingMemoryThreshold,
fileManager: fileManager,
to: url,
method: method,
headers: headers,
......@@ -381,7 +384,7 @@ public enum AF {
///
/// - Returns: The `UploadRequest` created.
@discardableResult
public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
public static func upload(multipartFormData: MultipartFormData,
usingThreshold encodingMemoryThreshold: UInt64 = MultipartUpload.encodingMemoryThreshold,
with urlRequest: URLRequestConvertible,
interceptor: RequestInterceptor? = nil) -> UploadRequest {
......
......@@ -100,7 +100,8 @@ open class MultipartFormData {
/// The boundary used to separate the body parts in the encoded form data.
public let boundary: String
private let fileManager: FileManager
let fileManager: FileManager
private var bodyParts: [BodyPart]
private var bodyPartError: AFError?
private let streamBufferSize: Int
......
......@@ -31,7 +31,7 @@ open class MultipartUpload {
lazy var result = Result { try build() }
let isInBackgroundSession: Bool
let multipartBuilder: (MultipartFormData) -> Void
let multipartFormData: MultipartFormData
let encodingMemoryThreshold: UInt64
let request: URLRequestConvertible
let fileManager: FileManager
......@@ -39,25 +39,21 @@ open class MultipartUpload {
init(isInBackgroundSession: Bool,
encodingMemoryThreshold: UInt64 = MultipartUpload.encodingMemoryThreshold,
request: URLRequestConvertible,
fileManager: FileManager = .default,
multipartBuilder: @escaping (MultipartFormData) -> Void) {
multipartFormData: MultipartFormData) {
self.isInBackgroundSession = isInBackgroundSession
self.encodingMemoryThreshold = encodingMemoryThreshold
self.request = request
self.fileManager = fileManager
self.multipartBuilder = multipartBuilder
self.fileManager = multipartFormData.fileManager
self.multipartFormData = multipartFormData
}
func build() throws -> (request: URLRequest, uploadable: UploadRequest.Uploadable) {
let formData = MultipartFormData(fileManager: fileManager)
multipartBuilder(formData)
var urlRequest = try request.asURLRequest()
urlRequest.setValue(formData.contentType, forHTTPHeaderField: "Content-Type")
urlRequest.setValue(multipartFormData.contentType, forHTTPHeaderField: "Content-Type")
let uploadable: UploadRequest.Uploadable
if formData.contentLength < encodingMemoryThreshold && !isInBackgroundSession {
let data = try formData.encode()
if multipartFormData.contentLength < encodingMemoryThreshold && !isInBackgroundSession {
let data = try multipartFormData.encode()
uploadable = .data(data)
} else {
......@@ -69,7 +65,7 @@ open class MultipartUpload {
try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
do {
try formData.writeEncodedData(to: fileURL)
try multipartFormData.writeEncodedData(to: fileURL)
} catch {
// Cleanup after attempted write if it fails.
try? fileManager.removeItem(at: fileURL)
......
......@@ -326,22 +326,23 @@ open class Session {
interceptor: RequestInterceptor? = nil) -> UploadRequest {
let convertible = ParameterlessRequestConvertible(url: url, method: method, headers: headers)
return upload(multipartFormData: multipartFormData,
let formData = MultipartFormData(fileManager: fileManager)
multipartFormData(formData)
return upload(multipartFormData: formData,
usingThreshold: encodingMemoryThreshold,
with: convertible,
interceptor: interceptor)
}
open func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
open func upload(multipartFormData: MultipartFormData,
usingThreshold encodingMemoryThreshold: UInt64 = MultipartUpload.encodingMemoryThreshold,
fileManager: FileManager = .default,
with request: URLRequestConvertible,
interceptor: RequestInterceptor? = nil) -> UploadRequest {
let multipartUpload = MultipartUpload(isInBackgroundSession: (session.configuration.identifier != nil),
encodingMemoryThreshold: encodingMemoryThreshold,
request: request,
fileManager: fileManager,
multipartBuilder: multipartFormData)
multipartFormData: multipartFormData)
return upload(multipartUpload, interceptor: interceptor)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册