|
|
@@ -1,15 +1,12 @@
|
|
|
-//! Freedesktop launcher install: `.desktop` entry + hicolor PNG from site favicon.
|
|
|
+//! Freedesktop launcher install: `.desktop` entry + favicon saved as fetched (e.g. SVG, PNG).
|
|
|
|
|
|
-use std::io::{Cursor, Read};
|
|
|
-use std::path::PathBuf;
|
|
|
+use std::io::Read;
|
|
|
+use std::path::{Path, PathBuf};
|
|
|
|
|
|
-use image::imageops::FilterType;
|
|
|
-use image::{DynamicImage, ImageFormat, RgbaImage};
|
|
|
use scraper::{Html, Selector};
|
|
|
use sha2::{Digest, Sha256};
|
|
|
use url::Url;
|
|
|
|
|
|
-const ICON_SIZE: u32 = 48;
|
|
|
const ID_HEX_LEN: usize = 12;
|
|
|
|
|
|
pub fn run_install(
|
|
|
@@ -20,16 +17,11 @@ pub fn run_install(
|
|
|
) -> Result<(), String> {
|
|
|
let url = normalize_url(url_arg)?;
|
|
|
let id = stable_id(&url);
|
|
|
- let icon_name = format!("appiphany-{id}");
|
|
|
+ let icon_stem = format!("appiphany-{id}");
|
|
|
let data_home = xdg_data_home();
|
|
|
let apps_dir = data_home.join("applications");
|
|
|
- let icon_dir = data_home
|
|
|
- .join("icons")
|
|
|
- .join("hicolor")
|
|
|
- .join(format!("{ICON_SIZE}x{ICON_SIZE}"))
|
|
|
- .join("apps");
|
|
|
+ let icon_dir = appiphany_icon_dir(&data_home);
|
|
|
let desktop_path = apps_dir.join(format!("appiphany-{id}.desktop"));
|
|
|
- let icon_path = icon_dir.join(format!("{icon_name}.png"));
|
|
|
|
|
|
if desktop_path.exists() && !force {
|
|
|
return Err(format!(
|
|
|
@@ -52,19 +44,22 @@ pub fn run_install(
|
|
|
.or(page_title.filter(|s| !s.is_empty()))
|
|
|
.unwrap_or_else(|| host_label(&url));
|
|
|
|
|
|
- let icon_written = match fetch_favicon_png(&url) {
|
|
|
- Ok(png) => {
|
|
|
- std::fs::write(&icon_path, png).map_err(|e| e.to_string())?;
|
|
|
- true
|
|
|
+ let icon_written = match fetch_favicon_bytes(&url) {
|
|
|
+ Ok((bytes, icon_url, content_type)) => {
|
|
|
+ clear_stored_icons(&icon_dir, &id).map_err(|e| e.to_string())?;
|
|
|
+ let ext = pick_icon_extension(&icon_url, content_type.as_deref(), &bytes);
|
|
|
+ let icon_path = icon_dir.join(format!("{icon_stem}.{ext}"));
|
|
|
+ std::fs::write(&icon_path, bytes).map_err(|e| e.to_string())?;
|
|
|
+ Some(icon_path)
|
|
|
}
|
|
|
Err(e) => {
|
|
|
eprintln!("appiphany: favicon: {e} (launcher will use a generic icon name)");
|
|
|
- false
|
|
|
+ None
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- let icon_value = if icon_written {
|
|
|
- icon_path
|
|
|
+ let icon_value = if let Some(ref path) = icon_written {
|
|
|
+ path
|
|
|
.to_str()
|
|
|
.ok_or("non-UTF8 icon path")?
|
|
|
.to_string()
|
|
|
@@ -76,8 +71,8 @@ pub fn run_install(
|
|
|
std::fs::write(&desktop_path, desktop_body).map_err(|e| e.to_string())?;
|
|
|
|
|
|
eprintln!("Wrote {}", desktop_path.display());
|
|
|
- if icon_written {
|
|
|
- eprintln!("Wrote {}", icon_path.display());
|
|
|
+ if let Some(ref p) = icon_written {
|
|
|
+ eprintln!("Wrote {}", p.display());
|
|
|
}
|
|
|
Ok(())
|
|
|
}
|
|
|
@@ -97,12 +92,8 @@ pub fn run_uninstall(
|
|
|
let desktop_path = data_home
|
|
|
.join("applications")
|
|
|
.join(format!("appiphany-{id}.desktop"));
|
|
|
- let icon_path = data_home
|
|
|
- .join("icons")
|
|
|
- .join("hicolor")
|
|
|
- .join(format!("{ICON_SIZE}x{ICON_SIZE}"))
|
|
|
- .join("apps")
|
|
|
- .join(format!("appiphany-{id}.png"));
|
|
|
+ let legacy_icon_path = legacy_hicolor_icon_path(&data_home, &id);
|
|
|
+ let icon_dir = appiphany_icon_dir(&data_home);
|
|
|
|
|
|
let mut removed = false;
|
|
|
if desktop_path.exists() {
|
|
|
@@ -110,11 +101,26 @@ pub fn run_uninstall(
|
|
|
eprintln!("Removed {}", desktop_path.display());
|
|
|
removed = true;
|
|
|
}
|
|
|
- if icon_path.exists() {
|
|
|
- std::fs::remove_file(&icon_path).map_err(|e| e.to_string())?;
|
|
|
- eprintln!("Removed {}", icon_path.display());
|
|
|
+ if legacy_icon_path.exists() {
|
|
|
+ std::fs::remove_file(&legacy_icon_path).map_err(|e| e.to_string())?;
|
|
|
+ eprintln!("Removed {}", legacy_icon_path.display());
|
|
|
removed = true;
|
|
|
}
|
|
|
+ if let Ok(entries) = std::fs::read_dir(&icon_dir) {
|
|
|
+ let prefix = format!("appiphany-{id}.");
|
|
|
+ for ent in entries.flatten() {
|
|
|
+ let name = ent.file_name();
|
|
|
+ let name = name.to_string_lossy();
|
|
|
+ if name.starts_with(&prefix) {
|
|
|
+ let p = ent.path();
|
|
|
+ if p.is_file() {
|
|
|
+ std::fs::remove_file(&p).map_err(|e| e.to_string())?;
|
|
|
+ eprintln!("Removed {}", p.display());
|
|
|
+ removed = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
if !removed {
|
|
|
return Err(format!("no launcher found for `{target}` (id {id})"));
|
|
|
@@ -139,6 +145,36 @@ fn xdg_data_home() -> PathBuf {
|
|
|
.unwrap_or_else(|| PathBuf::from("/tmp/.local/share"))
|
|
|
}
|
|
|
|
|
|
+fn appiphany_icon_dir(data_home: &Path) -> PathBuf {
|
|
|
+ data_home.join("icons").join("appiphany")
|
|
|
+}
|
|
|
+
|
|
|
+fn legacy_hicolor_icon_path(data_home: &Path, id: &str) -> PathBuf {
|
|
|
+ data_home
|
|
|
+ .join("icons")
|
|
|
+ .join("hicolor")
|
|
|
+ .join("48x48")
|
|
|
+ .join("apps")
|
|
|
+ .join(format!("appiphany-{id}.png"))
|
|
|
+}
|
|
|
+
|
|
|
+fn clear_stored_icons(icon_dir: &Path, id: &str) -> std::io::Result<()> {
|
|
|
+ if !icon_dir.exists() {
|
|
|
+ return Ok(());
|
|
|
+ }
|
|
|
+ let prefix = format!("appiphany-{id}.");
|
|
|
+ for ent in std::fs::read_dir(icon_dir)?.flatten() {
|
|
|
+ let name = ent.file_name();
|
|
|
+ if name.to_string_lossy().starts_with(&prefix) {
|
|
|
+ let p = ent.path();
|
|
|
+ if p.is_file() {
|
|
|
+ let _ = std::fs::remove_file(p);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+}
|
|
|
+
|
|
|
fn host_label(url: &str) -> String {
|
|
|
Url::parse(url)
|
|
|
.ok()
|
|
|
@@ -197,6 +233,14 @@ fn ureq_agent() -> ureq::Agent {
|
|
|
}
|
|
|
|
|
|
fn fetch_bytes(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>, String> {
|
|
|
+ let (bytes, _) = fetch_bytes_with_content_type(agent, url)?;
|
|
|
+ Ok(bytes)
|
|
|
+}
|
|
|
+
|
|
|
+fn fetch_bytes_with_content_type(
|
|
|
+ agent: &ureq::Agent,
|
|
|
+ url: &str,
|
|
|
+) -> Result<(Vec<u8>, Option<String>), String> {
|
|
|
let resp = agent
|
|
|
.get(url)
|
|
|
.set("User-Agent", "appiphany/1.0 (desktop-install)")
|
|
|
@@ -205,11 +249,14 @@ fn fetch_bytes(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>, String> {
|
|
|
if !(200..300).contains(&resp.status()) {
|
|
|
return Err(format!("HTTP {} for {url}", resp.status()));
|
|
|
}
|
|
|
+ let content_type = resp
|
|
|
+ .header("Content-Type")
|
|
|
+ .map(|s| s.split(';').next().unwrap_or(s).trim().to_ascii_lowercase());
|
|
|
let mut buf = Vec::new();
|
|
|
resp.into_reader()
|
|
|
.read_to_end(&mut buf)
|
|
|
.map_err(|e| e.to_string())?;
|
|
|
- Ok(buf)
|
|
|
+ Ok((buf, content_type))
|
|
|
}
|
|
|
|
|
|
fn fetch_page_title(page_url: &str) -> Option<String> {
|
|
|
@@ -223,7 +270,8 @@ fn fetch_page_title(page_url: &str) -> Option<String> {
|
|
|
.filter(|s| !s.is_empty())
|
|
|
}
|
|
|
|
|
|
-fn fetch_favicon_png(page_url: &str) -> Result<Vec<u8>, String> {
|
|
|
+/// Returns raw icon bytes, resolved icon URL, and optional Content-Type from the icon response.
|
|
|
+fn fetch_favicon_bytes(page_url: &str) -> Result<(Vec<u8>, String, Option<String>), String> {
|
|
|
let base = Url::parse(page_url).map_err(|e| e.to_string())?;
|
|
|
let agent = ureq_agent();
|
|
|
|
|
|
@@ -234,9 +282,8 @@ fn fetch_favicon_png(page_url: &str) -> Result<Vec<u8>, String> {
|
|
|
.or_else(|| base.join("/favicon.ico").ok().map(|u| u.to_string()));
|
|
|
|
|
|
let icon_url = icon_url.ok_or_else(|| "no favicon candidate URL".to_string())?;
|
|
|
- let bytes = fetch_bytes(&agent, &icon_url)?;
|
|
|
-
|
|
|
- decode_to_png(&bytes)
|
|
|
+ let (bytes, content_type) = fetch_bytes_with_content_type(&agent, &icon_url)?;
|
|
|
+ Ok((bytes, icon_url, content_type))
|
|
|
}
|
|
|
|
|
|
struct IconCandidate {
|
|
|
@@ -305,22 +352,83 @@ fn parse_largest_size(sizes: &str) -> Option<i32> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-fn decode_to_png(bytes: &[u8]) -> Result<Vec<u8>, String> {
|
|
|
- let img = image::load_from_memory(bytes).map_err(|e| e.to_string())?;
|
|
|
+fn extension_from_url_path(icon_url: &str) -> Option<&'static str> {
|
|
|
+ let path = Url::parse(icon_url).ok()?.path().to_ascii_lowercase();
|
|
|
+ let (_, ext) = path.rsplit_once('.')?;
|
|
|
+ match ext {
|
|
|
+ "svg" => Some("svg"),
|
|
|
+ "svgz" => Some("svgz"),
|
|
|
+ "png" => Some("png"),
|
|
|
+ "ico" => Some("ico"),
|
|
|
+ "jpg" | "jpeg" => Some("jpg"),
|
|
|
+ "gif" => Some("gif"),
|
|
|
+ "webp" => Some("webp"),
|
|
|
+ _ => None,
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- let rgba = to_rgba_scaled(img);
|
|
|
- let mut out = Vec::new();
|
|
|
- RgbaImage::from_raw(ICON_SIZE, ICON_SIZE, rgba)
|
|
|
- .ok_or_else(|| "icon decode produced invalid buffer".to_string())?
|
|
|
- .write_to(&mut Cursor::new(&mut out), ImageFormat::Png)
|
|
|
- .map_err(|e| e.to_string())?;
|
|
|
- Ok(out)
|
|
|
+fn extension_from_mime(mime: &str) -> Option<&'static str> {
|
|
|
+ match mime.trim() {
|
|
|
+ "image/svg+xml" => Some("svg"),
|
|
|
+ "image/png" => Some("png"),
|
|
|
+ "image/x-icon" | "image/vnd.microsoft.icon" => Some("ico"),
|
|
|
+ "image/jpeg" => Some("jpg"),
|
|
|
+ "image/gif" => Some("gif"),
|
|
|
+ "image/webp" => Some("webp"),
|
|
|
+ _ => None,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-fn to_rgba_scaled(img: DynamicImage) -> Vec<u8> {
|
|
|
- let img = img.to_rgba8();
|
|
|
- if img.width() == ICON_SIZE && img.height() == ICON_SIZE {
|
|
|
- return img.into_raw();
|
|
|
+fn extension_from_magic(bytes: &[u8]) -> Option<&'static str> {
|
|
|
+ if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
|
|
|
+ return Some("png");
|
|
|
+ }
|
|
|
+ if bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a") {
|
|
|
+ return Some("gif");
|
|
|
+ }
|
|
|
+ if bytes.len() >= 3 && bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff {
|
|
|
+ return Some("jpg");
|
|
|
+ }
|
|
|
+ if bytes.len() >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 1 && bytes[3] == 0 {
|
|
|
+ return Some("ico");
|
|
|
+ }
|
|
|
+ if bytes.len() >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 2 && bytes[3] == 0 {
|
|
|
+ return Some("ico");
|
|
|
+ }
|
|
|
+ let head = bytes.get(..512).unwrap_or(bytes);
|
|
|
+ if let Ok(s) = std::str::from_utf8(head) {
|
|
|
+ let t = s.trim_start();
|
|
|
+ if t.starts_with("<?xml") || t.starts_with("<svg") {
|
|
|
+ return Some("svg");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ None
|
|
|
+}
|
|
|
+
|
|
|
+fn pick_icon_extension(icon_url: &str, content_type: Option<&str>, bytes: &[u8]) -> &'static str {
|
|
|
+ extension_from_url_path(icon_url)
|
|
|
+ .or_else(|| content_type.and_then(extension_from_mime))
|
|
|
+ .or_else(|| extension_from_magic(bytes))
|
|
|
+ .unwrap_or("ico")
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod tests {
|
|
|
+ use super::*;
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn pick_ext_prefers_url_for_svg() {
|
|
|
+ let ext = pick_icon_extension(
|
|
|
+ "https://example.com/assets/icon.svg?v=1",
|
|
|
+ Some("text/html"),
|
|
|
+ b"<svg></svg>",
|
|
|
+ );
|
|
|
+ assert_eq!(ext, "svg");
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn pick_ext_sniffs_svg_when_no_hint() {
|
|
|
+ let ext = pick_icon_extension("https://example.com/favicon", None, b" <svg>");
|
|
|
+ assert_eq!(ext, "svg");
|
|
|
}
|
|
|
- image::imageops::resize(&img, ICON_SIZE, ICON_SIZE, FilterType::Lanczos3).into_raw()
|
|
|
}
|