minimal working on Android filesystem

This commit is contained in:
polv committed 2026-02-27 02:27:21 +07:00
1 parent 8c9ad287eb
commit 164f7c95bd
9 files changed
+603 -15

No files matched your search

+49 -4
View File
@@ -1,22 +1,63 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use cjdic2_core::{AppService, YomitanRow};
use tauri::{AppHandle, Emitter, Manager, path::BaseDirectory};
use tauri::{AppHandle, Emitter, Manager};
#[cfg(target_os = "android")]
use tauri_plugin_fs::FsExt;
#[tauri::command]
pub async fn init_yomitan(
app: AppHandle,
state: tauri::State<'_, AppService>,
) -> Result<(), String> {
let folder_name = "resources/yomitan";
// 1. Resolve the internal Asset URI
let zip_dir = app
.path()
.resolve("resources/yomitan", BaseDirectory::Resource)
.resolve(folder_name, tauri::path::BaseDirectory::Resource)
.map_err(|e| {
eprintln!("{:?}", e);
e.to_string()
})?;
// println!("{:?}", zip_dir.as_path());
state
let config = app.config();
println!("{:#?}", app.asset_resolver());
if let Some(r) = &config.bundle.resources {
println!("{:?}", r);
}
// Embedded files cannot be read with std fs directly, not to mention read_dir
// It need to be copied with app.fs().read(...) one-by-one, and no read_dir here
// @link https://v2.tauri.app/plugin/file-system/
// app.config().bundle.resources don't seem to resolve **/*
#[cfg(target_os = "android")]
let zip_dir = {
let filename = "PixivLight_2026-02-24.zip";
// 2. Define the physical destination in AppData
let app_data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
let app_data_path = app_data_dir.join(filename);
if !app_data_path.exists() {
// 3. IMPORTANT: Use app.fs().read() to read from the APK asset
// std::fs::read will fail with "os error 2" on Android
let data = app
.fs()
.read(&zip_dir.join(filename))
.map_err(|e| format!("Failed to read asset from APK: {}", e))?;
// 4. Use std::fs to write to a real physical path in AppData
if let Some(parent) = app_data_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
std::fs::write(&app_data_path, data)
.map_err(|e| format!("Failed to write to AppData: {}", e))?;
}
app_data_dir
};
let r = state
.load_yomitan_zip_dir(
zip_dir,
"ja",
@@ -32,6 +73,10 @@ pub async fn init_yomitan(
e.to_string()
})?;
if r.new_dicts.len() + r.to_be_removed_dicts.len() > 0 {
app.request_restart();
}
Ok(())
}