From 0f62444bee4a15c12bafefdc83ae524ae517ef06 Mon Sep 17 00:00:00 2001 From: firewire Date: Thu, 19 Mar 2026 02:52:17 -0400 Subject: [PATCH] Some refactoring --- src/app.rs | 111 +-------------------------------------------------- src/util.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 109 deletions(-) diff --git a/src/app.rs b/src/app.rs index 87a5381..b8cb7fc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,13 +1,11 @@ use crate::types; use crate::util::clamp; +use crate::util::download; +use crate::util::search_files; use eframe::egui; use egui::ColorImage; -use futures_util::stream::StreamExt; use human_bytes::human_bytes; use image::{ColorType, GenericImage, Rgba}; -use std::cmp::min; -use std::io::Write; -use std::path::Path; use tokio::sync::mpsc; pub struct Application { @@ -368,108 +366,3 @@ impl eframe::App for Application { self.render_center_panel(ctx); } } - -async fn search_files( - url: String, - query: String, - page: usize, - page_size: usize, -) -> Result<(Vec, types::Metadata), String> { - let full_url = format!("{}?q={}&p={}&s={}", url, query, page, page_size); - - let client = match reqwest::Client::builder() - .user_agent( - "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0", - ) - .danger_accept_invalid_certs(true) - .build() - { - Ok(client) => client, - Err(e) => return Err(format!("Failed to create the client: {}", e)), - }; - - let res = client.get(full_url).send().await; - let response = match res { - Ok(response_ok) => response_ok, - Err(e) => return Err(format!("Failed to download the file: {}", e)), - }; - if response.status() != reqwest::StatusCode::OK { - return Err(format!( - "Failed to download the file: {}", - response.status() - )); - } - - let results = match response.json::().await { - Ok(r) => r, - Err(e) => { - return Err(format!("Failed to deserialize results data: {}", e)); - } - }; - - Ok((results.results, results.metadata)) -} - -async fn download(url: String, dir: String) -> Result { - let file_name = Path::new(&url) - .file_name() - .unwrap() - .to_str() - .unwrap() - .to_string(); - let file_path = Path::new(&dir).join(file_name); - - if file_path.exists() { - return Ok(file_path.to_str().unwrap().to_string()); - } - - let mut file = match std::fs::File::create(file_path.clone()) { - Ok(file) => file, - Err(e) => return Err(format!("Failed to create the file: {}", e)), - }; - let client = match reqwest::Client::builder() - .user_agent( - "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0", - ) - .danger_accept_invalid_certs(true) - .build() - { - Ok(client) => client, - Err(e) => return Err(format!("Failed to create the client: {}", e)), - }; - let res = client.get(&url).send().await; - let response = match res { - Ok(response_ok) => response_ok, - Err(e) => return Err(format!("Failed to download the file: {}", e)), - }; - if response.status() != reqwest::StatusCode::OK { - return Err(format!( - "Failed to download the file: {}", - response.status() - )); - } - - let total_size_res = response - .content_length() - .ok_or("Failed to get the file size"); - let total_size = match total_size_res { - Ok(res_ok) => res_ok, - Err(e) => return Err(format!("Failed to get the file size: {}", e)), - }; - - let mut downloaded: u64 = 0; - let mut stream = response.bytes_stream(); - while let Some(item) = stream.next().await { - let chunk = item.or(Err(format!( - "Error while downloading file: {}", - url.clone() - )))?; - file.write_all(&chunk).or(Err(format!( - "Error while writing to file: {}", - file_path.to_str().unwrap() - )))?; - let new = min(downloaded + (chunk.len() as u64), total_size); - downloaded = new; - } - Ok(file_path.to_str().unwrap().to_string()) -} diff --git a/src/util.rs b/src/util.rs index 619a5c1..3b68914 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,3 +1,10 @@ +use futures_util::stream::StreamExt; +use std::cmp::min; +use std::io::Write; +use std::path::Path; + +use crate::types; + pub fn clamp(val: usize, min: usize, max: usize) -> usize { if val < min { min @@ -7,3 +14,108 @@ pub fn clamp(val: usize, min: usize, max: usize) -> usize { val } } + +pub async fn search_files( + url: String, + query: String, + page: usize, + page_size: usize, +) -> Result<(Vec, types::Metadata), String> { + let full_url = format!("{}?q={}&p={}&s={}", url, query, page, page_size); + + let client = match reqwest::Client::builder() + .user_agent( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0", + ) + .danger_accept_invalid_certs(true) + .build() + { + Ok(client) => client, + Err(e) => return Err(format!("Failed to create the client: {}", e)), + }; + + let res = client.get(full_url).send().await; + let response = match res { + Ok(response_ok) => response_ok, + Err(e) => return Err(format!("Failed to download the file: {}", e)), + }; + if response.status() != reqwest::StatusCode::OK { + return Err(format!( + "Failed to download the file: {}", + response.status() + )); + } + + let results = match response.json::().await { + Ok(r) => r, + Err(e) => { + return Err(format!("Failed to deserialize results data: {}", e)); + } + }; + + Ok((results.results, results.metadata)) +} + +pub async fn download(url: String, dir: String) -> Result { + let file_name = Path::new(&url) + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_string(); + let file_path = Path::new(&dir).join(file_name); + + if file_path.exists() { + return Ok(file_path.to_str().unwrap().to_string()); + } + + let mut file = match std::fs::File::create(file_path.clone()) { + Ok(file) => file, + Err(e) => return Err(format!("Failed to create the file: {}", e)), + }; + let client = match reqwest::Client::builder() + .user_agent( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0", + ) + .danger_accept_invalid_certs(true) + .build() + { + Ok(client) => client, + Err(e) => return Err(format!("Failed to create the client: {}", e)), + }; + let res = client.get(&url).send().await; + let response = match res { + Ok(response_ok) => response_ok, + Err(e) => return Err(format!("Failed to download the file: {}", e)), + }; + if response.status() != reqwest::StatusCode::OK { + return Err(format!( + "Failed to download the file: {}", + response.status() + )); + } + + let total_size_res = response + .content_length() + .ok_or("Failed to get the file size"); + let total_size = match total_size_res { + Ok(res_ok) => res_ok, + Err(e) => return Err(format!("Failed to get the file size: {}", e)), + }; + + let mut downloaded: u64 = 0; + let mut stream = response.bytes_stream(); + while let Some(item) = stream.next().await { + let chunk = item.or(Err(format!( + "Error while downloading file: {}", + url.clone() + )))?; + file.write_all(&chunk).or(Err(format!( + "Error while writing to file: {}", + file_path.to_str().unwrap() + )))?; + let new = min(downloaded + (chunk.len() as u64), total_size); + downloaded = new; + } + Ok(file_path.to_str().unwrap().to_string()) +}