Fix ID population

This commit is contained in:
NGnius (Graham) 2024-04-12 21:55:29 -04:00
parent be60be71ac
commit 9d452acc28
5 changed files with 48 additions and 4 deletions

View file

@ -438,7 +438,7 @@ dependencies = [
[[package]]
name = "community_settings_srv"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"actix-web",
"clap",

View file

@ -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

View file

@ -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);

View file

@ -101,6 +101,46 @@ fn make_symlinks_absolute_in_dir(root: impl AsRef<Path>, dir: impl AsRef<Path>)
Ok(())
}
pub fn sync_ids(root: impl AsRef<Path>) -> 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)
}

View file

@ -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(())
}