Skip to content

Commit 7f0d345

Browse files
Merge pull request #12 from FelixHerrmann/feature/uttype-conversion
[Feature] UTType Conversion
2 parents d79455a + 5dc2a36 commit 7f0d345

14 files changed

+55
-15
lines changed

Sources/MultipartFormData/Boundary.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ extension Boundary {
3838

3939
public var debugDescription: String {
4040
switch self {
41-
case .empty: return "Boundary must not be empty."
42-
case .tooLong: return "Boundary is too long. Max size is 70 characters."
43-
case .noASCII: return "Boundary contains at least one character that is not ASCII compatible."
41+
case .empty:
42+
return "Boundary must not be empty."
43+
case .tooLong:
44+
return "Boundary is too long. Max size is 70 characters."
45+
case .noASCII:
46+
return "Boundary contains at least one character that is not ASCII compatible."
4447
}
4548
}
4649
}

Sources/MultipartFormData/MediaType.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extension MediaType {
3737
public static let multipartFormData = MediaType(type: "multipart", subtype: "form-data")
3838

3939
public static let textPlain = MediaType(type: "text", subtype: "plain")
40+
public static let textCsv = MediaType(type: "text", subtype: "csv")
4041
public static let textHtml = MediaType(type: "text", subtype: "html")
4142
public static let textCss = MediaType(type: "text", subtype: "css")
4243

@@ -63,6 +64,34 @@ extension MediaType {
6364
}
6465
// swiftlint:enable missing_docs
6566

67+
// MARK: - UniformTypeIdentifiers
68+
69+
#if canImport(UniformTypeIdentifiers)
70+
import UniformTypeIdentifiers
71+
72+
extension MediaType {
73+
/// Create a media type from a uniform type.
74+
/// - Parameter uniformType: The uniform type (UTType).
75+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
76+
public init?(uniformType: UTType) {
77+
guard let mimeTypeSplit = uniformType.preferredMIMEType?.split(separator: "/") else { return nil }
78+
guard mimeTypeSplit.count == 2 else { return nil }
79+
self.type = String(mimeTypeSplit[0])
80+
self.subtype = String(mimeTypeSplit[1])
81+
}
82+
}
83+
84+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
85+
extension UTType {
86+
/// Create a uniform type from a media type.
87+
/// - Parameter mediaType: The media type.
88+
/// - Parameter supertype: Another UTType instance that the resulting type must conform to; for example, UTTypeData.
89+
public init?(mediaType: MediaType, conformingTo supertype: UTType = .data) {
90+
self.init(mimeType: mediaType._text, conformingTo: supertype)
91+
}
92+
}
93+
#endif
94+
6695
// MARK: - Debug
6796

6897
extension MediaType: CustomDebugStringConvertible {

Tests/MultipartFormDataTests/BoundaryTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class BoundaryTests: XCTestCase {
12-
1312
func testEmpty() {
1413
XCTAssertThrowsError(try Boundary(uncheckedBoundary: "")) { error in
1514
XCTAssertEqual(error as? Boundary.InvalidBoundaryError, .empty)

Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class BodyDataBuilderTests: XCTestCase {
12-
1312
func testSingleData() {
1413
let data = _buildData {
1514
Data("a".utf8)

Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class HTTPHeaderBuilderTests: XCTestCase {
12-
1312
func testAvailableHeaderCombinations() {
1413
let dispositionResult = _buildHeader {
1514
ContentDisposition(name: "a")

Tests/MultipartFormDataTests/Builder/MultipartFormDataBuilderTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class MultipartFormDataBuilderTests: XCTestCase {
12-
1312
func testSingleSubpart() throws {
1413
let subparts = _buildSubparts {
1514
Subpart {

Tests/MultipartFormDataTests/ContentDispositionTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class ContentDispositionTests: XCTestCase {
12-
1312
func testPercentEncodingError() throws {
1413
XCTAssertNoThrow(try ContentDisposition(uncheckedName: "a", uncheckedFilename: "a"))
1514

Tests/MultipartFormDataTests/ContentTypeTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class ContentTypeTests: XCTestCase {
12-
1312
func testBoundaryParameters() throws {
1413
let contentType = ContentType(boundary: try Boundary(uncheckedBoundary: "test"))
1514

Tests/MultipartFormDataTests/HTTPHeaderFieldTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class HTTPHeaderFieldTests: XCTestCase {
12-
1312
func testDebugDescription() {
1413
let parameter = HTTPHeaderParameter("name", value: "value")
1514
let testHeaderField = TestHeaderField(value: "value", parameters: [parameter])

Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import XCTest
99
@testable import MultipartFormData
1010

1111
final class HTTPHeaderParameterTests: XCTestCase {
12-
1312
func testArrayText() {
1413
let singleParameter = [
1514
HTTPHeaderParameter("test", value: "a")

0 commit comments

Comments
 (0)