refactor: moved get_short_path to utils and use it in decompress
This commit is contained in:
parent
6067ffe107
commit
e2e572ccab
2
src-tauri/Cargo.lock
generated
2
src-tauri/Cargo.lock
generated
@ -2325,6 +2325,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"url",
|
"url",
|
||||||
|
"windows-sys 0.60.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -5188,7 +5189,6 @@ dependencies = [
|
|||||||
"tauri-plugin",
|
"tauri-plugin",
|
||||||
"thiserror 2.0.12",
|
"thiserror 2.0.12",
|
||||||
"tokio",
|
"tokio",
|
||||||
"windows-sys 0.60.2",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -25,10 +25,6 @@ thiserror = "2.0.12"
|
|||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
reqwest = { version = "0.11", features = ["json", "blocking", "stream"] }
|
reqwest = { version = "0.11", features = ["json", "blocking", "stream"] }
|
||||||
|
|
||||||
# Windows-specific dependencies
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
|
||||||
windows-sys = { version = "0.60.2", features = ["Win32_Storage_FileSystem"] }
|
|
||||||
|
|
||||||
# Unix-specific dependencies
|
# Unix-specific dependencies
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
nix = { version = "=0.30.1", features = ["signal", "process"] }
|
nix = { version = "=0.30.1", features = ["signal", "process"] }
|
||||||
|
|||||||
@ -3,31 +3,7 @@ use std::path::PathBuf;
|
|||||||
use crate::error::{ErrorCode, LlamacppError, ServerResult};
|
use crate::error::{ErrorCode, LlamacppError, ServerResult};
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use std::os::windows::ffi::OsStrExt;
|
use jan_utils::path::get_short_path;
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
use std::ffi::OsStr;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
use windows_sys::Win32::Storage::FileSystem::GetShortPathNameW;
|
|
||||||
|
|
||||||
/// Get Windows short path to avoid issues with spaces and special characters
|
|
||||||
#[cfg(windows)]
|
|
||||||
pub fn get_short_path<P: AsRef<std::path::Path>>(path: P) -> Option<String> {
|
|
||||||
let wide: Vec<u16> = OsStr::new(path.as_ref())
|
|
||||||
.encode_wide()
|
|
||||||
.chain(Some(0))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut buffer = vec![0u16; 260];
|
|
||||||
let len = unsafe { GetShortPathNameW(wide.as_ptr(), buffer.as_mut_ptr(), buffer.len() as u32) };
|
|
||||||
|
|
||||||
if len > 0 {
|
|
||||||
Some(String::from_utf16_lossy(&buffer[..len as usize]))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Validate that a binary path exists and is accessible
|
/// Validate that a binary path exists and is accessible
|
||||||
pub fn validate_binary_path(backend_path: &str) -> ServerResult<PathBuf> {
|
pub fn validate_binary_path(backend_path: &str) -> ServerResult<PathBuf> {
|
||||||
@ -259,18 +235,6 @@ mod tests {
|
|||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
#[test]
|
|
||||||
fn test_get_short_path() {
|
|
||||||
// Test with a real path that should exist on Windows
|
|
||||||
use std::env;
|
|
||||||
if let Ok(temp_dir) = env::var("TEMP") {
|
|
||||||
let result = get_short_path(&temp_dir);
|
|
||||||
// Should return some short path or None (both are valid)
|
|
||||||
// We can't assert the exact value as it depends on the system
|
|
||||||
println!("Short path result: {:?}", result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_validate_model_path_multiple_m_flags() {
|
fn test_validate_model_path_multiple_m_flags() {
|
||||||
|
|||||||
@ -184,6 +184,17 @@ pub fn decompress(app: tauri::AppHandle, path: &str, output_dir: &str) -> Result
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
// Use short path on Windows to handle paths with spaces
|
||||||
|
#[cfg(windows)]
|
||||||
|
let file = {
|
||||||
|
if let Some(short_path) = jan_utils::path::get_short_path(&path_buf) {
|
||||||
|
fs::File::open(&short_path).map_err(|e| e.to_string())?
|
||||||
|
} else {
|
||||||
|
fs::File::open(&path_buf).map_err(|e| e.to_string())?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
let file = fs::File::open(&path_buf).map_err(|e| e.to_string())?;
|
let file = fs::File::open(&path_buf).map_err(|e| e.to_string())?;
|
||||||
if path.ends_with(".tar.gz") {
|
if path.ends_with(".tar.gz") {
|
||||||
let tar = flate2::read::GzDecoder::new(file);
|
let tar = flate2::read::GzDecoder::new(file);
|
||||||
|
|||||||
@ -16,6 +16,9 @@ tokio = { version = "1", features = ["process", "fs", "macros", "rt"] }
|
|||||||
tokio-util = "0.7.14"
|
tokio-util = "0.7.14"
|
||||||
url = "2.5"
|
url = "2.5"
|
||||||
|
|
||||||
|
[target.'cfg(windows)'.dependencies]
|
||||||
|
windows-sys = { version = "0.60.2", features = ["Win32_Storage_FileSystem"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.0"
|
tempfile = "3.0"
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,15 @@
|
|||||||
use std::path::Prefix;
|
use std::path::Prefix;
|
||||||
use std::path::{Component, Path, PathBuf};
|
use std::path::{Component, Path, PathBuf};
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
use std::os::windows::ffi::OsStrExt;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
use windows_sys::Win32::Storage::FileSystem::GetShortPathNameW;
|
||||||
|
|
||||||
/// Normalizes file paths by handling path components, prefixes, and resolving relative paths
|
/// Normalizes file paths by handling path components, prefixes, and resolving relative paths
|
||||||
/// Based on: https://github.com/rust-lang/cargo/blob/rust-1.67.0/crates/cargo-util/src/paths.rs#L82-L107
|
/// Based on: https://github.com/rust-lang/cargo/blob/rust-1.67.0/crates/cargo-util/src/paths.rs#L82-L107
|
||||||
pub fn normalize_path(path: &Path) -> PathBuf {
|
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||||
@ -74,3 +83,39 @@ pub fn remove_prefix(path: &str, prefix: &str) -> String {
|
|||||||
path.to_string()
|
path.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get Windows short path to avoid issues with spaces and special characters
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub fn get_short_path<P: AsRef<std::path::Path>>(path: P) -> Option<String> {
|
||||||
|
let wide: Vec<u16> = OsStr::new(path.as_ref())
|
||||||
|
.encode_wide()
|
||||||
|
.chain(Some(0))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut buffer = vec![0u16; 260];
|
||||||
|
let len = unsafe { GetShortPathNameW(wide.as_ptr(), buffer.as_mut_ptr(), buffer.len() as u32) };
|
||||||
|
|
||||||
|
if len > 0 {
|
||||||
|
Some(String::from_utf16_lossy(&buffer[..len as usize]))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
#[test]
|
||||||
|
fn test_get_short_path() {
|
||||||
|
// Test with a real path that should exist on Windows
|
||||||
|
use std::env;
|
||||||
|
if let Ok(temp_dir) = env::var("TEMP") {
|
||||||
|
let result = get_short_path(&temp_dir);
|
||||||
|
// Should return some short path or None (both are valid)
|
||||||
|
// We can't assert the exact value as it depends on the system
|
||||||
|
println!("Short path result: {:?}", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user