From 63fd70ad34ccda16b93d86374afc22d59a7f402c Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 18 May 2023 11:02:02 +1000 Subject: [PATCH] Add a .with_desktop_media_paths() helper --- rslib/src/collection/mod.rs | 12 ++++++++++++ rslib/src/import_export/package/colpkg/tests.rs | 5 +---- rslib/src/sync/collection/tests.rs | 4 ++-- rslib/src/tests.rs | 5 +---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/rslib/src/collection/mod.rs b/rslib/src/collection/mod.rs index 4967dd419..6f3e859ca 100644 --- a/rslib/src/collection/mod.rs +++ b/rslib/src/collection/mod.rs @@ -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 diff --git a/rslib/src/import_export/package/colpkg/tests.rs b/rslib/src/import_export/package/colpkg/tests.rs index e798b2538..1fe5192e5 100644 --- a/rslib/src/import_export/package/colpkg/tests.rs +++ b/rslib/src/import_export/package/colpkg/tests.rs @@ -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 { 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(); diff --git a/rslib/src/sync/collection/tests.rs b/rslib/src/sync/collection/tests.rs index eba4c52a5..a2e416f09 100644 --- a/rslib/src/sync/collection/tests.rs +++ b/rslib/src/sync/collection/tests.rs @@ -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() } diff --git a/rslib/src/tests.rs b/rslib/src/tests.rs index 744d76cc4..de3251749 100644 --- a/rslib/src/tests.rs +++ b/rslib/src/tests.rs @@ -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)