Skip to content

Commit

Permalink
feat: Add extension for creating data from data url (#3474)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Aug 27, 2020
1 parent 1ed974d commit 2909fd0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ios/Capacitor/Capacitor/Data+Capacitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extension Data: CapacitorExtension {}
public extension CapacitorExtensionTypeWrapper where T == Data {

static func data(base64EncodedOrDataUrl: String) -> Data? {
if isBase64DataUrl(base64EncodedOrDataUrl) {
if let url = URL(string: base64EncodedOrDataUrl) {
do {
return try T(contentsOf: url)
} catch {
return nil
}
}
return nil
} else {
return T(base64Encoded: base64EncodedOrDataUrl)
}
}

private static func isBase64DataUrl(_ base64EncodedOrDataUrl: String) -> Bool {
return base64EncodedOrDataUrl.starts(with: "data:") && base64EncodedOrDataUrl.contains("base64,")
}
}

0 comments on commit 2909fd0

Please sign in to comment.