mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
add downloader
This commit is contained in:
1 parent
811dd73e13
commit
5380742513
11 files changed
+403
-94
No files matched your search
@@ -25,3 +25,5 @@ tauri-plugin-http = "2"
|
||||
anyhow = "1.0.102"
|
||||
tauri-plugin-fs = "2"
|
||||
serde_json = "1.0.149"
|
||||
reqwest = { version = "0.13.2", features = ["stream"] }
|
||||
serde = "1.0.228"
|
||||
@@ -17,12 +17,13 @@
|
||||
]
|
||||
},
|
||||
"fs:allow-exists",
|
||||
"fs:allow-appdata-write",
|
||||
"fs:allow-appdata-meta",
|
||||
"fs:allow-read-dir",
|
||||
"fs:allow-appdata-read",
|
||||
{
|
||||
"identifier": "fs:scope",
|
||||
"allow": [
|
||||
"$APPDATA/**/*.zip"
|
||||
"$APPDATA/**/*.zip",
|
||||
"$APPDATA/yomitan/*"
|
||||
],
|
||||
"deny": [
|
||||
"$APPDATA/*.db"
|
||||
|
||||
+101
-4
@@ -1,6 +1,10 @@
|
||||
use std::{borrow::Cow, path::PathBuf};
|
||||
use std::{borrow::Cow, fs::File, io::Write, path::PathBuf};
|
||||
|
||||
use cjdic2_core::{AppService, CJDicError, SqlParam, YomitanRow, ZipSource};
|
||||
use cjdic2_core::{
|
||||
AppService, CJDicError, SqlParam, YomitanRow, YomitanZipImportResult, ZipSource,
|
||||
};
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use tauri::{AppHandle, Emitter, Manager, Runtime, path::BaseDirectory};
|
||||
use tauri_plugin_fs::FsExt;
|
||||
|
||||
@@ -52,7 +56,7 @@ pub async fn init_yomitan(
|
||||
let zip_dir = app
|
||||
.path()
|
||||
.resolve(folder_name, tauri::path::BaseDirectory::Resource)
|
||||
.map_err(|e| CJDicError::AnyhowError(e.to_string()))?;
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
// Manifest is needed, as Android bundled fs can't read_dir
|
||||
let files: Vec<String> =
|
||||
@@ -62,7 +66,7 @@ pub async fn init_yomitan(
|
||||
.iter()
|
||||
.map(|f| BundledZip::new(app.clone(), &format!("{folder_name}/{f}")))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| CJDicError::AnyhowError(e.to_string()))?;
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
if zip_files.is_empty() {
|
||||
continue;
|
||||
@@ -94,6 +98,37 @@ pub async fn init_yomitan(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn import_yomitan_dict(
|
||||
app: AppHandle,
|
||||
bundle_name: &str,
|
||||
lang: &str,
|
||||
state: tauri::State<'_, AppService>,
|
||||
) -> Result<YomitanZipImportResult, CJDicError> {
|
||||
let mut data_dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
data_dir.push("yomitan");
|
||||
data_dir.push(lang);
|
||||
data_dir.push(bundle_name);
|
||||
|
||||
let mut writer = state.get_yomitan_writer()?;
|
||||
AppService::import_yomitan_zip_file(&mut writer, &data_dir, lang, |r| {
|
||||
app.emit("yomitan-import-progress", r).unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn remove_yomitan_dict(
|
||||
bundle_name: &str,
|
||||
lang: &str,
|
||||
state: tauri::State<'_, AppService>,
|
||||
) -> Result<(), CJDicError> {
|
||||
let mut writer = state.get_yomitan_writer()?;
|
||||
AppService::remove_yomitan_dictionary(&mut writer, bundle_name, lang)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn search_yomitan(
|
||||
q_term: &str,
|
||||
@@ -113,3 +148,65 @@ pub async fn execute_sql(
|
||||
) -> Result<serde_json::Value, CJDicError> {
|
||||
state.execute_sql(sql, params)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
struct DownloadProgress {
|
||||
url: String,
|
||||
filepath: String,
|
||||
content_length: u64,
|
||||
downloaded: u64,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn download_url(
|
||||
app: AppHandle,
|
||||
url: String,
|
||||
filepath: String,
|
||||
) -> Result<bool, CJDicError> {
|
||||
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
|
||||
let mut response = client
|
||||
.get(url.as_str())
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
// Check if the request was successful
|
||||
if response.status().is_success() {
|
||||
let content_length = response.content_length().unwrap_or(0);
|
||||
let mut file = File::create(&file_pf).map_err(|e| CJDicError::Error(e.to_string()))?;
|
||||
|
||||
// Downloading the file in chunks
|
||||
let mut downloaded: u64 = 0;
|
||||
while let Some(chunk) = response
|
||||
.chunk()
|
||||
.await
|
||||
.map_err(|e| CJDicError::Error(e.to_string()))?
|
||||
{
|
||||
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();
|
||||
}
|
||||
println!("File downloaded successfully to {:?}", &file_pf);
|
||||
} else {
|
||||
eprintln!("Failed to download file: {:?}", response.status());
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
@@ -15,20 +15,24 @@ pub fn run() {
|
||||
.setup(|app| {
|
||||
let app_dir = app.path().app_config_dir()?;
|
||||
create_dir_all(&app_dir)?;
|
||||
// println!("{:?}", app_dir);
|
||||
|
||||
// db_path is app_dir;
|
||||
let db_dir = app_dir.as_path();
|
||||
// let db_dir = Path::new(r"D:\Projects\cjdic2\tmp\save-db");
|
||||
let service = AppService::new(&db_dir)?;
|
||||
app.manage(service);
|
||||
|
||||
let app_data_dir = app.path().app_data_dir()?;
|
||||
create_dir_all(&app_data_dir.join("yomitan/ja"))?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
init_yomitan,
|
||||
import_yomitan_dict,
|
||||
remove_yomitan_dict,
|
||||
search_yomitan,
|
||||
execute_sql
|
||||
execute_sql,
|
||||
download_url,
|
||||
])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application")
|
||||
|
||||
Reference in new issue
Block a user