mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -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::{
|
||||
collections::{HashMap, HashSet},
|
||||
path::{Path, PathBuf},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use itertools::Itertools;
|
||||
|
@ -70,10 +70,10 @@ impl ExportData {
|
|||
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| {
|
||||
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);
|
||||
|
|
|
@ -47,7 +47,7 @@ impl Collection {
|
|||
let media = if let Some(media_fn) = media_fn {
|
||||
media_fn(media)
|
||||
} 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;
|
||||
|
||||
|
@ -73,7 +73,7 @@ impl Collection {
|
|||
let mut data = ExportData::default();
|
||||
data.gather_data(self, deck_id, with_scheduling)?;
|
||||
if with_media {
|
||||
data.gather_media_paths(&self.media_folder);
|
||||
data.gather_media_paths();
|
||||
}
|
||||
|
||||
let mut temp_col = Collection::new_minimal(path)?;
|
||||
|
|
|
@ -71,16 +71,24 @@ impl Collection {
|
|||
pub struct MediaIter(Box<dyn Iterator<Item = io::Result<PathBuf>>>);
|
||||
|
||||
impl MediaIter {
|
||||
/// Iterator over all files in the given path, without traversing subfolders.
|
||||
pub fn from_folder(path: &Path) -> Result<Self> {
|
||||
Ok(Self(Box::new(
|
||||
read_dir_files(path)?.map(|res| res.map(|entry| entry.path())),
|
||||
)))
|
||||
}
|
||||
|
||||
/// Skips missing paths.
|
||||
pub fn from_file_list(list: impl IntoIterator<Item = PathBuf> + 'static) -> Self {
|
||||
/// Iterator over all given files in the given folder.
|
||||
/// Missing files are silently ignored.
|
||||
pub fn from_file_list(
|
||||
list: impl IntoIterator<Item = PathBuf> + 'static,
|
||||
folder: PathBuf,
|
||||
) -> Self {
|
||||
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