Prepare for new anki format
This commit is contained in:
1 parent
0de809fcce
commit
ccfb302770
2 files changed
+34
-11
No files matched your search
@@ -5,6 +5,7 @@ use log::info;
|
|||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use crate::pages::import::AnkiSchema;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ImportData(pub(crate) Vec<ImportGroup>);
|
pub struct ImportData(pub(crate) Vec<ImportGroup>);
|
||||||
@@ -25,7 +26,18 @@ pub struct ImportNote {
|
|||||||
pub fields: Vec<String>,
|
pub fields: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationData> {
|
pub fn load_groups(connection: &Connection, schema: AnkiSchema) -> Result<ImportData, NotificationData> {
|
||||||
|
match schema {
|
||||||
|
AnkiSchema::Old => {
|
||||||
|
load_old_schema(connection)
|
||||||
|
}
|
||||||
|
AnkiSchema::Beta => {
|
||||||
|
load_new_schema(connection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_old_schema(connection: &Connection) -> Result<ImportData, NotificationData> {
|
||||||
let json_models: String = connection
|
let json_models: String = connection
|
||||||
.query_one("select models from col", (), |row| row.get(0))
|
.query_one("select models from col", (), |row| row.get(0))
|
||||||
.map_err(|_| IncorrectAnkiFile)?;
|
.map_err(|_| IncorrectAnkiFile)?;
|
||||||
@@ -82,7 +94,9 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
|
|||||||
}
|
}
|
||||||
Err(IncorrectAnkiFile)
|
Err(IncorrectAnkiFile)
|
||||||
}
|
}
|
||||||
|
fn load_new_schema(connection: &Connection) -> Result<ImportData, NotificationData> {
|
||||||
|
return Err(IncorrectAnkiFile);
|
||||||
|
}
|
||||||
pub fn get_words_of_group(
|
pub fn get_words_of_group(
|
||||||
connection: &Connection,
|
connection: &Connection,
|
||||||
group_id: u64,
|
group_id: u64,
|
||||||
|
|||||||
+18
-9
@@ -6,7 +6,7 @@ use crate::data::repositories::{BatchedRepository, Repository};
|
|||||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||||
use crate::fs::*;
|
use crate::fs::*;
|
||||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||||
use crate::notification::NotificationData::OpenFileError;
|
use crate::notification::NotificationData::{CompressError, OpenFileError};
|
||||||
use crate::notification::{MaybeError, NotificationData};
|
use crate::notification::{MaybeError, NotificationData};
|
||||||
use crate::pages::import::ImportMessage::*;
|
use crate::pages::import::ImportMessage::*;
|
||||||
use crate::push_error;
|
use crate::push_error;
|
||||||
@@ -33,6 +33,7 @@ use std::path::PathBuf;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
|
use zstd::Decoder;
|
||||||
|
|
||||||
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
||||||
|
|
||||||
@@ -362,8 +363,8 @@ impl ImportState {
|
|||||||
Task::perform(
|
Task::perform(
|
||||||
async {
|
async {
|
||||||
spawn_blocking(move || {
|
spawn_blocking(move || {
|
||||||
Self::extract_import_file(path)?;
|
let schema = Self::extract_import_file(path)?;
|
||||||
let data = Self::read_import_file()?;
|
let data = Self::read_import_file(schema)?;
|
||||||
Ok(data)
|
Ok(data)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
@@ -376,7 +377,7 @@ impl ImportState {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_import_file(path: PathBuf) -> MaybeError {
|
fn extract_import_file(path: PathBuf) -> Result<AnkiSchema, NotificationData> {
|
||||||
let file = File::open(path).map_err(|_| OpenFileError)?;
|
let file = File::open(path).map_err(|_| OpenFileError)?;
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut archive = ZipArchive::new(reader).map_err(|_| OpenFileError)?;
|
let mut archive = ZipArchive::new(reader).map_err(|_| OpenFileError)?;
|
||||||
@@ -387,20 +388,24 @@ impl ImportState {
|
|||||||
.truncate(true)
|
.truncate(true)
|
||||||
.open(&temp_file_path)
|
.open(&temp_file_path)
|
||||||
.map_err(|_| OpenFileError)?;
|
.map_err(|_| OpenFileError)?;
|
||||||
if let Ok(mut file) = archive.by_name("collection.anki21") {
|
if let Ok(mut file) = archive.by_name("collection.anki21b") {
|
||||||
|
let mut decoder = Decoder::new(&mut file).map_err(|_| CompressError)?;
|
||||||
|
std::io::copy(&mut decoder, &mut temp_file).map_err(|_| OpenFileError)?;
|
||||||
|
Ok(AnkiSchema::Beta)
|
||||||
|
} else if let Ok(mut file) = archive.by_name("collection.anki21") {
|
||||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
||||||
Ok(())
|
Ok(AnkiSchema::Old)
|
||||||
} else if let Ok(mut file) = archive.by_name("collection.anki2") {
|
} else if let Ok(mut file) = archive.by_name("collection.anki2") {
|
||||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
||||||
Ok(())
|
Ok(AnkiSchema::Old)
|
||||||
} else {
|
} else {
|
||||||
Err(OpenFileError)
|
Err(OpenFileError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn read_import_file() -> Result<ImportData, NotificationData> {
|
fn read_import_file(schema: AnkiSchema) -> Result<ImportData, NotificationData> {
|
||||||
let temp_file_path = temp_import_path();
|
let temp_file_path = temp_import_path();
|
||||||
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||||
let data = load_groups(&connection)?;
|
let data = load_groups(&connection, schema)?;
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
fn setup_mapping(&mut self, property_name: String) {
|
fn setup_mapping(&mut self, property_name: String) {
|
||||||
@@ -593,3 +598,7 @@ impl ImportState {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum AnkiSchema {
|
||||||
|
Old, Beta
|
||||||
|
}
|
||||||
Reference in new issue
Block a user