Skip to content

Commit 0fb25cd

Browse files
authored
Merge pull request #16 from rootstrap/feature/separate-key-name-from-file-name
Separate key name from fileName on MimeType
2 parents 5a5ee1f + fadd150 commit 0fb25cd

4 files changed

Lines changed: 55 additions & 27 deletions

File tree

Sources/RSSwiftNetworking/Extensions/Data+Base64.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ import Foundation
1111
// Helper to retrieve the right string value for base64 API uploaders
1212
public extension Data {
1313
func asBase64Param(withType type: MimeType = .jpeg) -> String {
14-
"data:\(type.rawValue);base64,\(self.base64EncodedString())"
14+
"data:\(type.contentType);base64,\(self.base64EncodedString())"
1515
}
1616
}

Sources/RSSwiftNetworking/Models/Base64Media.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import Foundation
1111
public class Base64Media: MultipartMedia {
1212
var base64: String
1313

14-
override init(key: String, data: Data, type: MimeType = .jpeg) {
14+
override public init(fileName: String, key: String, data: Data, type: MimeType = .jpeg) {
1515
self.base64 = data.asBase64Param(withType: type)
16-
super.init(key: key, data: data, type: type)
16+
super.init(fileName: fileName, key: key, data: data, type: type)
1717
}
1818
}

Sources/RSSwiftNetworking/Models/MultipartMedia.swift

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,73 @@
99
import Foundation
1010

1111
// Basic media MIME types, add more if needed.
12-
public enum MimeType: String {
13-
case jpeg = "image/jpeg"
14-
case bmp = "image/bmp"
15-
case png = "image/png"
12+
public enum MimeType {
13+
case jpeg
14+
case bmp
15+
case png
16+
case mov
17+
case mpeg
18+
case avi
19+
case json
20+
case custom(contentType: String, extension: String)
1621

17-
case mov = "video/quicktime"
18-
case mpeg = "video/mpeg"
19-
case avi = "video/avi"
20-
case json = "application/json"
21-
22-
case usd, usdz = "application/octet-stream"
22+
public var contentType: String {
23+
switch self {
24+
case .jpeg:
25+
return "image/jpeg"
26+
case .bmp:
27+
return "image/bmp"
28+
case .png:
29+
return "image/png"
30+
case .mov:
31+
return "video/quicktime"
32+
case .mpeg:
33+
return "video/mpeg"
34+
case .avi:
35+
return "video/avi"
36+
case .json:
37+
return "application/json"
38+
case .custom(let contentType, _):
39+
return contentType
40+
}
41+
}
2342

24-
func fileExtension() -> String {
43+
public var fileExtension: String {
2544
switch self {
26-
case .bmp: return ".bmp"
27-
case .png: return ".png"
28-
case .mov: return ".mov"
29-
case .mpeg: return ".mpeg"
30-
case .avi: return ".avi"
31-
case .json: return ".json"
32-
case .usd: return ".usd"
33-
case .usdz: return ".usdz"
34-
default: return ".jpg"
45+
case .jpeg:
46+
return ".jpg"
47+
case .bmp:
48+
return ".bmp"
49+
case .png:
50+
return ".png"
51+
case .mov:
52+
return ".mov"
53+
case .mpeg:
54+
return ".mpeg"
55+
case .avi:
56+
return ".avi"
57+
case .json:
58+
return ".json"
59+
case .custom( _, let fileExtension):
60+
return fileExtension
3561
}
3662
}
3763
}
3864

3965
public class MultipartMedia {
40-
public var key: String
66+
public var key: String // key name to send file
67+
public var fileName: String // file name
4168
public var data: Data
4269
public var type: MimeType
4370

4471
public var toFile: String {
45-
key.validFilename + type.fileExtension()
72+
fileName.validFilename + type.fileExtension
4673
}
4774

48-
public init(key: String, data: Data, type: MimeType = .jpeg) {
75+
public init(fileName: String, key: String, data: Data, type: MimeType = .jpeg) {
4976
self.key = key
5077
self.data = data
5178
self.type = type
79+
self.fileName = fileName
5280
}
5381
}

Sources/RSSwiftNetworkingAlamofire/AlamofireNetworkProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension DataRequest: Cancellable {}
145145

146146
fileprivate extension MultipartMedia {
147147
func embed(inForm multipart: MultipartFormData) {
148-
multipart.append(data, withName: key, fileName: toFile, mimeType: type.rawValue)
148+
multipart.append(data, withName: key, fileName: toFile, mimeType: type.contentType)
149149
}
150150
}
151151

0 commit comments

Comments
 (0)