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
+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::fs::*;
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::notification::NotificationData::OpenFileError;
|
||||
use crate::notification::NotificationData::{CompressError, OpenFileError};
|
||||
use crate::notification::{MaybeError, NotificationData};
|
||||
use crate::pages::import::ImportMessage::*;
|
||||
use crate::push_error;
|
||||
@@ -33,6 +33,7 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::task::spawn_blocking;
|
||||
use zip::ZipArchive;
|
||||
use zstd::Decoder;
|
||||
|
||||
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
||||
|
||||
@@ -362,8 +363,8 @@ impl ImportState {
|
||||
Task::perform(
|
||||
async {
|
||||
spawn_blocking(move || {
|
||||
Self::extract_import_file(path)?;
|
||||
let data = Self::read_import_file()?;
|
||||
let schema = Self::extract_import_file(path)?;
|
||||
let data = Self::read_import_file(schema)?;
|
||||
Ok(data)
|
||||
})
|
||||
.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 reader = BufReader::new(file);
|
||||
let mut archive = ZipArchive::new(reader).map_err(|_| OpenFileError)?;
|
||||
@@ -387,20 +388,24 @@ impl ImportState {
|
||||
.truncate(true)
|
||||
.open(&temp_file_path)
|
||||
.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)?;
|
||||
Ok(())
|
||||
Ok(AnkiSchema::Old)
|
||||
} else if let Ok(mut file) = archive.by_name("collection.anki2") {
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
||||
Ok(())
|
||||
Ok(AnkiSchema::Old)
|
||||
} else {
|
||||
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 connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let data = load_groups(&connection)?;
|
||||
let data = load_groups(&connection, schema)?;
|
||||
Ok(data)
|
||||
}
|
||||
fn setup_mapping(&mut self, property_name: String) {
|
||||
@@ -593,3 +598,7 @@ impl ImportState {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum AnkiSchema {
|
||||
Old, Beta
|
||||
}
|
||||
Reference in new issue
Block a user