feat: refactor rust commands

This commit is contained in:
2024-01-31 08:20:39 +07:00
parent b7f4af7883
commit 02e0309a41
9 changed files with 169 additions and 166 deletions

View File

@@ -0,0 +1,50 @@
use std::time::Duration;
use webpage::{Webpage, WebpageOptions};
#[derive(serde::Serialize)]
pub struct OpenGraphResponse {
title: String,
description: String,
url: String,
image: String,
}
#[tauri::command]
pub fn fetch_opg(url: String) -> Result<OpenGraphResponse, ()> {
let mut options = WebpageOptions::default();
options.allow_insecure = true;
options.max_redirections = 3;
options.timeout = Duration::from_secs(15);
let info = Webpage::from_url(&url, options).expect("Failed");
let html = info.html;
let result = OpenGraphResponse {
title: html
.opengraph
.properties
.get("title")
.cloned()
.unwrap_or_default(),
description: html
.opengraph
.properties
.get("description")
.cloned()
.unwrap_or_default(),
url: html
.opengraph
.properties
.get("url")
.cloned()
.unwrap_or_default(),
image: html
.opengraph
.images
.get(0)
.and_then(|i| Some(i.url.clone()))
.unwrap_or_default(),
};
Ok(result.into())
}