Browse Source

Session manager now cleans up temp multipart form data file after upload.

Christian Noon 9 years ago
parent
commit
9688b16f05
2 changed files with 14 additions and 5 deletions
  1. 13 1
      Source/SessionManager.swift
  2. 1 4
      Tests/UploadTests.swift

+ 13 - 1
Source/SessionManager.swift

@@ -697,12 +697,24 @@ open class SessionManager {
 
                     try formData.writeEncodedData(to: fileURL)
 
+                    let upload = self.upload(fileURL, with: urlRequestWithContentType)
+
+                    // Cleanup the temp file once the upload is complete
+                    upload.delegate.queue.addOperation {
+                        do {
+                            try FileManager.default.removeItem(at: fileURL)
+                        } catch {
+                            // No-op
+                        }
+                    }
+
                     DispatchQueue.main.async {
                         let encodingResult = MultipartFormDataEncodingResult.success(
-                            request: self.upload(fileURL, with: urlRequestWithContentType),
+                            request: upload,
                             streamingFromDisk: true,
                             streamFileURL: fileURL
                         )
+
                         encodingCompletion?(encodingResult)
                     }
                 }

+ 1 - 4
Tests/UploadTests.swift

@@ -475,10 +475,7 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
 
         if let streamingFromDisk = streamingFromDisk, let streamFilePath = streamFileURL?.path {
             XCTAssertTrue(streamingFromDisk, "streaming from disk should be true")
-            XCTAssertTrue(
-                FileManager.default.fileExists(atPath: streamFilePath),
-                "stream file path should exist"
-            )
+            XCTAssertFalse(FileManager.default.fileExists(atPath: streamFilePath), "stream file path should not exist")
         }
     }