Kaynağa Gözat

fix caching

biblius 9 ay önce
ebeveyn
işleme
48f16039ed
3 değiştirilmiş dosya ile 29 ekleme ve 11 silme
  1. 2 2
      deploy.sh
  2. 11 5
      src/main.rs
  3. 16 4
      src/routes.rs

+ 2 - 2
deploy.sh

@@ -1,4 +1,4 @@
 cargo build --release --target=aarch64-unknown-linux-gnu
 
-scp -r assets root@192.168.0.69:/opt/ntitled
-scp target/aarch64-unknown-linux-gnu/release/ntitled root@192.168.0.69:/opt/ntitled
+scp -r assets root@192.168.0.169:/opt/ntitled/assets
+scp target/aarch64-unknown-linux-gnu/release/ntitled root@192.168.0.169:/opt/ntitled

+ 11 - 5
src/main.rs

@@ -22,11 +22,12 @@ use tracing::{debug, info, warn, Level};
 const SKIP_EXT: &[&str] = &["srt", "smi", "mjpeg", "jpg", "jpeg"];
 
 fn skip_ext(ext: &str) -> bool {
-    let mut skip = false;
     for e in SKIP_EXT {
-        skip = ext.ends_with(e);
+        if ext.ends_with(e) {
+            return true;
+        }
     }
-    skip
+    false
 }
 
 use crate::ffmpeg::{FFProbeOutput, SubtitleStream};
@@ -49,7 +50,7 @@ pub struct State {
 fn extract_name_ext(entry: &DirEntry) -> Option<(String, String)> {
     let path = entry.path();
     let ext = path.extension()?.to_str()?;
-    let (name, _) = path.file_name()?.to_str()?.split_once(ext)?;
+    let (name, _) = path.file_name()?.to_str()?.split_once(&format!(".{ext}"))?;
     Some((name.to_owned(), ext.to_owned()))
 }
 
@@ -86,9 +87,14 @@ impl State {
         let mut cached_file_names = vec![];
 
         for entry in entries.iter() {
-            let Some((name, _)) = extract_name_ext(entry) else {
+            let Some((name, ext)) = extract_name_ext(entry) else {
                 continue;
             };
+
+            if skip_ext(&ext) {
+                continue;
+            }
+
             let file = self.cache.get_file_meta(&name)?;
             if let Some(file) = file {
                 debug!("Found cached file {}", file.name);

+ 16 - 4
src/routes.rs

@@ -1,4 +1,5 @@
 use crate::{
+    cache::Cache,
     error::NtitledError,
     htmx::{DirHtmx, FileHtmx, SubtitleResponseHtmx},
     ost::{data::SubtitleResponse, hash::compute_hash},
@@ -29,7 +30,7 @@ pub async fn get_directory(
 
     info!("Listing {path}");
 
-    let files = state.0.scan_dir_contents(path)?;
+    let files = state.scan_dir_contents(path)?;
 
     let response = files
         .into_iter()
@@ -109,7 +110,7 @@ pub struct DownloadQuery {
 }
 
 pub async fn download_subtitles(
-    state: axum::extract::State<State>,
+    mut state: axum::extract::State<State>,
     query: axum::extract::Query<DownloadQuery>,
 ) -> Result<String, NtitledError> {
     let file_id = urlencoding::decode(&query.file_id)?.parse()?;
@@ -122,10 +123,21 @@ pub async fn download_subtitles(
 
     let ext = Path::new(&full_path).extension().and_then(OsStr::to_str);
 
-    let path = ext
+    let srt_path = ext
         .map(|ext| query.full_path.replace(&format!(".{ext}"), ".srt"))
         .unwrap_or(format!("{full_path}.srt"));
 
-    state.client.download_subtitles(file_id, &path).await?;
+    state.client.download_subtitles(file_id, &srt_path).await?;
+
+    if let Some(name) = file {
+        let existing_meta = state
+            .cache
+            .get_file_meta(&name.replace(&format!(".{}", ext.unwrap_or_default()), ""))?;
+        if let Some(mut meta) = existing_meta {
+            meta.has_subs = true;
+            state.cache.set_file_meta(&meta)?;
+        }
+    }
+
     Ok(String::from("Successfully downloaded subtitles"))
 }