Skip to content

Commit

Permalink
Merge pull request #16 from rootstrap/feature/separate-key-name-from-…
Browse files Browse the repository at this point in the history
…file-name

Separate key name from fileName on MimeType
  • Loading branch information
tarruk authored Jun 7, 2023
2 parents 5a5ee1f + fadd150 commit 0fb25cd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Sources/RSSwiftNetworking/Extensions/Data+Base64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import Foundation
// Helper to retrieve the right string value for base64 API uploaders
public extension Data {
func asBase64Param(withType type: MimeType = .jpeg) -> String {
"data:\(type.rawValue);base64,\(self.base64EncodedString())"
"data:\(type.contentType);base64,\(self.base64EncodedString())"
}
}
4 changes: 2 additions & 2 deletions Sources/RSSwiftNetworking/Models/Base64Media.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Foundation
public class Base64Media: MultipartMedia {
var base64: String

override init(key: String, data: Data, type: MimeType = .jpeg) {
override public init(fileName: String, key: String, data: Data, type: MimeType = .jpeg) {
self.base64 = data.asBase64Param(withType: type)
super.init(key: key, data: data, type: type)
super.init(fileName: fileName, key: key, data: data, type: type)
}
}
74 changes: 51 additions & 23 deletions Sources/RSSwiftNetworking/Models/MultipartMedia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,73 @@
import Foundation

// Basic media MIME types, add more if needed.
public enum MimeType: String {
case jpeg = "image/jpeg"
case bmp = "image/bmp"
case png = "image/png"
public enum MimeType {
case jpeg
case bmp
case png
case mov
case mpeg
case avi
case json
case custom(contentType: String, extension: String)

case mov = "video/quicktime"
case mpeg = "video/mpeg"
case avi = "video/avi"
case json = "application/json"

case usd, usdz = "application/octet-stream"
public var contentType: String {
switch self {
case .jpeg:
return "image/jpeg"
case .bmp:
return "image/bmp"
case .png:
return "image/png"
case .mov:
return "video/quicktime"
case .mpeg:
return "video/mpeg"
case .avi:
return "video/avi"
case .json:
return "application/json"
case .custom(let contentType, _):
return contentType
}
}

func fileExtension() -> String {
public var fileExtension: String {
switch self {
case .bmp: return ".bmp"
case .png: return ".png"
case .mov: return ".mov"
case .mpeg: return ".mpeg"
case .avi: return ".avi"
case .json: return ".json"
case .usd: return ".usd"
case .usdz: return ".usdz"
default: return ".jpg"
case .jpeg:
return ".jpg"
case .bmp:
return ".bmp"
case .png:
return ".png"
case .mov:
return ".mov"
case .mpeg:
return ".mpeg"
case .avi:
return ".avi"
case .json:
return ".json"
case .custom( _, let fileExtension):
return fileExtension
}
}
}

public class MultipartMedia {
public var key: String
public var key: String // key name to send file
public var fileName: String // file name
public var data: Data
public var type: MimeType

public var toFile: String {
key.validFilename + type.fileExtension()
fileName.validFilename + type.fileExtension
}

public init(key: String, data: Data, type: MimeType = .jpeg) {
public init(fileName: String, key: String, data: Data, type: MimeType = .jpeg) {
self.key = key
self.data = data
self.type = type
self.fileName = fileName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension DataRequest: Cancellable {}

fileprivate extension MultipartMedia {
func embed(inForm multipart: MultipartFormData) {
multipart.append(data, withName: key, fileName: toFile, mimeType: type.rawValue)
multipart.append(data, withName: key, fileName: toFile, mimeType: type.contentType)
}
}

Expand Down

0 comments on commit 0fb25cd

Please sign in to comment.