Skip to content

Commit

Permalink
跨平台
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualHotBar committed Apr 19, 2024
1 parent af85c78 commit 6e81939
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 94 deletions.
81 changes: 29 additions & 52 deletions src-tauri/src/autostart.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

extern crate winreg;
use std::io;
use winreg::enums::*;
use winreg::RegKey;

pub fn set_autostart(enabled: bool) -> io::Result<()> {
let os = env::consts::OS;
match os {
"windows" => set_autostart_windows(enabled),
"linux" => set_autostart_linux(enabled),
_ => Err(io::Error::new(
io::ErrorKind::Other,
"Unsupported operating system",
)),
}
}

//is_autostart
pub fn is_autostart() -> io::Result<bool> {
let os = env::consts::OS;
match os {
"windows" => Ok(is_startup_key_set("NetMount")?),
"linux" => Ok(service_file_exists("netmount")),
_ => Err(io::Error::new(
io::ErrorKind::Other,
"Unsupported operating system",
)),
}
}

use std::process::Command;
#[cfg(target_os = "windows")]
fn set_autostart_windows(enabled: bool) -> io::Result<()> {
pub fn set_autostart(enabled: bool) -> io::Result<()> {
use std::process::Command;
let exe_path = env::current_exe()?;
let exe_path_str = exe_path.to_string_lossy().into_owned();

Expand Down Expand Up @@ -67,7 +35,30 @@ fn set_autostart_windows(enabled: bool) -> io::Result<()> {
}
}

fn set_autostart_linux(enabled: bool) -> io::Result<()> {
#[cfg(target_os = "windows")]
pub fn is_autostart() -> io::Result<bool> {
extern crate winreg;
use winreg::RegKey;
use winreg::enums::*;

let app_name = "NetMount";
let exe_path = env::current_exe()?;
let exe_path_str = exe_path.to_string_lossy().into_owned();
// 打开注册表的“Run”键
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let run_key = hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")?;

// 尝试获取传入的app_name的值
match run_key.get_value::<String, _>(app_name) {
Ok(path) => Ok(path == format!("\"{}\"", exe_path_str)), // 如果成功获取值,返回true
Err(_) => Ok(false), // 如果获取失败,返回false
}
}

#[cfg(target_os = "linux")]
pub fn is_autostart(enabled: bool) -> io::Result<()> {
use std::fs::File;
use std::io::prelude::*;
let exe_path = env::current_exe()?;
let exe_path_str = exe_path
.to_str()
Expand Down Expand Up @@ -117,23 +108,9 @@ WantedBy=multi-user.target
}
}

#[cfg(target_os = "windows")]
fn is_startup_key_set(app_name: &str) -> io::Result<bool> {
let exe_path = env::current_exe()?;
let exe_path_str = exe_path.to_string_lossy().into_owned();
// 打开注册表的“Run”键
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let run_key = hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")?;

// 尝试获取传入的app_name的值
match run_key.get_value::<String, _>(app_name) {
Ok(path) => Ok(path==format!("\"{}\"", exe_path_str)), // 如果成功获取值,返回true
Err(_) => Ok(false), // 如果获取失败,返回false
}
}

fn service_file_exists(service_name: &str) -> bool {
#[cfg(target_os = "linux")]
pub fn is_autostart(service_name: &str) -> bool {
use std::path::Path;
let file_path = format!("/etc/systemd/system/{}.service", service_name);
Path::new(&file_path).exists()
}

31 changes: 21 additions & 10 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,36 @@ use std::sync::Mutex;
// 从语言包中获取指定键的翻译文本

fn main() {
// 确保应用程序只有一个实例运行
ensure_single_instance();

tauri::Builder::default()
// 根据不同的操作系统配置Tauri Builder
let builder = tauri::Builder::default()
.setup(|app| {
set_window_shadow(app);
set_window_shadow(app); // 设置窗口阴影
Ok(())
})
.system_tray(tray::menu())
.on_system_tray_event(tray::handler)
.system_tray(tray::menu()) // 设置系统托盘菜单
.on_system_tray_event(tray::handler) // 设置系统托盘事件处理器
.invoke_handler(tauri::generate_handler![
// 注册可以从前端调用的Rust函数
read_config_file,
write_config_file,
download_file,
get_available_drive_letter,
get_autostart_state,
set_autostart_state,
get_winfsp_install_state,
set_localized
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
set_localized,
]);

// 针对Windows系统额外注册函数
#[cfg(target_os = "windows")]
let builder = builder.invoke_handler(tauri::generate_handler![
get_winfsp_install_state,
get_available_drive_letter
]);

// 运行Tauri应用,使用`generate_context!()`来加载应用配置
builder.run(tauri::generate_context!()).expect("error while running tauri application");
}

use once_cell::sync::Lazy;
Expand Down Expand Up @@ -110,6 +119,7 @@ fn run_command(cmd: &str) -> Result<std::process::Child, Box<dyn Error>> {
} */

#[tauri::command]
#[cfg(target_os = "windows")]
fn get_winfsp_install_state() -> Result<bool, usize> {
match is_winfsp_installed() {
Ok(is_enabled) => Ok(is_enabled),
Expand Down Expand Up @@ -146,6 +156,7 @@ fn download_file(url: String, out_path: String) -> Result<bool, usize> {
}

#[tauri::command]
#[cfg(target_os = "windows")]
fn get_available_drive_letter() -> Result<String, String> {
match find_first_available_drive_letter() {
Ok(Some(drive)) => Ok(drive),
Expand Down
69 changes: 38 additions & 31 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,49 @@ use tauri::{Manager, Runtime};

use window_shadows::set_shadow;

use std::error::Error;
use std::fs;
use std::io::{self, Write};

//use tauri::AppHandle;

pub fn set_window_shadow<R: Runtime>(app: &tauri::App<R>) {
let window = app.get_window("main").unwrap();
set_shadow(&window, true).expect("Unsupported platform!");
let window = app.get_window("main").unwrap();
set_shadow(&window, true).expect("Unsupported platform!");
}

#[cfg(target_os = "windows")]
pub fn find_first_available_drive_letter() -> Result<Option<String>, io::Error> {
for drive in ('A'..='Z').rev().map(|c| format!("{}:", c)) {
let drive_path = format!("{}\\", drive);
if fs::metadata(&drive_path).is_err() {
// 如果检测到错误,假设盘符未被使用并返回
return Ok(Some(drive));
}
}

// 如果所有盘符都被占用,返回None
Ok(None)
for drive in ('A'..='Z').rev().map(|c| format!("{}:", c)) {
let drive_path = format!("{}\\", drive);
if fs::metadata(&drive_path).is_err() {
// 如果检测到错误,假设盘符未被使用并返回
return Ok(Some(drive));
}
}

// 如果所有盘符都被占用,返回None
Ok(None)
}

use futures_util::stream::StreamExt;
use reqwest::Client;
use std::fs::File;
use futures_util::stream::StreamExt; // 此处使用futures_util
use std::fs::File; // 此处使用futures_util

#[tokio::main]
pub async fn download_with_progress<F>(url: &str, output_path: &str, mut callback: F) -> io::Result<()>
pub async fn download_with_progress<F>(
url: &str,
output_path: &str,
mut callback: F,
) -> io::Result<()>
where
F: FnMut(usize,usize),
F: FnMut(usize, usize),
{
let response = Client::new().get(url).send().await.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;

let response = Client::new()
.get(url)
.send()
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;

let total_size = response.content_length().unwrap_or(1) as usize;

if response.status().is_success() {
Expand All @@ -48,38 +57,36 @@ where
let chunk = item.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
file.write_all(&chunk)?;
downloaded += chunk.len();
callback(total_size,downloaded);
callback(total_size, downloaded);
}
} else {
return Err(io::Error::new(io::ErrorKind::Other, "请求失败"));
}

Ok(())
}

extern crate winreg;
use winreg::enums::*;
use winreg::RegKey;

use std::error::Error;

pub fn is_winfsp_installed() -> Result<bool, Box<dyn Error>> {
#[cfg(target_os = "windows")]
pub fn is_winfsp_installed() -> Result<bool, Box<dyn Error>> {
extern crate winreg;
use winreg::enums::*;
use winreg::RegKey;
// 打开HKEY_LOCAL_MACHINE
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);

// 定义需要检查的注册表键路径
let registry_keys = [
"SOFTWARE\\WinFsp",
"SOFTWARE\\WOW6432Node\\WinFsp",
"SYSTEM\\CurrentControlSet\\Services\\WinFsp.Launcher"
"SYSTEM\\CurrentControlSet\\Services\\WinFsp.Launcher",
];

// 遍历每个注册表键路径
for &registry_key in registry_keys.iter() {
// 尝试打开指定键
match hklm.open_subkey(registry_key) {
Ok(_) => return Ok(true), // 如果键存在(即WinFsp已安装),返回true
Err(_) => continue, // 如果打开键失败(例如键不存在),忽略错误并继续
Ok(_) => return Ok(true), // 如果键存在(即WinFsp已安装),返回true
Err(_) => continue, // 如果打开键失败(例如键不存在),忽略错误并继续
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/controller/storage/parameters/defaults/webdav.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { DefaultParams, ParamsSelectType } from "../../../../type/rclone/storage/defaults";

//
interface WebdavParamsStandard {
// 标准选项
url: string;
Expand All @@ -10,7 +11,7 @@ interface WebdavParamsStandard {
bearer_token?: string;
}


//
interface WebdavParamsAdvanced {
// 高级选项
bearer_token_command?: string;
Expand Down

0 comments on commit 6e81939

Please sign in to comment.