mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
extract image assets for yomitan zip
This commit is contained in:
1 parent
79deaf9372
commit
52c25992ab
11 files changed
+114
-52
No files matched your search
@@ -17,7 +17,7 @@ fn main() -> Result<(), anyhow::Error> {
|
||||
println!("{:?}", p);
|
||||
})?;
|
||||
|
||||
let zip_dir = Path::new("src-tauri/resources/yomitan");
|
||||
let zip_dir = Path::new("tmp/yomitan");
|
||||
println!("zip_dir: {:?}", absolute(zip_dir)); // Relative to workspace root
|
||||
|
||||
for entry in
|
||||
@@ -31,9 +31,15 @@ fn main() -> Result<(), anyhow::Error> {
|
||||
|
||||
println!(
|
||||
"{:?}",
|
||||
AppService::import_yomitan_zip_file(&mut writer, &p, "ja", |progress| {
|
||||
println!("{:?}", progress);
|
||||
})?,
|
||||
AppService::import_yomitan_zip_file(
|
||||
&mut writer,
|
||||
&p,
|
||||
&zip_dir.to_path_buf(),
|
||||
"ja",
|
||||
|progress| {
|
||||
println!("{:?}", progress);
|
||||
}
|
||||
)?,
|
||||
);
|
||||
} else {
|
||||
println!("not zip_file: {:?}", p);
|
||||
|
||||
@@ -7,7 +7,8 @@ use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
env::current_dir,
|
||||
io::{Cursor, Read},
|
||||
fs::{File, create_dir_all},
|
||||
io::{self, Cursor, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use zip::ZipArchive;
|
||||
@@ -437,6 +438,7 @@ impl YomitanWriter {
|
||||
pub fn import_dictionary_zip_file<Z, Callback>(
|
||||
&mut self,
|
||||
zip_file: &Z,
|
||||
asset_dir: &PathBuf,
|
||||
lang: &str,
|
||||
progress_callback: Callback,
|
||||
) -> anyhow::Result<YomitanZipImportResult, CJDicError>
|
||||
@@ -452,6 +454,27 @@ impl YomitanWriter {
|
||||
let cursor = Cursor::new(zip_file.bytes()?);
|
||||
let mut archive = ZipArchive::new(cursor)?;
|
||||
|
||||
for i in 0..archive.len() {
|
||||
// Metadata — borrow ends before by_index_raw
|
||||
let (name, is_dir) = {
|
||||
let f = archive.by_index(i)?;
|
||||
(f.name().to_owned(), f.is_dir())
|
||||
};
|
||||
|
||||
if !name.starts_with("assets") || is_dir {
|
||||
continue;
|
||||
}
|
||||
|
||||
let out_path = asset_dir.join(name);
|
||||
if let Some(parent) = out_path.parent() {
|
||||
create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let mut entry = archive.by_index(i)?;
|
||||
let mut out = File::create(&out_path)?;
|
||||
io::copy(&mut entry, &mut out)?; // writes plain bytes
|
||||
}
|
||||
|
||||
let index_file = match archive.by_name("index.json") {
|
||||
Ok(mut f) => {
|
||||
let mut s = String::new();
|
||||
|
||||
+11
-3
@@ -1,6 +1,6 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use rusqlite::{Connection, params_from_iter};
|
||||
@@ -166,6 +166,7 @@ impl AppService {
|
||||
pub fn load_yomitan_zip_dir<Z, LoadCallback, ImportCallback>(
|
||||
&self,
|
||||
zip_dir: Vec<Z>,
|
||||
asset_dir: &PathBuf,
|
||||
lang: &str,
|
||||
load_callback: LoadCallback,
|
||||
import_callback: ImportCallback,
|
||||
@@ -246,7 +247,13 @@ impl AppService {
|
||||
let mut writer = YomitanWriter::new(self.db.dir.clone())?;
|
||||
for z in new_dicts.clone().iter() {
|
||||
if let Some(zip_file) = dir_zip_map.get(z) {
|
||||
Self::import_yomitan_zip_file(&mut writer, *zip_file, lang, &import_callback)?;
|
||||
Self::import_yomitan_zip_file(
|
||||
&mut writer,
|
||||
*zip_file,
|
||||
&asset_dir,
|
||||
lang,
|
||||
&import_callback,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -268,6 +275,7 @@ impl AppService {
|
||||
pub fn import_yomitan_zip_file<Z, Callback>(
|
||||
writer: &mut YomitanWriter, // Reuse existing connection
|
||||
zip_file: &Z,
|
||||
asset_dir: &PathBuf,
|
||||
lang: &str,
|
||||
progress_callback: Callback,
|
||||
) -> Result<YomitanZipImportResult, CJDicError>
|
||||
@@ -275,7 +283,7 @@ impl AppService {
|
||||
Z: ZipSource,
|
||||
Callback: Fn(YomitanProgress),
|
||||
{
|
||||
Ok(writer.import_dictionary_zip_file(zip_file, lang, progress_callback)?)
|
||||
Ok(writer.import_dictionary_zip_file(zip_file, asset_dir, lang, progress_callback)?)
|
||||
}
|
||||
|
||||
pub fn remove_yomitan_dictionary(
|
||||
|
||||
Reference in new issue
Block a user