Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
refactor(cache): drop dead wrappers and async-unsafe IO
Samir Hossainsa2f2e454d449
diff --git a/src/cache.rs b/src/cache.rsindex cc3ae61..7ba91a7 100644--- a/src/cache.rs+++ b/src/cache.rs@@ -4,6 +4,7 @@ use std::path::PathBuf;const CACHE_EXPIRY_SECS: u64 = 24 * 60 * 60;const STREAM_CACHE_EXPIRY_SECS: u64 = 2 * 60 * 60;+const HOMEPAGE_CACHE_EXPIRY_SECS: u64 = 60 * 60;fn read_json_cache(path: &PathBuf, expiry_secs: u64) -> Option<serde_json::Value> {if path.exists() {@@ -59,10 +60,6 @@ fn stream_payload_has_results(data: &serde_json::Value) -> bool {})}-pub fn get_cache_dir(subdir: &str) -> PathBuf {- get_provider_cache_dir(ProviderKind::MovieBox, subdir)-}-fn hash_key(value: &str) -> String {use md5::{Digest, Md5};let mut hasher = Md5::new();@@ -87,10 +84,6 @@ pub fn get_provider_cache_dir(provider: ProviderKind, subdir: &str) -> PathBuf {path}-pub fn get_cache_path(subject_id: &str, season: usize, episode: usize) -> PathBuf {- get_provider_stream_path(ProviderKind::MovieBox, subject_id, season, episode)-}-pub fn get_provider_stream_path(provider: ProviderKind,subject_id: &str,@@ -108,17 +101,6 @@ pub fn get_provider_stream_path(path}-pub fn get_stream_cache(- subject_id: &str,- season: usize,- episode: usize,-) -> Option<serde_json::Value> {- read_json_cache(- &get_cache_path(subject_id, season, episode),- STREAM_CACHE_EXPIRY_SECS,- )-}-pub fn get_provider_stream_cache(provider: ProviderKind,subject_id: &str,@@ -135,10 +117,6 @@ pub fn get_provider_stream_cache(}}-pub fn get_details_path(subject_id: &str) -> PathBuf {- get_provider_details_path(ProviderKind::MovieBox, subject_id)-}-pub fn get_provider_details_path(provider: ProviderKind, subject_id: &str) -> PathBuf {let mut path = get_provider_cache_dir(provider, "details");let schema = if provider == ProviderKind::FourKHdHub {@@ -151,10 +129,6 @@ pub fn get_provider_details_path(provider: ProviderKind, subject_id: &str) -> Papath}-pub fn get_details_cache(subject_id: &str) -> Option<serde_json::Value> {- read_json_cache(&get_details_path(subject_id), CACHE_EXPIRY_SECS)-}-pub fn get_provider_details_cache(provider: ProviderKind,subject_id: &str,@@ -170,10 +144,6 @@ pub fn invalidate_provider_details_cache(provider: ProviderKind, subject_id: &stlet _ = fs::remove_file(path);}-pub fn get_search_path(query: &str) -> PathBuf {- get_provider_search_path(ProviderKind::MovieBox, query)-}-pub fn get_provider_search_path(provider: ProviderKind, query: &str) -> PathBuf {let mut path = get_provider_cache_dir(provider, "search");let hashed = hash_key(query);@@ -181,10 +151,6 @@ pub fn get_provider_search_path(provider: ProviderKind, query: &str) -> PathBufpath}-pub fn get_search_cache(query: &str) -> Option<serde_json::Value> {- read_json_cache(&get_search_path(query), CACHE_EXPIRY_SECS)-}-pub fn get_provider_search_cache(provider: ProviderKind, query: &str) -> Option<serde_json::Value> {let path = get_provider_search_path(provider, query);let value = read_json_cache(&path, CACHE_EXPIRY_SECS)?;@@ -196,11 +162,6 @@ pub fn get_provider_search_cache(provider: ProviderKind, query: &str) -> Option<}}-pub fn set_search_cache(query: &str, data: &serde_json::Value) {- let path = get_search_path(query);- write_json_cache(&path, data);-}-pub fn set_provider_search_cache(provider: ProviderKind, query: &str, data: &serde_json::Value) {let path = get_provider_search_path(provider, query);if !search_payload_has_results(data) {@@ -219,11 +180,6 @@ fn search_payload_has_results(data: &serde_json::Value) -> bool {.is_some_and(|subjects| !subjects.is_empty())}-pub fn set_details_cache(subject_id: &str, data: &serde_json::Value) {- let path = get_details_path(subject_id);- write_json_cache(&path, data);-}-pub fn set_provider_details_cache(provider: ProviderKind,subject_id: &str,@@ -233,13 +189,6 @@ pub fn set_provider_details_cache(write_json_cache(&path, data);}-pub fn set_stream_cache(subject_id: &str, season: usize, episode: usize, data: &serde_json::Value) {- let path = get_cache_path(subject_id, season, episode);- if stream_payload_has_results(data) {- write_json_cache(&path, data);- }-}-pub fn set_provider_stream_cache(provider: ProviderKind,subject_id: &str,@@ -253,10 +202,6 @@ pub fn set_provider_stream_cache(}}-pub fn invalidate_stream_cache(subject_id: &str, season: usize, episode: usize) {- invalidate_provider_stream_cache(ProviderKind::MovieBox, subject_id, season, episode);-}-pub fn invalidate_provider_stream_cache(provider: ProviderKind,subject_id: &str,@@ -317,7 +262,7 @@ fn get_homepage_path(tab_id: &str, page: usize) -> PathBuf {}pub fn get_homepage_cache(tab_id: &str, page: usize) -> Option<serde_json::Value> {- read_json_cache(&get_homepage_path(tab_id, page), 3600)+ read_json_cache(&get_homepage_path(tab_id, page), HOMEPAGE_CACHE_EXPIRY_SECS)}pub fn set_homepage_cache(tab_id: &str, page: usize, data: &serde_json::Value) {@@ -325,25 +270,13 @@ pub fn set_homepage_cache(tab_id: &str, page: usize, data: &serde_json::Value) {write_json_cache(&path, data);}-pub fn get_image_path(id: &str) -> PathBuf {- get_namespaced_image_path(ProviderKind::MovieBox.cache_key(), id)-}-fn get_namespaced_image_path(namespace: &str, id: &str) -> PathBuf {let mut path = dirs::cache_dir().unwrap_or_else(std::env::temp_dir);path.push("moviebox-tui");path.push(namespace);path.push("images");let _ = fs::create_dir_all(&path);- use md5::{Digest, Md5};- let mut hasher = Md5::new();- hasher.update(id.as_bytes());- let result = hasher.finalize();- let mut safe_name = String::with_capacity(32);- for b in result {- use std::fmt::Write;- let _ = write!(&mut safe_name, "{:02x}", b);- }+ let safe_name = hash_key(id);path.push(format!("{safe_name}.img"));path}diff --git a/src/tui/app.rs b/src/tui/app.rsindex e096eb7..f873d8b 100644--- a/src/tui/app.rs+++ b/src/tui/app.rs@@ -1700,7 +1700,7 @@ impl App {_ => {}},Action::ClearCache => {- crate::cache::clear_all_cache();+ tokio::task::spawn_blocking(crate::cache::clear_all_cache);self.state.set_status("Cache cleared completely.".to_string(), 150);}@@ -3412,7 +3412,15 @@ impl App {let sender = self.action_sender.clone();let link_clone = link.clone();tokio::spawn(async move {- if let Some(res) = crate::cache::get_captions_cache(&subject_id, &rid) {+ let cached = tokio::task::spawn_blocking({+ let subject_id = subject_id.clone();+ let rid = rid.clone();+ move || crate::cache::get_captions_cache(&subject_id, &rid)+ })+ .await+ .ok()+ .flatten();+ if let Some(res) = cached {sender.send(Action::ShowSubtitlePopup(link_clone.clone(),@@ -3429,7 +3437,16 @@ impl App {.await;match result {Ok(Ok(res)) => {- crate::cache::set_captions_cache(&subject_id, &rid, &res);+ let subject_id = subject_id.clone();+ let rid = rid.clone();+ let res_for_cache = res.clone();+ tokio::task::spawn_blocking(move || {+ crate::cache::set_captions_cache(+ &subject_id,+ &rid,+ &res_for_cache,+ );+ });sender.send(Action::ShowSubtitlePopup(link_clone, res, open_with)).ok();@@ -4240,36 +4257,6 @@ impl App {let is_movie = season == 0 && episode == 0;tokio::spawn(async move {- if !force_refresh {- let id_for_cache = id_clone.clone();- if let Ok(Some(cached)) = tokio::task::spawn_blocking(move || {- crate::cache::get_provider_stream_cache(- context.provider,- &id_for_cache,- season,- episode,- )- })- .await- {- tokio::time::sleep(std::time::Duration::from_millis(120)).await;- sender- .send(Action::SetStatus("Loaded from cache.".to_string()))- .ok();- sender- .send(Action::EpisodeStreamsReady(- context,- request_id,- subject_id.clone(),- season,- episode,- cached,- ))- .ok();- return;- }- }-sender.send(Action::SetStatus("Fetching streams...".to_string())).ok();