mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 08:46:37 -04:00
Only store file folder once in MediaIter
This commit is contained in:
parent
a46026f283
commit
d1dd0586bd
3 changed files with 16 additions and 8 deletions
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
path::{Path, PathBuf},
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -70,10 +70,10 @@ impl ExportData {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn gather_media_paths(&mut self, media_folder: &Path) {
|
pub(super) fn gather_media_paths(&mut self) {
|
||||||
let mut inserter = |name: &str| {
|
let mut inserter = |name: &str| {
|
||||||
if filename_is_safe(name) {
|
if filename_is_safe(name) {
|
||||||
self.media_paths.insert(media_folder.join(name));
|
self.media_paths.insert(PathBuf::from(name));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let svg_getter = svg_getter(&self.notetypes);
|
let svg_getter = svg_getter(&self.notetypes);
|
||||||
|
|
|
@ -47,7 +47,7 @@ impl Collection {
|
||||||
let media = if let Some(media_fn) = media_fn {
|
let media = if let Some(media_fn) = media_fn {
|
||||||
media_fn(media)
|
media_fn(media)
|
||||||
} else {
|
} else {
|
||||||
MediaIter::from_file_list(media)
|
MediaIter::from_file_list(media, self.media_folder.clone())
|
||||||
};
|
};
|
||||||
let col_size = temp_col.as_file().metadata()?.len() as usize;
|
let col_size = temp_col.as_file().metadata()?.len() as usize;
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ impl Collection {
|
||||||
let mut data = ExportData::default();
|
let mut data = ExportData::default();
|
||||||
data.gather_data(self, deck_id, with_scheduling)?;
|
data.gather_data(self, deck_id, with_scheduling)?;
|
||||||
if with_media {
|
if with_media {
|
||||||
data.gather_media_paths(&self.media_folder);
|
data.gather_media_paths();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut temp_col = Collection::new_minimal(path)?;
|
let mut temp_col = Collection::new_minimal(path)?;
|
||||||
|
|
|
@ -71,16 +71,24 @@ impl Collection {
|
||||||
pub struct MediaIter(Box<dyn Iterator<Item = io::Result<PathBuf>>>);
|
pub struct MediaIter(Box<dyn Iterator<Item = io::Result<PathBuf>>>);
|
||||||
|
|
||||||
impl MediaIter {
|
impl MediaIter {
|
||||||
|
/// Iterator over all files in the given path, without traversing subfolders.
|
||||||
pub fn from_folder(path: &Path) -> Result<Self> {
|
pub fn from_folder(path: &Path) -> Result<Self> {
|
||||||
Ok(Self(Box::new(
|
Ok(Self(Box::new(
|
||||||
read_dir_files(path)?.map(|res| res.map(|entry| entry.path())),
|
read_dir_files(path)?.map(|res| res.map(|entry| entry.path())),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Skips missing paths.
|
/// Iterator over all given files in the given folder.
|
||||||
pub fn from_file_list(list: impl IntoIterator<Item = PathBuf> + 'static) -> Self {
|
/// Missing files are silently ignored.
|
||||||
|
pub fn from_file_list(
|
||||||
|
list: impl IntoIterator<Item = PathBuf> + 'static,
|
||||||
|
folder: PathBuf,
|
||||||
|
) -> Self {
|
||||||
Self(Box::new(
|
Self(Box::new(
|
||||||
list.into_iter().filter(|path| path.exists()).map(Ok),
|
list.into_iter()
|
||||||
|
.map(move |file| folder.join(file))
|
||||||
|
.filter(|path| path.exists())
|
||||||
|
.map(Ok),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue