Search
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
6021
Cargo.lock
generated
Normal file
6021
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
Cargo.toml
Normal file
26
Cargo.toml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
[package]
|
||||||
|
name = "file-srv-gui-v3"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
eframe = "0.33.3"
|
||||||
|
futures-util = "0.3.32"
|
||||||
|
human_bytes = "0.4.3"
|
||||||
|
image = { version = "0.25.10", features = ["png", "jpeg"] }
|
||||||
|
reqwest = { version = "0.13.2", features = ["stream", "json"] }
|
||||||
|
rfd = "0.17.2"
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros"] }
|
||||||
|
egui_extras = { version = "0.33.3", features = ["all_loaders", "gif", "http"] }
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
panic = 'abort'
|
||||||
|
opt-level = 3
|
||||||
|
overflow-checks = false
|
||||||
|
debug-assertions = false
|
||||||
|
incremental = false
|
||||||
|
rpath = false
|
||||||
|
codegen-units = 1
|
||||||
|
strip = true
|
||||||
153
src/app.rs
Normal file
153
src/app.rs
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
use eframe::egui;
|
||||||
|
use crate::types;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
pub struct Application {
|
||||||
|
download_path: Option<String>,
|
||||||
|
server_url: String,
|
||||||
|
query: String,
|
||||||
|
status: String,
|
||||||
|
search_ctx: types::SearchContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Application {
|
||||||
|
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
||||||
|
cc.egui_ctx.set_visuals(egui::Visuals::dark());
|
||||||
|
egui_extras::install_image_loaders(&cc.egui_ctx);
|
||||||
|
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_channels(&mut self, ctx: &egui::Context) {
|
||||||
|
if self.search_ctx.search_rx.is_some() {
|
||||||
|
let mut clear_rx = false;
|
||||||
|
if let Some(rx) = self.search_ctx.search_rx.as_mut() {
|
||||||
|
match rx.try_recv() {
|
||||||
|
Ok(Ok(results)) => {
|
||||||
|
// Ok recv, ok results
|
||||||
|
self.search_ctx.search_results = results.0.clone();
|
||||||
|
self.status = "Ready!".to_string();
|
||||||
|
self.search_ctx.total_pages = results.1.total_pages;
|
||||||
|
self.search_ctx.page = results.1.page;
|
||||||
|
self.search_ctx.per_page = results.1.page_size;
|
||||||
|
self.search_ctx.is_searching = false;
|
||||||
|
clear_rx = true;
|
||||||
|
},
|
||||||
|
Ok(Err(e)) => {
|
||||||
|
// Ok recv, err results
|
||||||
|
self.status = format!("Search failed: {}", e);
|
||||||
|
self.search_ctx.is_searching = false;
|
||||||
|
clear_rx = true;
|
||||||
|
},
|
||||||
|
Err(mpsc::error::TryRecvError::Empty) => {
|
||||||
|
ctx.request_repaint();
|
||||||
|
},
|
||||||
|
Err(mpsc::error::TryRecvError::Disconnected) => {
|
||||||
|
self.status = "Search thread ended unexpectedly".to_string();
|
||||||
|
self.search_ctx.is_searching = false;
|
||||||
|
clear_rx = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if clear_rx { self.search_ctx.search_rx = None; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Application {
|
||||||
|
fn default() -> Self {
|
||||||
|
Application {
|
||||||
|
download_path: None,
|
||||||
|
server_url: "https://stuff.catgirls.fish/search".to_string(),
|
||||||
|
query: "".to_string(),
|
||||||
|
status: "Ready!".to_string(),
|
||||||
|
search_ctx: types::SearchContext::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for Application {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||||
|
|
||||||
|
self.process_channels(ctx);
|
||||||
|
|
||||||
|
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
|
||||||
|
// Search button and input
|
||||||
|
ui.label("Search: ");
|
||||||
|
if ui.text_edit_singleline(&mut self.query).lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
|
||||||
|
|
||||||
|
let query = self.query.clone();
|
||||||
|
let url = self.server_url.clone();
|
||||||
|
let page = self.search_ctx.page.clone();
|
||||||
|
let page_size = self.search_ctx.per_page.clone();
|
||||||
|
let (tx, rx) = mpsc::channel::<Result<(Vec<types::FileEntry>, types::Metadata), String>>(1);
|
||||||
|
|
||||||
|
self.search_ctx.search_rx = Some(rx);
|
||||||
|
self.search_ctx.is_searching = true;
|
||||||
|
self.search_ctx.search_results.clear();
|
||||||
|
self.search_ctx.page = 0;
|
||||||
|
self.search_ctx.total_pages = 0;
|
||||||
|
self.search_ctx.total_results = 0;
|
||||||
|
self.status = "Searching...".to_string();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let res = search_files(url, query, page, page_size).await;
|
||||||
|
let _ = tx.send(res).await;
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
ui.label(&self.status);
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.heading("Hello world");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
15
src/main.rs
Normal file
15
src/main.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
|
mod app;
|
||||||
|
mod types;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
let options = eframe::NativeOptions::default();
|
||||||
|
eframe::run_native(
|
||||||
|
"File-serve GUI v3",
|
||||||
|
options,
|
||||||
|
Box::new(|cc| Ok(Box::new(app::Application::new(&cc)))),
|
||||||
|
)
|
||||||
|
.expect("Failed to start application");
|
||||||
|
}
|
||||||
50
src/types.rs
Normal file
50
src/types.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct Root {
|
||||||
|
pub results: Vec<FileEntry>,
|
||||||
|
pub metadata: Metadata,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct FileEntry {
|
||||||
|
pub name: String,
|
||||||
|
pub ext: String,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub path: String,
|
||||||
|
pub url: String,
|
||||||
|
pub size: i64,
|
||||||
|
pub preview: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct Metadata {
|
||||||
|
pub page: usize,
|
||||||
|
pub total_pages: usize,
|
||||||
|
pub page_size: usize
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SearchContext {
|
||||||
|
pub is_searching: bool,
|
||||||
|
pub search_rx: Option<mpsc::Receiver<Result<(Vec<FileEntry>, Metadata), String>>>,
|
||||||
|
pub search_results: Vec<FileEntry>,
|
||||||
|
pub page: usize,
|
||||||
|
pub per_page : usize,
|
||||||
|
pub total_pages: usize,
|
||||||
|
pub total_results: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for SearchContext {
|
||||||
|
fn default() -> Self {
|
||||||
|
SearchContext {
|
||||||
|
is_searching: false,
|
||||||
|
search_rx: None,
|
||||||
|
search_results: vec![],
|
||||||
|
page: 1,
|
||||||
|
per_page: 25,
|
||||||
|
total_pages: 0,
|
||||||
|
total_results: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user