Add a .with_desktop_media_paths() helper

This commit is contained in:
Damien Elmes 2023-05-18 11:02:02 +10:00
parent cff613bfef
commit 63fd70ad34
4 changed files with 16 additions and 10 deletions

View File

@ -17,6 +17,7 @@ use crate::decks::Deck;
use crate::decks::DeckId;
use crate::error::Result;
use crate::i18n::I18n;
use crate::io::create_dir_all;
use crate::notetype::Notetype;
use crate::notetype::NotetypeId;
use crate::scheduler::queue::CardQueues;
@ -89,6 +90,17 @@ impl CollectionBuilder {
self
}
/// For a `foo.anki2` file, use `foo.media` and `foo.mdb`. Mobile clients
/// use different paths, so the backend must continue to use
/// [set_media_paths].
pub fn with_desktop_media_paths(&mut self) -> &mut Self {
let col_path = self.collection_path.as_ref().unwrap();
let media_folder = col_path.with_extension("media");
create_dir_all(&media_folder).expect("creating media folder");
let media_db = col_path.with_extension("mdb");
self.set_media_paths(media_folder, media_db)
}
pub fn set_server(&mut self, server: bool) -> &mut Self {
self.server = Some(server);
self

View File

@ -9,7 +9,6 @@ use tempfile::tempdir;
use crate::collection::CollectionBuilder;
use crate::import_export::package::import_colpkg;
use crate::io::create_dir;
use crate::io::create_dir_all;
use crate::io::read_file;
use crate::media::MediaManager;
@ -17,11 +16,9 @@ use crate::prelude::*;
fn collection_with_media(dir: &Path, name: &str) -> Result<Collection> {
let name = format!("{name}_src");
let media_folder = dir.join(format!("{name}.media"));
create_dir(&media_folder)?;
// add collection with sentinel note
let mut col = CollectionBuilder::new(dir.join(format!("{name}.anki2")))
.set_media_paths(media_folder, dir.join(format!("{name}.mdb")))
.with_desktop_media_paths()
.build()?;
let nt = col.get_notetype_by_name("Basic")?.unwrap();
let mut note = nt.new_note();

View File

@ -480,7 +480,7 @@ impl SyncTestContext {
pub fn col1(&self) -> Collection {
let base = self.folder.path();
CollectionBuilder::new(base.join("col1.anki2"))
.set_media_paths(base.join("col1.media"), base.join("col1.media.db"))
.with_desktop_media_paths()
.build()
.unwrap()
}
@ -488,7 +488,7 @@ impl SyncTestContext {
pub fn col2(&self) -> Collection {
let base = self.folder.path();
CollectionBuilder::new(base.join("col2.anki2"))
.set_media_paths(base.join("col2.media"), base.join("col2.media.db"))
.with_desktop_media_paths()
.build()
.unwrap()
}

View File

@ -10,17 +10,14 @@ use tempfile::TempDir;
use crate::collection::CollectionBuilder;
use crate::deckconfig::DeckConfigInner;
use crate::io::create_dir;
use crate::media::MediaManager;
use crate::prelude::*;
pub(crate) fn open_fs_test_collection(name: &str) -> (Collection, TempDir) {
let tempdir = tempdir().unwrap();
let dir = tempdir.path();
let media_folder = dir.join(format!("{name}.media"));
create_dir(&media_folder).unwrap();
let col = CollectionBuilder::new(dir.join(format!("{name}.anki2")))
.set_media_paths(media_folder, dir.join(format!("{name}.mdb")))
.with_desktop_media_paths()
.build()
.unwrap();
(col, tempdir)