-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMultipartForm.swift
159 lines (130 loc) · 5.55 KB
/
MultipartForm.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import Foundation
enum MultipartFormError: Swift.Error {
case inaccessbileFile(path: String)
case impossible
}
struct MultipartFormField {
let name: String
let filename: String?
let mimeType: String?
let bytes: UInt64
fileprivate let inputStream: InputStream
init(text: String, name: String, filename: String? = nil, mimeType: String? = nil) {
self.init(data: text.data(using: .utf8)!, name: name, filename: filename, mimeType: mimeType)
}
init(data: Data, name: String, filename: String? = nil, mimeType: String? = nil) {
self.inputStream = InputStream(data: data)
self.name = name
self.filename = filename
self.bytes = UInt64(data.count)
self.mimeType = mimeType
}
init(fileAtPath path: String, name: String, filename: String? = nil, mimeType: String? = nil) throws {
guard let inputStream = InputStream(fileAtPath: path),
let attrs = try? FileManager.default.attributesOfItem(atPath: path),
let bytes = (attrs[FileAttributeKey.size] as? NSNumber)?.uint64Value else {
throw MultipartFormError.inaccessbileFile(path: path)
}
self.inputStream = inputStream
self.name = name
self.filename = filename ?? path.split(separator: "/").last.flatMap({ String($0) })
self.bytes = bytes
self.mimeType = mimeType
}
}
extension Array where Element == MultipartFormField {
private func multipartFormDestination(forceWriteToFile: Bool) throws -> (outputStream: OutputStream, tempFilePath: String?) {
let dest: OutputStream
let tempFilePath: String?
// Build the form data in memory if the content is estimated to be less than 10 MB. Otherwise, use a temporary file.
let thresholdBytesForUsingTmpFile = 10_000_000
let estimatedFormDataBytes = reduce(0) { $0 + $1.bytes }
if forceWriteToFile || estimatedFormDataBytes > thresholdBytesForUsingTmpFile {
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString).path
guard let stream = OutputStream(toFileAtPath: tempFile, append: false) else {
throw MultipartFormError.inaccessbileFile(path: tempFile)
}
dest = stream
tempFilePath = tempFile
} else {
dest = OutputStream.toMemory()
tempFilePath = nil
}
return (dest, tempFilePath)
}
func multipartFormDataStream(boundary: String, forceWriteToFile: Bool = false) throws -> Either<Data, URL> {
guard !isEmpty else {
return .left(Data())
}
let (dest, tempFilePath) = try multipartFormDestination(forceWriteToFile: forceWriteToFile)
// Build the form content
do {
dest.open()
defer { dest.close() }
writeMultipartFormData(destination: dest, boundary: boundary)
}
// Return the result as `InputStream`
if let tempFilePath {
return .right(URL(fileURLWithPath: tempFilePath))
}
if let data = dest.property(forKey: .dataWrittenToMemoryStreamKey) as? Data {
return .left(data)
}
throw MultipartFormError.impossible
}
private func writeMultipartFormData(destination dest: OutputStream, boundary: String) {
for field in self {
dest.writeMultipartForm(boundary: boundary, isEnd: false)
// Write headers
var disposition = ["form-data", "name=\"\(field.name)\""]
if let filename = field.filename {
disposition += ["filename=\"\(filename)\""]
}
dest.writeMultipartFormHeader(name: "Content-Disposition", value: disposition.joined(separator: "; "))
if let mimeType = field.mimeType {
dest.writeMultipartFormHeader(name: "Content-Type", value: mimeType)
}
// Write a linebreak between header and content
dest.writeMultipartFormLineBreak()
// Write content
field.inputStream.open()
defer {
field.inputStream.close()
}
let maxLength = 1024
var buffer = [UInt8](repeating: 0, count: maxLength)
while field.inputStream.hasBytesAvailable {
let bytes = field.inputStream.read(&buffer, maxLength: maxLength)
dest.write(data: Data(bytesNoCopy: &buffer, count: bytes, deallocator: .none))
}
dest.writeMultipartFormLineBreak()
}
dest.writeMultipartForm(boundary: boundary, isEnd: true)
}
}
private let multipartFormDataLineBreak = "\r\n"
private extension OutputStream {
func write(data: Data) {
let count = data.count
guard count > 0 else { return }
_ = data.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) in
write(ptr.bindMemory(to: Int8.self).baseAddress!, maxLength: count)
}
}
func writeMultipartForm(lineContent: String) {
write(data: "\(lineContent)\(multipartFormDataLineBreak)".data(using: .utf8)!)
}
func writeMultipartFormLineBreak() {
write(data: multipartFormDataLineBreak.data(using: .utf8)!)
}
func writeMultipartFormHeader(name: String, value: String) {
writeMultipartForm(lineContent: "\(name): \(value)")
}
func writeMultipartForm(boundary: String, isEnd: Bool) {
if isEnd {
writeMultipartForm(lineContent: "--\(boundary)--")
} else {
writeMultipartForm(lineContent: "--\(boundary)")
}
}
}