|
|
@@ -659,6 +659,8 @@ open class SessionManager {
|
|
|
let formData = MultipartFormData()
|
|
|
multipartFormData(formData)
|
|
|
|
|
|
+ var tempFileURL: URL?
|
|
|
+
|
|
|
do {
|
|
|
var urlRequestWithContentType = try urlRequest.asURLRequest()
|
|
|
urlRequestWithContentType.setValue(formData.contentType, forHTTPHeaderField: "Content-Type")
|
|
|
@@ -682,6 +684,8 @@ open class SessionManager {
|
|
|
let fileName = UUID().uuidString
|
|
|
let fileURL = directoryURL.appendingPathComponent(fileName)
|
|
|
|
|
|
+ tempFileURL = fileURL
|
|
|
+
|
|
|
var directoryError: Error?
|
|
|
|
|
|
// Create directory inside serial queue to ensure two threads don't do this in parallel
|
|
|
@@ -719,6 +723,15 @@ open class SessionManager {
|
|
|
}
|
|
|
}
|
|
|
} catch {
|
|
|
+ // Cleanup the temp file in the event that the multipart form data encoding failed
|
|
|
+ if let tempFileURL = tempFileURL {
|
|
|
+ do {
|
|
|
+ try FileManager.default.removeItem(at: tempFileURL)
|
|
|
+ } catch {
|
|
|
+ // No-op
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
DispatchQueue.main.async { encodingCompletion?(.failure(error)) }
|
|
|
}
|
|
|
}
|