Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit d9c7980

Browse files
committed
Remove temp file once HTTP request completes
1 parent 25b4ca5 commit d9c7980

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

WordPressKit/HTTPClient.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extension URLSession {
109109

110110
private func task(
111111
for builder: HTTPRequestBuilder,
112-
completion: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void
112+
completion originalCompletion: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void
113113
) throws -> URLSessionTask {
114114
var request = try builder.build(encodeBody: false)
115115

@@ -123,6 +123,7 @@ extension URLSession {
123123
let task: URLSessionTask
124124
let body = try builder.encodeMultipartForm(request: &request, forceWriteToFile: isBackgroundSession)
125125
?? builder.encodeXMLRPC(request: &request, forceWriteToFile: isBackgroundSession)
126+
var completion = originalCompletion
126127
if let body {
127128
// Use special `URLSession.uploadTask` API for multipart POST requests.
128129
task = body.map(
@@ -133,11 +134,17 @@ extension URLSession {
133134
return uploadTask(with: request, from: $0, completionHandler: completion)
134135
}
135136
},
136-
right: {
137+
right: { tempFileURL in
138+
// Remove the temp file, which contains request body, once the HTTP request completes.
139+
completion = { data, response, error in
140+
try? FileManager.default.removeItem(at: tempFileURL)
141+
originalCompletion(data, response, error)
142+
}
143+
137144
if callCompletionFromDelegate {
138-
return uploadTask(with: request, fromFile: $0)
145+
return uploadTask(with: request, fromFile: tempFileURL)
139146
} else {
140-
return uploadTask(with: request, fromFile: $0, completionHandler: completion)
147+
return uploadTask(with: request, fromFile: tempFileURL, completionHandler: completion)
141148
}
142149
}
143150
)

0 commit comments

Comments
 (0)