Skip to content

Commit

Permalink
Backport MultipartRequestBody to old OS versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmassel committed Nov 27, 2024
1 parent d2d7db5 commit 220edf1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions native/swift/Sources/wordpress-api/MultipartRequestBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,29 @@ struct MultipartRequestBody {
data.append(boundaryData)
data.append(contentsOf: lineBreak)
data.append(part.httpHeadersData)
try fileHandle.write(contentsOf: data)
try write(data, to: fileHandle)

for try await bodyData in part.readData() {
try fileHandle.write(contentsOf: bodyData)
try write(bodyData, to: fileHandle)
}

try fileHandle.write(contentsOf: lineBreak)
try write(lineBreak, to: fileHandle)
}

try fileHandle.write(contentsOf: boundaryMarker + boundaryData + boundaryMarker + lineBreak)
try write(boundaryMarker + boundaryData + boundaryMarker + lineBreak, to: fileHandle)

try fileHandle.close()

return filePath
}

func write(_ data: Data, to fileHandle: FileHandle) throws {
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try fileHandle.write(contentsOf: data)
} else {
fileHandle.write(data)
}
}
}

enum HttpPart {
Expand Down

0 comments on commit 220edf1

Please sign in to comment.