Skip to content

Commit

Permalink
[swift5]: support arrays in multipart/form-data
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinjul committed Sep 12, 2022
1 parent 194d421 commit ea734b9
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 572 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,25 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
let upload = manager.upload(multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
} else {
mpForm.append(fileURL, withName: k)
for v in (v as? Array ?? [v]) {
switch v {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
} else {
mpForm.append(fileURL, withName: k)
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
case let data as Data:
mpForm.append(data, withName: k)
case let uuid as UUID:
mpForm.append(uuid.uuidString.data(using: String.Encoding.utf8)!, withName: k)
default:
fatalError("Unprocessable value \(v) with key \(k)")
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
case let data as Data:
mpForm.append(data, withName: k)
case let uuid as UUID:
mpForm.append(uuid.uuidString.data(using: String.Encoding.utf8)!, withName: k)
default:
fatalError("Unprocessable value \(v) with key \(k)")
}
}
}, to: URLString, method: xMethod, headers: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,60 +455,62 @@ private class FormDataEncoding: ParameterEncoding {
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

for (key, value) in parameters {
switch value {
case let fileURL as URL:
for value in (value as? Array ?? [value]) {
switch value {
case let fileURL as URL:
urlRequest = try configureFileUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
fileURL: fileURL
)
case let string as String:
if let data = string.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest = try configureFileUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
fileURL: fileURL
)
}
case let number as NSNumber:
case let string as String:
if let data = number.stringValue.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}
if let data = string.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

case let data as Data:
case let number as NSNumber:

urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
if let data = number.stringValue.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

case let uuid as UUID:
case let data as Data:

if let data = uuid.uuidString.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
case let uuid as UUID:

if let data = uuid.uuidString.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {

let upload = manager.upload(multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
} else {
mpForm.append(fileURL, withName: k)
for v in (v as? Array ?? [v]) {
switch v {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
} else {
mpForm.append(fileURL, withName: k)
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
case let data as Data:
mpForm.append(data, withName: k)
case let uuid as UUID:
mpForm.append(uuid.uuidString.data(using: String.Encoding.utf8)!, withName: k)
default:
fatalError("Unprocessable value \(v) with key \(k)")
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
case let data as Data:
mpForm.append(data, withName: k)
case let uuid as UUID:
mpForm.append(uuid.uuidString.data(using: String.Encoding.utf8)!, withName: k)
default:
fatalError("Unprocessable value \(v) with key \(k)")
}
}
}, to: URLString, method: xMethod, headers: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,60 +455,62 @@ private class FormDataEncoding: ParameterEncoding {
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

for (key, value) in parameters {
switch value {
case let fileURL as URL:
for value in (value as? Array ?? [value]) {
switch value {
case let fileURL as URL:

urlRequest = try configureFileUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
fileURL: fileURL
)

case let string as String:

if let data = string.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest = try configureFileUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
fileURL: fileURL
)
}

case let number as NSNumber:
case let string as String:

if let data = number.stringValue.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}
if let data = string.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

case let data as Data:
case let number as NSNumber:

urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
if let data = number.stringValue.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

case let uuid as UUID:
case let data as Data:

if let data = uuid.uuidString.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
case let uuid as UUID:

if let data = uuid.uuidString.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,60 +455,62 @@ private class FormDataEncoding: ParameterEncoding {
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

for (key, value) in parameters {
switch value {
case let fileURL as URL:
for value in (value as? Array ?? [value]) {
switch value {
case let fileURL as URL:

urlRequest = try configureFileUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
fileURL: fileURL
)

case let string as String:

if let data = string.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest = try configureFileUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
fileURL: fileURL
)
}

case let number as NSNumber:
case let string as String:

if let data = number.stringValue.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}
if let data = string.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

case let data as Data:
case let number as NSNumber:

urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
if let data = number.stringValue.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

case let uuid as UUID:
case let data as Data:

if let data = uuid.uuidString.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
case let uuid as UUID:

if let data = uuid.uuidString.data(using: .utf8) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
}
}

Expand Down
Loading

0 comments on commit ea734b9

Please sign in to comment.