Skip to content

Commit

Permalink
Merge branch 'master' into ci/cargo-clippy-check
Browse files Browse the repository at this point in the history
  • Loading branch information
zty012 authored Dec 28, 2024
2 parents 15e019f + 72e3cae commit 75dd84c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pnpm lint-staged || bun lint-staged
pnpm lint-staged
56 changes: 22 additions & 34 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::fs::File;
use std::env;
use std::io::Read;
use std::io::Write;

use base64::engine::general_purpose;
use base64::Engine;

use std::env;
use std::fs::read; // 引入 read 函数用于读取文件
#[cfg(debug_assertions)]
use tauri::Manager; // 引入 base64 编码函数
use tauri::Runtime;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
Expand All @@ -28,9 +25,7 @@ fn open_file_by_path(path: String) -> String {
/// 保存字符串到指定路径
#[tauri::command]
fn save_file_by_path(path: String, content: String) -> Result<bool, String> {
let mut file = std::fs::File::create(&path).map_err(|e| e.to_string())?;
file.write_all(content.as_bytes())
.map_err(|e| e.to_string())?;
std::fs::write(path, content).map_err(|e| e.to_string())?;
Ok(true)
}

Expand All @@ -49,47 +44,40 @@ fn check_json_exist(path: String) -> bool {

#[tauri::command]
fn convert_image_to_base64(image_path: String) -> Result<String, String> {
match read(&image_path) {
Ok(image_data) => {
// let base64_str = Engine::encode(&image_data);
let base64_str = general_purpose::STANDARD.encode(&image_data);
Ok(base64_str)
}
Err(e) => Err(format!("无法读取文件: {}, {}", e, image_path)),
}
Ok(general_purpose::STANDARD
.encode(&std::fs::read(image_path).map_err(|e| format!("无法读取文件: {}", e))?))
}

/// 将base64编码字符串保存为图片文件
#[tauri::command]
fn save_base64_to_image(base64_str: &str, file_name: &str) -> Result<(), String> {
// 进行解码
match general_purpose::STANDARD.decode(base64_str) {
Ok(image_data) => {
// 创建文件并写入数据
let mut file = File::create(file_name).map_err(|e| format!("无法创建文件: {}", e))?;
file.write_all(&image_data)
.map_err(|e| format!("无法写入文件: {}", e))?;
Ok(())
}
Err(e) => Err(format!("解码失败: {}", e)),
}
std::fs::write(
file_name,
&general_purpose::STANDARD
.decode(base64_str)
.map_err(|e| format!("解码失败: {}", e))?,
)
.map_err(|e| e.to_string())?;
Ok(())
}

/// 读取 MP3 文件并返回其 Base64 编码字符串
#[tauri::command]
fn read_mp3_file(path: String) -> Result<String, String> {
let mut file = File::open(&path).map_err(|e| format!("无法打开文件: {}", e))?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.map_err(|e| format!("读取文件时出错: {}", e))?;

// 将文件内容编码为 Base64
let base64_str = general_purpose::STANDARD.encode(&buffer);
Ok(base64_str)
Ok(general_purpose::STANDARD
.encode(&std::fs::read(path).map_err(|e| format!("读取文件时出错: {}", e))?))
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
// 在 Linux 上禁用 DMA-BUF 渲染器
// 否则无法在 Linux 上运行
// 相同的bug: https://github.com/tauri-apps/tauri/issues/10702
// 解决方案来源: https://github.com/clash-verge-rev/clash-verge-rev/blob/ae5b2cfb79423c7e76a281725209b812774367fa/src-tauri/src/lib.rs#L27-L28
#[cfg(target_os = "linux")]
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");

tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::new().build())
.setup(|app| {
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.0.0-dev",
"identifier": "liren.project-graph",
"build": {
"beforeDevCommand": "pnpm dev",
"beforeDevCommand": "pnpm dev || bun run dev",
"devUrl": "http://localhost:1420",
"beforeBuildCommand": "pnpm build",
"beforeBuildCommand": "pnpm build || bun run build",
"frontendDist": "../dist"
},
"app": {
Expand All @@ -19,6 +19,7 @@
"dragDropEnabled": false
}
],
"macOSPrivateApi": true,
"security": {
"csp": null,
"capabilities": []
Expand Down
2 changes: 1 addition & 1 deletion src/core/RecentFileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export namespace RecentFileManager {
for (const file of recentFiles) {
try {
// const isExists = await exists(file.path); // 检查文件是否存在
const isExists = await invoke<string>("check_json_exist", {
const isExists = await invoke<boolean>("check_json_exist", {
path: file.path,
});
if (isExists) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/stage/StageSaveManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export namespace StageSaveManager {
*/
export async function backupHandle(path: string, data: Serialized.File) {
const backupFolderPath = PathString.dirPath(path);
const isExists = await invoke<string>("check_json_exist", {
const isExists = await invoke<boolean>("check_json_exist", {
path: backupFolderPath,
});
if (!isExists) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async function loadStartFile() {
if (path === "") {
return;
}
const isExists = await invoke<string>("check_json_exist", {
const isExists = await invoke<boolean>("check_json_exist", {
path,
});
if (isExists) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/_toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ async function openBrowserOrFile() {
// 是网址
myOpen(nodeText);
} else {
const isExists = await invoke<string>("check_json_exist", {
const isExists = await invoke<boolean>("check_json_exist", {
path: nodeText,
});
if (isExists) {
Expand Down

0 comments on commit 75dd84c

Please sign in to comment.