1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use tracing::warn;
use zerolaunch_plugin_api::services::shell::LnkResolver;
/// Windows 平台 Lnk 快捷方式解析器实现。
/// 使用 lnk crate 解析 .lnk 快捷方式文件,优先使用 GB18030 编码,失败后回退 UTF-16LE。
pub struct WindowsLnkResolver;
impl Default for WindowsLnkResolver {
fn default() -> Self {
Self::new()
}
}
impl WindowsLnkResolver {
pub fn new() -> Self {
Self
}
}
impl LnkResolver for WindowsLnkResolver {
/// 解析 .lnk 快捷方式文件的目标路径。
/// 优先使用 GB18030 编码打开,失败后回退 UTF-16LE 编码。
/// 参数:lnk_path - .lnk 文件的路径。
/// 返回:解析成功返回目标路径,失败返回 None。
fn resolve_lnk_target(&self, lnk_path: &str) -> Option<String> {
let shell_link_result = lnk::ShellLink::open(lnk_path, encoding_rs::GB18030);
let shell_link = match shell_link_result {
Ok(link) => link,
Err(e_gb18030) => {
warn!(
"Failed to open LNK file '{}' with GB18030 encoding: {:?}",
lnk_path, e_gb18030
);
match lnk::ShellLink::open(lnk_path, encoding_rs::UTF_16LE) {
Ok(link) => {
warn!(
"在主要编码尝试失败后,成功使用 UTF-16LE 编码打开 LNK 文件: {}",
lnk_path
);
link
}
Err(e_utf16) => {
warn!(
"尝试使用 UTF-16LE 编码打开 LNK 文件 '{}' 再次失败: {:?}",
lnk_path, e_utf16
);
return None;
}
}
}
};
let link_info = match shell_link.link_info() {
Some(info) => info,
None => {
warn!("无法从 LNK 文件 '{}' 获取 link_info。", lnk_path);
return None;
}
};
match link_info.local_base_path() {
Some(path) => Some(path.to_string()),
None => {
warn!(
"无法从 LNK 文件 '{}' 获取基本路径 (local_base_path)。",
lnk_path
);
None
}
}
}
}