Jelajahi Sumber

Multipart form data upload now deletes temp file if encoding fails.

Christian Noon 9 tahun lalu
induk
melakukan
e68bfdccaa
1 mengubah file dengan 13 tambahan dan 0 penghapusan
  1. 13 0
      Source/SessionManager.swift

+ 13 - 0
Source/SessionManager.swift

@@ -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)) }
             }
         }