From 9d452acc28f3ca329912b5479f78e28683f6ce5c Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Fri, 12 Apr 2024 21:55:29 -0400 Subject: [PATCH] Fix ID population --- backend/community_settings_srv/Cargo.lock | 2 +- backend/community_settings_srv/Cargo.toml | 2 +- .../src/api/save_setting.rs | 4 +- .../community_settings_srv/src/file_util.rs | 40 +++++++++++++++++++ backend/community_settings_srv/src/main.rs | 4 +- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/backend/community_settings_srv/Cargo.lock b/backend/community_settings_srv/Cargo.lock index 1cf7fd3..af16b79 100644 --- a/backend/community_settings_srv/Cargo.lock +++ b/backend/community_settings_srv/Cargo.lock @@ -438,7 +438,7 @@ dependencies = [ [[package]] name = "community_settings_srv" -version = "0.1.0" +version = "0.1.1" dependencies = [ "actix-web", "clap", diff --git a/backend/community_settings_srv/Cargo.toml b/backend/community_settings_srv/Cargo.toml index 37cb95c..01baf76 100644 --- a/backend/community_settings_srv/Cargo.toml +++ b/backend/community_settings_srv/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "community_settings_srv" -version = "0.1.0" +version = "0.1.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/backend/community_settings_srv/src/api/save_setting.rs b/backend/community_settings_srv/src/api/save_setting.rs index 1b2bdca..6b3d96e 100644 --- a/backend/community_settings_srv/src/api/save_setting.rs +++ b/backend/community_settings_srv/src/api/save_setting.rs @@ -18,7 +18,7 @@ pub async fn save_setting_handler( Err(_e) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "too many bytes in payload")), }; let next_id = file_util::next_setting_id(&cli.folder); - let parsed_data: community_settings_core::v1::Metadata = if super::is_mime_type_ron_capable(&content_type) { + let mut parsed_data: community_settings_core::v1::Metadata = if super::is_mime_type_ron_capable(&content_type) { // Parse as RON match ron::de::from_reader(bytes.as_ref()) { Ok(x) => x, @@ -42,6 +42,8 @@ pub async fn save_setting_handler( } } }; + // Override the ID with the one used by this server + parsed_data.id = next_id.to_string(); // TODO validate user and app id // Reject blocked users and apps let path_ron = file_util::setting_path_by_id(&cli.folder, next_id, file_util::RON_EXTENSION); diff --git a/backend/community_settings_srv/src/file_util.rs b/backend/community_settings_srv/src/file_util.rs index 3050739..627a9fc 100644 --- a/backend/community_settings_srv/src/file_util.rs +++ b/backend/community_settings_srv/src/file_util.rs @@ -101,6 +101,46 @@ fn make_symlinks_absolute_in_dir(root: impl AsRef, dir: impl AsRef) Ok(()) } +pub fn sync_ids(root: impl AsRef) -> std::io::Result<()> { + for dir_entry in root.as_ref() + .join(SETTING_FOLDER) + .join(ID_FOLDER) + .read_dir()? { + let dir_entry = dir_entry?; + let f_path = dir_entry.path(); + if let Some(ext) = f_path.extension() { + let id = f_path.file_stem().map(|os| os.to_string_lossy().to_string()).unwrap(); + if ext == RON_EXTENSION { + let reader = std::io::BufReader::new(std::fs::File::open(&f_path)?); + let mut setting: community_settings_core::v1::Metadata = match ron::de::from_reader(reader) { + Ok(x) => x, + Err(e) => { + log::debug!("Error while reading {}: {}", f_path.display(), e); + let e_msg = format!("{}", e); + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e_msg)); + } + }; + setting.id = id; + ron::ser::to_writer(std::fs::File::create(&f_path)?, &setting).unwrap(); + } else if ext == JSON_EXTENSION { + let reader = std::io::BufReader::new(std::fs::File::open(&f_path)?); + let mut setting: community_settings_core::v1::Metadata = match serde_json::from_reader(reader) { + Ok(x) => x, + Err(e) => { + log::debug!("Error while reading {}: {}", f_path.display(), e); + let e_msg = format!("{}", e); + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e_msg)); + } + }; + setting.id = id; + serde_json::to_writer(std::fs::File::create(&f_path)?, &setting).unwrap(); + } + } + + } + Ok(()) +} + pub fn filename(id: u128, ext: &str) -> String { format!("{}.{}", id, ext) } diff --git a/backend/community_settings_srv/src/main.rs b/backend/community_settings_srv/src/main.rs index a2dd153..0f4e40c 100644 --- a/backend/community_settings_srv/src/main.rs +++ b/backend/community_settings_srv/src/main.rs @@ -31,8 +31,10 @@ async fn main() -> std::io::Result<()> { // fix things if args.fix { - log::debug!("Fixing old symlinks"); + log::info!("Fixing old symlinks"); file_util::fix_symlinks(&args.folder)?; + log::info!("Resynchronizing file IDs with file name IDs"); + file_util::sync_ids(&args.folder)?; return Ok(()) }