-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add extension for creating data from data url (#3474)
- Loading branch information
1 parent
1ed974d
commit 2909fd0
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,") | ||
} | ||
} |