Parsing recipe files to only send used images instead of everything

master
Wynd 2026-03-13 21:58:25 +02:00
parent a4c4b40d99
commit 35755dd0ea
6 changed files with 131 additions and 14 deletions

58
Cargo.lock generated
View File

@ -231,6 +231,8 @@ dependencies = [
"env_logger", "env_logger",
"ipnet", "ipnet",
"log", "log",
"serde",
"toml",
] ]
[[package]] [[package]]
@ -262,6 +264,16 @@ version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c"
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]] [[package]]
name = "serde_core" name = "serde_core"
version = "1.0.228" version = "1.0.228"
@ -282,6 +294,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serde_spanned"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776"
dependencies = [
"serde_core",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.1" version = "0.11.1"
@ -299,6 +320,37 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "toml"
version = "1.0.6+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "399b1124a3c9e16766831c6bba21e50192572cdd98706ea114f9502509686ffc"
dependencies = [
"serde_core",
"serde_spanned",
"toml_datetime",
"toml_parser",
"winnow",
]
[[package]]
name = "toml_datetime"
version = "1.0.0+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32c2555c699578a4f59f0cc68e5116c8d7cabbd45e1409b989d4be085b53f13e"
dependencies = [
"serde_core",
]
[[package]]
name = "toml_parser"
version = "1.0.9+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702d4415e08923e7e1ef96cd5727c0dfed80b4d2fa25db9647fe5eb6f7c5a4c4"
dependencies = [
"winnow",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.24" version = "1.0.24"
@ -325,3 +377,9 @@ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "winnow"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"

View File

@ -23,6 +23,11 @@ clap = { version = "4.5.60", features = ["derive"] }
env_logger = "0.11.9" env_logger = "0.11.9"
ipnet = "2.11.0" ipnet = "2.11.0"
log = "0.4.29" log = "0.4.29"
toml = { version = "1.0.6", default-features = false, features = [
"serde",
"parse",
] }
serde = { version = "1.0.228", features = ["derive"] }
[profile.release] [profile.release]
opt-level = "z" opt-level = "z"

View File

@ -12,12 +12,12 @@ RUN cargo build --target x86_64-unknown-linux-musl --release
FROM alpine:3.21 FROM alpine:3.21
# Create app directory # Create app directory (also cd's us into this dir)
WORKDIR /app WORKDIR /app
# Expose the 8000 port for HTTP traffic
EXPOSE 9696 EXPOSE 9696
# Copy the binary compiled in the builder image
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/recipe-sync-server-rs ./ COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/recipe-sync-server-rs ./
ENTRYPOINT [ "/app/recipe-sync-server-rs" ] ENTRYPOINT [ "/app/recipe-sync-server-rs" ]

View File

@ -9,11 +9,3 @@ services:
- ./recipes:/app/recipes - ./recipes:/app/recipes
environment: environment:
- RUST_LOG=info - RUST_LOG=info
networks:
- sync-net
networks:
sync-net:
ipam:
config:
- subnet: 172.20.0.0/24

12
justfile 100644
View File

@ -0,0 +1,12 @@
set export
set quiet
USERNAME := "pixelatedw"
IMAGE := "recipe-sync-server-rs"
VERSION := `cargo get package.version`
run path:
RUST_LOG=info cargo run -- -e {{path}}
build:
docker build -t "${USERNAME}/${IMAGE}:${VERSION}" "."

View File

@ -8,6 +8,7 @@ use std::{
}; };
use clap::Parser; use clap::Parser;
use toml::Table;
use crate::{buffer::ByteBuffer, cli::CliArgs}; use crate::{buffer::ByteBuffer, cli::CliArgs};
@ -73,8 +74,6 @@ fn handle_connection(
let files_sent = paths.len(); let files_sent = paths.len();
log::info!("Sending {files_sent} files to {remote_ip}");
buffer.write_usize(files_sent); buffer.write_usize(files_sent);
for file in paths { for file in paths {
@ -99,6 +98,9 @@ fn handle_connection(
}; };
} }
let buf_size = buffer.len();
log::info!("Sending {files_sent} files to {remote_ip} totalling {buf_size} bytes");
let _ = conn.write_all(&buffer); let _ = conn.write_all(&buffer);
let _ = conn.flush(); let _ = conn.flush();
@ -112,9 +114,57 @@ fn walk_dir<P: AsRef<Path>>(path: P, file_paths: &mut Vec<String>) {
continue; continue;
} }
let file_path = entry.path().to_str().unwrap().to_string(); if let Some(ext) = entry.path().extension()
&& ext == "md"
{
let path = entry.path();
let file_path = path.to_str();
let parent_dir = path.parent();
file_paths.push(file_path); if let Some(file_path) = file_path {
if let Some(parent_dir) = parent_dir {
parse_md_file(file_path, parent_dir, file_paths);
}
file_paths.push(file_path.to_string());
}
} else {
continue;
}
}
}
fn parse_md_file(file_path: &str, parent_dir: &Path, file_paths: &mut Vec<String>) {
// Generate a string that only contains the TOML header
let mut toml_header = String::new();
let content = fs::read_to_string(file_path).unwrap();
let lines = content.lines();
for (i, line) in lines.enumerate() {
if i > 0 && line == "+++" {
break;
} else if i == 0 {
continue;
}
toml_header.push_str(line);
toml_header.push('\n');
}
let toml_header = toml_header.as_str();
let recipe = toml_header.parse::<Table>().unwrap();
// Take all the pics found and add their paths to the map
if recipe.contains_key("pics")
&& let Some(pics) = recipe["pics"].as_array()
{
for pic in pics {
if let Some(pic) = pic.as_str() {
let mut dir = PathBuf::from(parent_dir);
dir.push(pic);
file_paths.push(dir.to_str().unwrap().to_string());
}
}
} }
} }