mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +00:00
fix downloader, no bundle
This commit is contained in:
1 parent
5380742513
commit
047054f8f7
4 files changed
+129
-191
No files matched your search
+80
-91
@@ -1,95 +1,69 @@
|
||||
use std::{borrow::Cow, fs::File, io::Write, path::PathBuf};
|
||||
use std::{fs::File, io::Write, path::PathBuf};
|
||||
|
||||
use cjdic2_core::{
|
||||
AppService, CJDicError, SqlParam, YomitanRow, YomitanZipImportResult, ZipSource,
|
||||
};
|
||||
use cjdic2_core::{AppService, CJDicError, SqlParam, YomitanRow, YomitanZipImportResult};
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use tauri::{AppHandle, Emitter, Manager, Runtime, path::BaseDirectory};
|
||||
use tauri_plugin_fs::FsExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Emitter, Manager};
|
||||
|
||||
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)
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
pub struct YomitanDictEntry {
|
||||
url: String,
|
||||
filepath: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn init_yomitan(
|
||||
app: AppHandle,
|
||||
dicts: Vec<YomitanDictEntry>,
|
||||
lang: String,
|
||||
state: tauri::State<'_, AppService>,
|
||||
) -> Result<(), CJDicError> {
|
||||
let mut new_dicts: Vec<String> = vec![];
|
||||
let mut to_be_removed_dicts: Vec<String> = vec![];
|
||||
|
||||
for lang in vec!["ja"] {
|
||||
let folder_name = format!("resources/yomitan/{lang}");
|
||||
let folder_name = folder_name.as_str();
|
||||
let app_dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
let zip_dir = app
|
||||
.path()
|
||||
.resolve(folder_name, tauri::path::BaseDirectory::Resource)
|
||||
for d in dicts.iter().clone() {
|
||||
if !app_dir.join(&d.filepath).exists() {
|
||||
download_url_local(
|
||||
d.url.clone(),
|
||||
app_dir.clone(),
|
||||
d.filepath.clone(),
|
||||
|progress| {
|
||||
app.emit("download-url-progress", progress).unwrap();
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
// Manifest is needed, as Android bundled fs can't read_dir
|
||||
let files: Vec<String> =
|
||||
serde_json::from_str(&app.fs().read_to_string(zip_dir.join("manifest.json"))?)?;
|
||||
|
||||
let zip_files = files
|
||||
.iter()
|
||||
.map(|f| BundledZip::new(app.clone(), &format!("{folder_name}/{f}")))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
if zip_files.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut r = state.load_yomitan_zip_dir(
|
||||
zip_files,
|
||||
lang,
|
||||
|r| {
|
||||
app.emit("load-yomitan-dir", r).unwrap();
|
||||
},
|
||||
|r| {
|
||||
app.emit("yomitan-import-progress", r).unwrap();
|
||||
},
|
||||
)?;
|
||||
|
||||
new_dicts.append(&mut r.new_dicts);
|
||||
to_be_removed_dicts.append(&mut r.to_be_removed_dicts);
|
||||
}
|
||||
|
||||
let zip_files: Vec<PathBuf> = dicts
|
||||
.into_iter()
|
||||
.map(|d| app_dir.join(&d.filepath))
|
||||
.collect();
|
||||
|
||||
let mut r = state.load_yomitan_zip_dir(
|
||||
zip_files,
|
||||
lang.as_str(),
|
||||
|r| {
|
||||
app.emit("load-yomitan-dir", r).unwrap();
|
||||
},
|
||||
|r| {
|
||||
app.emit("yomitan-import-progress", r).unwrap();
|
||||
},
|
||||
)?;
|
||||
|
||||
new_dicts.append(&mut r.new_dicts);
|
||||
to_be_removed_dicts.append(&mut r.to_be_removed_dicts);
|
||||
|
||||
if new_dicts.len() + to_be_removed_dicts.len() > 0 {
|
||||
// Cleanup doesn't appear to help even after app restart
|
||||
// state.cleanup_yomitan_writer()?;
|
||||
println!("Imported {:?}", new_dicts);
|
||||
println!("Removed {:?}", to_be_removed_dicts);
|
||||
println!("Request app restart?",);
|
||||
|
||||
// Request app restart anyway
|
||||
app.request_restart();
|
||||
@@ -157,17 +131,16 @@ struct DownloadProgress {
|
||||
downloaded: u64,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn download_url(
|
||||
app: AppHandle,
|
||||
async fn download_url_local<Callback>(
|
||||
url: String,
|
||||
data_dir: PathBuf,
|
||||
filepath: String,
|
||||
) -> Result<bool, CJDicError> {
|
||||
callback: Callback,
|
||||
) -> Result<bool, CJDicError>
|
||||
where
|
||||
Callback: Fn(DownloadProgress),
|
||||
{
|
||||
let client = Client::new();
|
||||
let data_dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
let file_pf = data_dir.join(filepath.as_str());
|
||||
|
||||
// Making the asynchronous GET request
|
||||
@@ -192,16 +165,12 @@ pub async fn download_url(
|
||||
file.write_all(&chunk)?;
|
||||
downloaded += chunk.len() as u64;
|
||||
|
||||
app.emit(
|
||||
"download-url-progress",
|
||||
DownloadProgress {
|
||||
url: url.clone(),
|
||||
filepath: filepath.clone(),
|
||||
content_length,
|
||||
downloaded,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
callback(DownloadProgress {
|
||||
url: url.clone(),
|
||||
filepath: filepath.clone(),
|
||||
content_length,
|
||||
downloaded,
|
||||
});
|
||||
}
|
||||
println!("File downloaded successfully to {:?}", &file_pf);
|
||||
} else {
|
||||
@@ -210,3 +179,23 @@ pub async fn download_url(
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn download_url(
|
||||
app: AppHandle,
|
||||
url: String,
|
||||
filepath: String,
|
||||
) -> Result<bool, CJDicError> {
|
||||
download_url_local(
|
||||
url,
|
||||
app.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?,
|
||||
filepath,
|
||||
|progress| {
|
||||
app.emit("download-url-progress", progress).unwrap();
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))
|
||||
}
|
||||
Reference in new issue
Block a user