op1
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use base64::Engine;
|
||||
use std::fs;
|
||||
use std::process::Command;
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::collections::HashMap;
|
||||
use tauri::AppHandle;
|
||||
use std::path::PathBuf;
|
||||
use tauri::{AppHandle, Manager};
|
||||
use crate::emit_log;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
@@ -176,84 +179,18 @@ pub fn list_updates(handle: &AppHandle) -> Vec<Software> {
|
||||
let script = r#"
|
||||
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$ErrorActionPreference = 'SilentlyContinue'
|
||||
Add-Type -AssemblyName System.Drawing
|
||||
Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue
|
||||
|
||||
$pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue | Where-Object { $_.IsUpdateAvailable }
|
||||
|
||||
if ($pkgs) {
|
||||
$wshShell = New-Object -ComObject WScript.Shell
|
||||
|
||||
# 预加载开始菜单快捷方式
|
||||
$startMenuPaths = @(
|
||||
"$env:ProgramData\Microsoft\Windows\Start Menu\Programs",
|
||||
"$env:AppData\Microsoft\Windows\Start Menu\Programs"
|
||||
)
|
||||
$lnkFiles = Get-ChildItem -Path $startMenuPaths -Filter "*.lnk" -Recurse -File
|
||||
|
||||
# 预加载注册表项
|
||||
$registryPaths = @(
|
||||
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
|
||||
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
|
||||
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
|
||||
)
|
||||
$regItems = Get-ItemProperty $registryPaths
|
||||
|
||||
$pkgs | ForEach-Object {
|
||||
$p = $_
|
||||
$iconUrl = $null
|
||||
$foundPath = ""
|
||||
|
||||
# 策略 1: 寻找并解析开始菜单快捷方式 (去除箭头关键点)
|
||||
$matchedLnk = $lnkFiles | Where-Object { $_.BaseName -eq $p.Name -or $p.Name -like "*$($_.BaseName)*" } | Select-Object -First 1
|
||||
if ($matchedLnk) {
|
||||
try {
|
||||
# 解析快捷方式指向的真实目标
|
||||
$target = $wshShell.CreateShortcut($matchedLnk.FullName).TargetPath
|
||||
if ($target -and (Test-Path $target) -and $target.EndsWith(".exe")) {
|
||||
$foundPath = $target
|
||||
} else {
|
||||
# 如果目标不可读或是 UWP 快捷方式,仍使用快捷方式文件提取
|
||||
$foundPath = $matchedLnk.FullName
|
||||
}
|
||||
} catch {
|
||||
$foundPath = $matchedLnk.FullName
|
||||
}
|
||||
}
|
||||
|
||||
# 策略 2: 注册表 DisplayIcon
|
||||
if (-not $foundPath) {
|
||||
$matchedReg = $regItems | Where-Object { $_.DisplayName -eq $p.Name -or $_.PSChildName -eq $p.Id } | Select-Object -First 1
|
||||
if ($matchedReg.DisplayIcon) {
|
||||
$foundPath = $matchedReg.DisplayIcon.Split(',')[0].Trim('"')
|
||||
} elseif ($matchedReg.InstallLocation) {
|
||||
$loc = $matchedReg.InstallLocation.Trim('"')
|
||||
if (Test-Path $loc) {
|
||||
$exe = Get-ChildItem -Path $loc -Filter "*.exe" -File | Select-Object -First 1
|
||||
if ($exe) { $foundPath = $exe.FullName }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 提取并转 Base64
|
||||
if ($foundPath -and (Test-Path $foundPath)) {
|
||||
try {
|
||||
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($foundPath)
|
||||
$bitmap = $icon.ToBitmap()
|
||||
$ms = New-Object System.IO.MemoryStream
|
||||
$bitmap.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
|
||||
$base64 = [Convert]::ToBase64String($ms.ToArray())
|
||||
$iconUrl = "data:image/png;base64,$base64"
|
||||
$ms.Dispose(); $bitmap.Dispose(); $icon.Dispose()
|
||||
} catch {}
|
||||
}
|
||||
|
||||
[PSCustomObject]@{
|
||||
Name = [string]$p.Name;
|
||||
Id = [string]$p.Id;
|
||||
InstalledVersion = [string]$p.InstalledVersion;
|
||||
AvailableVersions = $p.AvailableVersions;
|
||||
IconUrl = $iconUrl
|
||||
Name = [string]$_.Name;
|
||||
Id = [string]$_.Id;
|
||||
InstalledVersion = [string]$_.InstalledVersion;
|
||||
AvailableVersions = $_.AvailableVersions;
|
||||
IconUrl = $null
|
||||
}
|
||||
} | ConvertTo-Json -Compress
|
||||
} else {
|
||||
@@ -285,6 +222,111 @@ pub fn get_software_info(handle: &AppHandle, id: &str) -> Option<Software> {
|
||||
res.into_iter().next()
|
||||
}
|
||||
|
||||
pub fn get_cached_or_extract_icon(handle: &AppHandle, id: &str, name: &str) -> Option<String> {
|
||||
let cache_key = sanitize_cache_key(id);
|
||||
let icon_dir = get_icon_cache_dir(handle);
|
||||
let icon_path = icon_dir.join(format!("{}.png", cache_key));
|
||||
|
||||
if let Ok(bytes) = fs::read(&icon_path) {
|
||||
return Some(format!(
|
||||
"data:image/png;base64,{}",
|
||||
base64::engine::general_purpose::STANDARD.encode(bytes)
|
||||
));
|
||||
}
|
||||
|
||||
let log_id = format!("icon-{}", cache_key);
|
||||
emit_log(handle, &log_id, "Icon Lookup", &format!("Resolving icon for {}...", id), "info");
|
||||
|
||||
let script = format!(r#"
|
||||
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$ErrorActionPreference = 'SilentlyContinue'
|
||||
Add-Type -AssemblyName System.Drawing
|
||||
Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue
|
||||
|
||||
$packageId = @'
|
||||
{id}
|
||||
'@
|
||||
$packageName = @'
|
||||
{name}
|
||||
'@
|
||||
|
||||
$foundPath = ""
|
||||
$wshShell = New-Object -ComObject WScript.Shell
|
||||
$startMenuPaths = @(
|
||||
"$env:ProgramData\Microsoft\Windows\Start Menu\Programs",
|
||||
"$env:AppData\Microsoft\Windows\Start Menu\Programs"
|
||||
)
|
||||
|
||||
$lnkFiles = Get-ChildItem -Path $startMenuPaths -Filter "*.lnk" -Recurse -File
|
||||
$matchedLnk = $lnkFiles | Where-Object {{ $_.BaseName -eq $packageName -or $packageName -like "*$($_.BaseName)*" }} | Select-Object -First 1
|
||||
if ($matchedLnk) {{
|
||||
try {{
|
||||
$target = $wshShell.CreateShortcut($matchedLnk.FullName).TargetPath
|
||||
if ($target -and (Test-Path $target) -and $target.EndsWith(".exe")) {{
|
||||
$foundPath = $target
|
||||
}} else {{
|
||||
$foundPath = $matchedLnk.FullName
|
||||
}}
|
||||
}} catch {{
|
||||
$foundPath = $matchedLnk.FullName
|
||||
}}
|
||||
}}
|
||||
|
||||
if (-not $foundPath) {{
|
||||
$registryPaths = @(
|
||||
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
|
||||
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
|
||||
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
|
||||
)
|
||||
$regItems = Get-ItemProperty $registryPaths
|
||||
$matchedReg = $regItems | Where-Object {{ $_.DisplayName -eq $packageName -or $_.PSChildName -eq $packageId }} | Select-Object -First 1
|
||||
if ($matchedReg.DisplayIcon) {{
|
||||
$foundPath = $matchedReg.DisplayIcon.Split(',')[0].Trim('"')
|
||||
}} elseif ($matchedReg.InstallLocation) {{
|
||||
$loc = $matchedReg.InstallLocation.Trim('"')
|
||||
if (Test-Path $loc) {{
|
||||
$exe = Get-ChildItem -Path $loc -Filter "*.exe" -File | Select-Object -First 1
|
||||
if ($exe) {{ $foundPath = $exe.FullName }}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
|
||||
if ($foundPath -and (Test-Path $foundPath)) {{
|
||||
try {{
|
||||
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($foundPath)
|
||||
if ($icon) {{
|
||||
$bitmap = $icon.ToBitmap()
|
||||
$ms = New-Object System.IO.MemoryStream
|
||||
$bitmap.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
|
||||
[Convert]::ToBase64String($ms.ToArray())
|
||||
$ms.Dispose()
|
||||
$bitmap.Dispose()
|
||||
$icon.Dispose()
|
||||
}}
|
||||
}} catch {{}}
|
||||
}}
|
||||
"#, id = id, name = name);
|
||||
|
||||
let output = Command::new("powershell")
|
||||
.args(["-NoProfile", "-Command", &script])
|
||||
.creation_flags(0x08000000)
|
||||
.output()
|
||||
.ok()?;
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let encoded = stdout.trim_start_matches('\u{feff}').trim();
|
||||
if encoded.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let bytes = base64::engine::general_purpose::STANDARD.decode(encoded).ok()?;
|
||||
if fs::create_dir_all(&icon_dir).is_err() {
|
||||
return Some(format!("data:image/png;base64,{}", encoded));
|
||||
}
|
||||
let _ = fs::write(&icon_path, &bytes);
|
||||
Some(format!("data:image/png;base64,{}", encoded))
|
||||
}
|
||||
|
||||
fn execute_powershell(handle: &AppHandle, log_id: &str, cmd_title: &str, script: &str) -> Vec<Software> {
|
||||
emit_log(handle, log_id, cmd_title, "Fetching data from Winget...", "info");
|
||||
|
||||
@@ -329,6 +371,17 @@ fn parse_json_output(json_str: String) -> Vec<Software> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn get_icon_cache_dir(handle: &AppHandle) -> PathBuf {
|
||||
let app_data_dir = handle.path().app_data_dir().unwrap_or_default();
|
||||
app_data_dir.join("icons")
|
||||
}
|
||||
|
||||
fn sanitize_cache_key(id: &str) -> String {
|
||||
id.chars()
|
||||
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn map_package(p: WingetPackage) -> Software {
|
||||
Software {
|
||||
id: p.id,
|
||||
|
||||
Reference in New Issue
Block a user