minimal working on Android with multiple zips' bundle

This commit is contained in:
polv committed 2026-02-28 00:45:51 +07:00
1 parent 7d447f8cf1
commit bd281a3b2e
13 files changed
+165 -115

No files matched your search

Generated
+2
View File
@@ -511,6 +511,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cjdic2-core", "cjdic2-core",
"serde_json",
"tauri", "tauri",
"tauri-build", "tauri-build",
"tauri-plugin-fs", "tauri-plugin-fs",
@@ -524,6 +525,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"blake3", "blake3",
"log",
"rusqlite", "rusqlite",
"serde", "serde",
"serde_json", "serde_json",
+1
View File
@@ -14,3 +14,4 @@ thiserror = "2.0.18"
zip = "8.1.0" zip = "8.1.0"
blake3 = "1.8.3" blake3 = "1.8.3"
anyhow = "1.0.102" anyhow = "1.0.102"
log = "0.4.29"
+1 -1
View File
@@ -29,7 +29,7 @@ fn main() -> Result<(), anyhow::Error> {
println!("zip_file: {:?}", p); println!("zip_file: {:?}", p);
println!( println!(
"{:?}", "{:?}",
AppService::import_yomitan_zip_file(&mut writer, p, "ja", |progress| { AppService::import_yomitan_zip_file(&mut writer, &p, "ja", |progress| {
println!("{:?}", progress); println!("{:?}", progress);
})?, })?,
); );
+3 -1
View File
@@ -14,7 +14,9 @@ mod yomitan;
pub use yomitan::YomitanRow; pub use yomitan::YomitanRow;
mod yomitan_writer; mod yomitan_writer;
pub use yomitan_writer::{YomitanWriter, YomitanZipImportProgress, YomitanZipImportResult}; pub use yomitan_writer::{
YomitanWriter, YomitanZipImportProgress, YomitanZipImportResult, ZipSource,
};
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct Database { pub(crate) struct Database {
+30 -16
View File
@@ -1,9 +1,14 @@
use anyhow::Context;
use blake3::hash; use blake3::hash;
use rusqlite::{Connection, Result, Transaction, params}; use rusqlite::{Connection, Result, Transaction, params};
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use std::{collections::HashMap, fs::File, io::Read, path::PathBuf, time::Instant}; use std::{
borrow::Cow,
collections::HashMap,
io::{Cursor, Read},
path::{Path, PathBuf},
time::Instant,
};
use zip::ZipArchive; use zip::ZipArchive;
use crate::CJDicError; use crate::CJDicError;
@@ -19,6 +24,21 @@ pub(super) fn normalize_term(s: &str) -> String {
.collect() .collect()
} }
pub trait ZipSource {
fn file_name(&self) -> &str;
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>>;
}
impl ZipSource for PathBuf {
fn file_name(&self) -> &str {
Path::file_name(self).and_then(|n| n.to_str()).unwrap_or("")
}
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>> {
std::fs::read(self).map(Cow::Owned)
}
}
#[derive(Serialize, Debug, Clone)] #[derive(Serialize, Debug, Clone)]
pub struct YomitanZipImportProgress { pub struct YomitanZipImportProgress {
pub message: String, pub message: String,
@@ -238,29 +258,23 @@ impl YomitanWriter {
Ok(()) Ok(())
} }
pub fn import_dictionary_zip_file<Callback>( pub fn import_dictionary_zip_file<Z, Callback>(
&mut self, &mut self,
zip_file: PathBuf, zip_file: &Z,
lang: &str, lang: &str,
progress_callback: Callback, progress_callback: Callback,
) -> anyhow::Result<YomitanZipImportResult, CJDicError> ) -> anyhow::Result<YomitanZipImportResult, CJDicError>
where where
Z: ZipSource,
Callback: Fn(YomitanZipImportProgress), Callback: Fn(YomitanZipImportProgress),
{ {
let bundle_name = if let Some(fo) = zip_file.file_name() let bundle_name = zip_file.file_name();
&& let Some(f) = fo.to_str()
{
f
} else {
return Err(CJDicError::FileNameNotFound);
};
let start_time = Instant::now(); let start_time = Instant::now();
self.conn.execute_batch("PRAGMA foreign_keys = off")?; self.conn.execute_batch("PRAGMA foreign_keys = off")?;
let f = let cursor = Cursor::new(zip_file.bytes()?);
File::open(&zip_file).with_context(|| format!("opening zip {}", zip_file.display()))?; let mut archive = ZipArchive::new(cursor)?;
let mut archive = ZipArchive::new(f).with_context(|| "reading zip archive")?;
let index_file = match archive.by_name("index.json") { let index_file = match archive.by_name("index.json") {
Ok(mut f) => { Ok(mut f) => {
@@ -575,8 +589,8 @@ impl YomitanWriter {
tx.commit()?; tx.commit()?;
println!( println!(
"Importing {:?} takes {:.2?}", "Importing {} takes {:.2?}",
zip_file, zip_file.file_name(),
start_time.elapsed() start_time.elapsed()
); );
+43 -6
View File
@@ -1,26 +1,63 @@
use serde::Serialize;
use thiserror::Error; use thiserror::Error;
use zip::result::ZipError; use zip::result::ZipError;
#[derive(Debug, Error)] #[derive(Debug, Error, Serialize)]
#[serde(tag = "kind", content = "message")]
pub enum CJDicError { pub enum CJDicError {
#[error("Database error: {0}")] #[error("Database error: {0}")]
Database(#[from] rusqlite::Error), Database(String),
#[error("Serialization error: {0}")] #[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error), Serialization(String),
#[error("Anyhow error: {0}")] #[error("Anyhow error: {0}")]
AnyhowError(#[from] anyhow::Error), AnyhowError(String),
#[error("IO error: {0}")] #[error("IO error: {0}")]
IOError(#[from] std::io::Error), IOError(String),
#[error("File name not found")] #[error("File name not found")]
FileNameNotFound, FileNameNotFound,
#[error("Zip error: {0}")] #[error("Zip error: {0}")]
ZipError(#[from] ZipError), ZipError(String),
#[error("Not found")] #[error("Not found")]
NotFound, NotFound,
} }
impl From<rusqlite::Error> for CJDicError {
fn from(e: rusqlite::Error) -> Self {
log::error!("{e:#}");
CJDicError::Database(e.to_string())
}
}
impl From<serde_json::Error> for CJDicError {
fn from(e: serde_json::Error) -> Self {
log::error!("{e:#}");
CJDicError::Serialization(e.to_string())
}
}
impl From<anyhow::Error> for CJDicError {
fn from(e: anyhow::Error) -> Self {
log::error!("{e:#}");
CJDicError::AnyhowError(e.to_string())
}
}
impl From<std::io::Error> for CJDicError {
fn from(e: std::io::Error) -> Self {
log::error!("{e:#}");
CJDicError::IOError(e.to_string())
}
}
impl From<ZipError> for CJDicError {
fn from(e: ZipError) -> Self {
log::error!("{e:#}");
CJDicError::ZipError(e.to_string())
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ mod db;
mod error; mod error;
mod service; mod service;
pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult}; pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult, ZipSource};
pub use error::CJDicError; pub use error::CJDicError;
pub use service::AppService; pub use service::AppService;
+18 -26
View File
@@ -1,13 +1,13 @@
use std::{ use std::{
collections::HashSet, collections::{HashMap, HashSet},
fs::read_dir, path::Path,
path::{Path, PathBuf},
}; };
use rusqlite::Connection; use rusqlite::Connection;
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{
ZipSource,
db::{ db::{
Database, YOMITAN_DBFILE, YomitanRow, YomitanWriter, YomitanZipImportProgress, Database, YOMITAN_DBFILE, YomitanRow, YomitanWriter, YomitanZipImportProgress,
YomitanZipImportResult, YomitanZipImportResult,
@@ -51,28 +51,22 @@ impl AppService {
Ok(writer) Ok(writer)
} }
pub fn load_yomitan_zip_dir<LoadCallback, ImportCallback>( pub fn load_yomitan_zip_dir<Z, LoadCallback, ImportCallback>(
&self, &self,
zip_dir: PathBuf, zip_dir: Vec<Z>,
lang: &str, lang: &str,
load_callback: LoadCallback, load_callback: LoadCallback,
import_callback: ImportCallback, import_callback: ImportCallback,
) -> Result<LoadYomitanZipDirResult, CJDicError> ) -> Result<LoadYomitanZipDirResult, CJDicError>
where where
Z: ZipSource,
LoadCallback: Fn(LoadYomitanZipDirResult), LoadCallback: Fn(LoadYomitanZipDirResult),
ImportCallback: Fn(YomitanZipImportProgress), ImportCallback: Fn(YomitanZipImportProgress),
{ {
let mut dir_zip_list: HashSet<String> = HashSet::new(); let mut dir_zip_map: HashMap<String, Z> = HashMap::new();
for entry in read_dir(&zip_dir)? { for entry in zip_dir {
let e = entry?; dir_zip_map.insert(entry.file_name().to_string(), entry);
let p = e.path();
if p.extension().and_then(|s| s.to_str()) == Some("zip")
&& let Some(f) = e.file_name().to_str()
{
dir_zip_list.insert(f.to_string());
}
} }
{ {
@@ -96,14 +90,14 @@ impl AppService {
db_zip_list.insert(b_name); db_zip_list.insert(b_name);
} }
for z in dir_zip_list.iter() { for z in dir_zip_map.keys() {
if !db_zip_list.contains(z) { if !db_zip_list.contains(&z.to_string()) {
new_dicts.push(z.to_string()); new_dicts.push(z.to_string());
} }
} }
for z in db_zip_list.iter() { for z in db_zip_list.iter() {
if !dir_zip_list.contains(z) { if !dir_zip_map.contains_key(z) {
to_be_removed_dicts.push(z.to_string()); to_be_removed_dicts.push(z.to_string());
} }
} }
@@ -138,12 +132,9 @@ impl AppService {
let mut writer = YomitanWriter::new(conn)?; let mut writer = YomitanWriter::new(conn)?;
for z in new_dicts.clone().iter() { for z in new_dicts.clone().iter() {
Self::import_yomitan_zip_file( if let Some(zip_file) = dir_zip_map.get(z) {
&mut writer, Self::import_yomitan_zip_file(&mut writer, zip_file, lang, &import_callback)?;
zip_dir.join(z), }
lang,
&import_callback,
)?;
} }
} }
@@ -153,13 +144,14 @@ impl AppService {
}) })
} }
pub fn import_yomitan_zip_file<Callback>( pub fn import_yomitan_zip_file<Z, Callback>(
writer: &mut YomitanWriter, writer: &mut YomitanWriter,
zip_file: PathBuf, zip_file: &Z,
lang: &str, lang: &str,
progress_callback: Callback, progress_callback: Callback,
) -> Result<YomitanZipImportResult, CJDicError> ) -> Result<YomitanZipImportResult, CJDicError>
where where
Z: ZipSource,
Callback: Fn(YomitanZipImportProgress), Callback: Fn(YomitanZipImportProgress),
{ {
Ok(writer.import_dictionary_zip_file(zip_file, lang, progress_callback)?) Ok(writer.import_dictionary_zip_file(zip_file, lang, progress_callback)?)
+1
View File
@@ -24,3 +24,4 @@ cjdic2-core = { path = "../src-core" }
tauri-plugin-http = "2" tauri-plugin-http = "2"
anyhow = "1.0.102" anyhow = "1.0.102"
tauri-plugin-fs = "2" tauri-plugin-fs = "2"
serde_json = "1.0.149"
+2 -1
View File
@@ -5,4 +5,5 @@
/proguard-tauri.pro /proguard-tauri.pro
/tauri.properties /tauri.properties
/src/main/assets/resources/**/*.zip /src/main/assets/**/*.zip
/src/main/assets/**/manifest.json
+2
View File
@@ -2,3 +2,5 @@
# *.json.zip from https://github.com/scriptin/jmdict-simplified/releases # *.json.zip from https://github.com/scriptin/jmdict-simplified/releases
# TODO: jmdict.db build script # TODO: jmdict.db build script
jmdict.db jmdict.db
manifest.json
+59 -62
View File
@@ -1,72 +1,74 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ use std::{borrow::Cow, path::PathBuf};
use cjdic2_core::{AppService, YomitanRow};
use tauri::{AppHandle, Emitter, Manager}; use cjdic2_core::{AppService, CJDicError, YomitanRow, ZipSource};
#[cfg(target_os = "android")] use tauri::{AppHandle, Emitter, Manager, Runtime, path::BaseDirectory};
use tauri_plugin_fs::FsExt; use tauri_plugin_fs::FsExt;
pub struct BundledZip<R: Runtime> {
pub name: String,
pub path: PathBuf, // resolved asset://localhost/... path on Android
pub app: AppHandle<R>,
}
impl<R: Runtime> BundledZip<R> {
pub fn new(app: AppHandle<R>, relative_path: &str) -> Result<Self, tauri::Error> {
let name = relative_path
.split('/')
.last()
.unwrap_or(relative_path)
.to_string();
let path = app.path().resolve(relative_path, BaseDirectory::Resource)?;
Ok(Self { name, path, app })
}
}
impl<R: Runtime> ZipSource for BundledZip<R> {
fn file_name(&self) -> &str {
&self.name
}
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>> {
self.app
.fs()
.read(&self.path) // PathBuf implements Into<FilePath>
.map(Cow::Owned)
}
}
#[tauri::command] #[tauri::command]
pub async fn init_yomitan( pub async fn init_yomitan(
app: AppHandle, app: AppHandle,
state: tauri::State<'_, AppService>, state: tauri::State<'_, AppService>,
) -> Result<(), String> { ) -> Result<(), CJDicError> {
let folder_name = "resources/yomitan"; let folder_name = "resources/yomitan";
// 1. Resolve the internal Asset URI
let zip_dir = app let zip_dir = app
.path() .path()
.resolve(folder_name, tauri::path::BaseDirectory::Resource) .resolve(folder_name, tauri::path::BaseDirectory::Resource)
.map_err(|e| { .map_err(|e| CJDicError::AnyhowError(e.to_string()))?;
eprintln!("{:?}", e);
e.to_string()
})?;
// Embedded files cannot be read with std fs directly, not to mention read_dir // Manifest is needed, as Android bundled fs can't read_dir
// It need to be copied with app.fs().read(...) one-by-one, and no read_dir here let files: Vec<String> =
// @link https://v2.tauri.app/plugin/file-system/ serde_json::from_str(&app.fs().read_to_string(zip_dir.join("manifest.json"))?)?;
// app.config().bundle.resources don't seem to resolve **/*
// utils::resources::ResourcePaths::new(&["pattern*".to_string()], true); didn't work correctly in Android
#[cfg(target_os = "android")]
let zip_dir = {
let filename = "PixivLight_2026-02-24.zip";
// 2. Define the physical destination in AppData let zip_files = files
let app_data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?; .iter()
let app_data_path = app_data_dir.join(filename); .map(|f| BundledZip::new(app.clone(), &format!("{folder_name}/{f}")))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| CJDicError::AnyhowError(e.to_string()))?;
if !app_data_path.exists() { let r = state.load_yomitan_zip_dir(
// 3. IMPORTANT: Use app.fs().read() to read from the APK asset zip_files,
// std::fs::read will fail with "os error 2" on Android "ja",
let data = app |r| {
.fs() app.emit("load-yomitan-dir", r).unwrap();
.read(&zip_dir.join(filename)) },
.map_err(|e| format!("Failed to read asset from APK: {}", e))?; |r| {
app.emit("yomitan-import-progress", r).unwrap();
// 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",
|r| {
app.emit("load-yomitan-dir", r).unwrap();
},
|r| {
app.emit("yomitan-import-progress", r).unwrap();
},
)
.map_err(|e| {
eprintln!("{:?}", e);
e.to_string()
})?;
if r.new_dicts.len() + r.to_be_removed_dicts.len() > 0 { if r.new_dicts.len() + r.to_be_removed_dicts.len() > 0 {
app.request_restart(); app.request_restart();
@@ -82,11 +84,6 @@ pub async fn search_yomitan(
limit: u32, limit: u32,
offset: u32, offset: u32,
state: tauri::State<'_, AppService>, state: tauri::State<'_, AppService>,
) -> Result<Vec<YomitanRow>, String> { ) -> Result<Vec<YomitanRow>, CJDicError> {
state state.search_yomitan(q_term, q_reading, limit, offset)
.search_yomitan(q_term, q_reading, limit, offset)
.map_err(|e| {
eprintln!("{:?}", e);
e.to_string()
})
} }
+2 -1
View File
@@ -24,7 +24,8 @@
}, },
"bundle": { "bundle": {
"resources": [ "resources": [
"resources/**/*.zip" "resources/**/*.zip",
"resources/**/manifest.json"
], ],
"active": true, "active": true,
"targets": "all", "targets": "all",