From a566a5687b6ba0f17ed40bed75e4bffca62cd611 Mon Sep 17 00:00:00 2001 From: Matthias Ahouansou Date: Fri, 9 May 2025 17:33:06 +0100 Subject: [PATCH] fix(config): attempt to deserialize deep directory structure before flat This meant that if you had set the directory structure, than it would always deserialize as "Flat". We also migrate the newly migrated media to the deep directory structure, if configured. --- src/config/mod.rs | 2 +- src/database/mod.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index bfe4065e..95f1c76e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -527,8 +527,8 @@ impl Default for DirectoryStructure { #[derive(Deserialize)] #[serde(untagged)] enum ShadowDirectoryStructure { - Flat {}, Deep { length: NonZeroU8, depth: NonZeroU8 }, + Flat {}, } impl TryFrom for DirectoryStructure { diff --git a/src/database/mod.rs b/src/database/mod.rs index e1389bc7..30d4231a 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -459,7 +459,7 @@ impl KeyValueDatabase { } // If the database has any data, perform data migrations before starting - let latest_database_version = 17; + let latest_database_version = 18; if services().users.count()? > 0 { // MIGRATIONS @@ -1050,9 +1050,44 @@ impl KeyValueDatabase { return Err(e); } } - services().globals.bump_database_version(17)?; + services().globals.bump_database_version(18)?; - warn!("Migration: 16 -> 17 finished"); + warn!("Migration: 16 -> 18 finished"); + } + + if services().globals.database_version()? < 18 { + if let crate::config::MediaBackendConfig::FileSystem { + path, + directory_structure: crate::config::DirectoryStructure::Deep { length, depth }, + } = &services().globals.config.media.backend + { + for file in fs::read_dir(path) + .unwrap() + .filter_map(Result::ok) + .filter(|entry| { + entry.file_name().len() == 64 + && entry.path().parent().and_then(|parent| parent.to_str()) + == Some(path.as_str()) + }) + { + tokio::fs::rename( + file.path(), + services().globals.get_media_path( + path.as_str(), + &crate::config::DirectoryStructure::Deep { + length: *length, + depth: *depth, + }, + file.file_name().to_str().unwrap(), + )?, + ) + .await?; + } + } + + services().globals.bump_database_version(18)?; + + warn!("Migration: 17 -> 18 finished"); } assert_eq!(