Some refactoring
This commit is contained in:
111
src/app.rs
111
src/app.rs
@@ -1,13 +1,11 @@
|
|||||||
use crate::types;
|
use crate::types;
|
||||||
use crate::util::clamp;
|
use crate::util::clamp;
|
||||||
|
use crate::util::download;
|
||||||
|
use crate::util::search_files;
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use egui::ColorImage;
|
use egui::ColorImage;
|
||||||
use futures_util::stream::StreamExt;
|
|
||||||
use human_bytes::human_bytes;
|
use human_bytes::human_bytes;
|
||||||
use image::{ColorType, GenericImage, Rgba};
|
use image::{ColorType, GenericImage, Rgba};
|
||||||
use std::cmp::min;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::path::Path;
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
pub struct Application {
|
pub struct Application {
|
||||||
@@ -368,108 +366,3 @@ impl eframe::App for Application {
|
|||||||
self.render_center_panel(ctx);
|
self.render_center_panel(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn search_files(
|
|
||||||
url: String,
|
|
||||||
query: String,
|
|
||||||
page: usize,
|
|
||||||
page_size: usize,
|
|
||||||
) -> Result<(Vec<types::FileEntry>, 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::<types::Root>().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<String, String> {
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|||||||
112
src/util.rs
112
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 {
|
pub fn clamp(val: usize, min: usize, max: usize) -> usize {
|
||||||
if val < min {
|
if val < min {
|
||||||
min
|
min
|
||||||
@@ -7,3 +14,108 @@ pub fn clamp(val: usize, min: usize, max: usize) -> usize {
|
|||||||
val
|
val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn search_files(
|
||||||
|
url: String,
|
||||||
|
query: String,
|
||||||
|
page: usize,
|
||||||
|
page_size: usize,
|
||||||
|
) -> Result<(Vec<types::FileEntry>, 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::<types::Root>().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<String, String> {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user