Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve/rust side code #198

Merged
merged 7 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 15 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,43 +44,29 @@ 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)]
Expand Down
4 changes: 2 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 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