瀏覽代碼

feat: Use url path name as fallback subscription name (#255)

Lai Zn 1 年之前
父節點
當前提交
0ae96918e9
共有 2 個文件被更改,包括 16 次插入1 次删除
  1. 3 1
      src-tauri/src/config/prfitem.rs
  2. 13 0
      src-tauri/src/utils/help.rs

+ 3 - 1
src-tauri/src/config/prfitem.rs

@@ -261,7 +261,9 @@ impl PrfItem {
                     },
                 }
             }
-            None => None,
+            None => Some(
+                crate::utils::help::get_last_part_and_decode(url).unwrap_or("Remote File".into()),
+            ),
         };
         let option = match update_interval {
             Some(val) => Some(PrfOption {

+ 13 - 0
src-tauri/src/utils/help.rs

@@ -80,6 +80,19 @@ pub fn parse_str<T: FromStr>(target: &str, key: &str) -> Option<T> {
     })
 }
 
+/// get the last part of the url, if not found, return empty string
+pub fn get_last_part_and_decode(url: &str) -> Option<String> {
+    let path = url.split('?').next().unwrap_or(""); // Splits URL and takes the path part
+    let segments: Vec<&str> = path.split('/').collect();
+    let last_segment = segments.last()?;
+
+    Some(
+        percent_encoding::percent_decode_str(last_segment)
+            .decode_utf8_lossy()
+            .to_string(),
+    )
+}
+
 /// open file
 /// use vscode by default
 pub fn open_file(app: tauri::AppHandle, path: PathBuf) -> Result<()> {