fix
This commit is contained in:
@@ -8,6 +8,7 @@ use tauri::{AppHandle, Manager, State};
|
||||
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
use reqwest;
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
|
||||
mod player;
|
||||
use player::{PlayerCommand, PlayerController, PlayerShared, PlayerState};
|
||||
@@ -223,6 +224,53 @@ async fn fetch_url(_app: AppHandle, url: String) -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn fetch_image_data_url(url: String) -> Result<String, String> {
|
||||
// Fetch remote images via backend and return a data: URL.
|
||||
// This helps when WebView blocks http images (mixed-content) or some hosts block hotlinking.
|
||||
let parsed = reqwest::Url::parse(&url).map_err(|e| e.to_string())?;
|
||||
match parsed.scheme() {
|
||||
"http" | "https" => {}
|
||||
_ => return Err("Only http/https URLs are allowed".to_string()),
|
||||
}
|
||||
|
||||
let resp = reqwest::Client::new()
|
||||
.get(parsed)
|
||||
.header(reqwest::header::USER_AGENT, "RadioPlayer/1.0")
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
return Err(format!("HTTP {} while fetching image", status));
|
||||
}
|
||||
|
||||
let content_type = resp
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(|s| s.split(';').next().unwrap_or(s).trim().to_string())
|
||||
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||
|
||||
let bytes = resp.bytes().await.map_err(|e| e.to_string())?;
|
||||
const MAX_BYTES: usize = 2 * 1024 * 1024;
|
||||
if bytes.len() > MAX_BYTES {
|
||||
return Err("Image too large".to_string());
|
||||
}
|
||||
|
||||
// Be conservative: prefer image/* content types, but allow svg even if mislabelled.
|
||||
let looks_like_image = content_type.starts_with("image/")
|
||||
|| content_type == "application/svg+xml"
|
||||
|| url.to_lowercase().ends_with(".svg");
|
||||
if !looks_like_image {
|
||||
return Err(format!("Not an image content-type: {}", content_type));
|
||||
}
|
||||
|
||||
let b64 = general_purpose::STANDARD.encode(bytes);
|
||||
Ok(format!("data:{};base64,{}", content_type, b64))
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
@@ -296,6 +344,8 @@ pub fn run() {
|
||||
cast_set_volume,
|
||||
// allow frontend to request arbitrary URLs via backend (bypass CORS)
|
||||
fetch_url,
|
||||
// fetch remote images via backend (data: URL), helps with mixed-content
|
||||
fetch_image_data_url,
|
||||
// native player commands (step 1 scaffold)
|
||||
player_play,
|
||||
player_stop,
|
||||
|
||||
Reference in New Issue
Block a user