i_slint_compiler/data_uri.rs
1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4pub fn decode_data_uri(data_uri: &str) -> Result<(Vec<u8>, String), String> {
5 let data_url =
6 data_url::DataUrl::process(data_uri).map_err(|e| format!("Invalid data URI: {e}"))?;
7
8 let media_type = data_url.mime_type();
9 if media_type.type_ != "image" {
10 return Err(format!(
11 "Unsupported media type: {media_type}. Only image/* data URLs are supported",
12 ));
13 }
14
15 let extension = if media_type.subtype.starts_with("svg") {
16 "svg".to_string()
17 } else {
18 media_type.subtype.clone()
19 };
20
21 let (decoded_data, _) =
22 data_url.decode_to_vec().map_err(|e| format!("Invalid data URI payload: {e}"))?;
23
24 Ok((decoded_data, extension))
25}