From 96d077445ed639bcfccd317faa59deb876bb6f3b Mon Sep 17 00:00:00 2001 From: Patrick Baxter Date: Fri, 22 Nov 2024 15:31:44 +1300 Subject: [PATCH] add check for byte length --- src/save_audio.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/save_audio.rs b/src/save_audio.rs index 8ab8c07..bd5edea 100644 --- a/src/save_audio.rs +++ b/src/save_audio.rs @@ -50,16 +50,21 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec, device_config: DeviceCo move |_| { // Reclaim some memory audio_bytes.shrink_to_fit(); + if audio_bytes.len() < 12 { + error!("Audio data too short: {} bytes", audio_bytes.len()); + return; + } let timestamp = LittleEndian::read_u64(&audio_bytes[2..10]); let recording_date_time = DateTime::from_timestamp_millis(timestamp as i64 / 1000) .unwrap_or(chrono::Local::now().with_timezone(&Utc)) .with_timezone(&chrono::Local); info!("Saving AAC file"); - if !fs::metadata(&output_dir).is_ok() { + if fs::metadata(&output_dir).is_err() { fs::create_dir_all(&output_dir) .expect(&format!("Failed to create AAC output directory {}", output_dir)); } let debug_dir = String::from("/home/pi/temp"); + // Separate debug directory creation from raw file writing if !fs::exists(&debug_dir).unwrap_or(false) { fs::create_dir(&debug_dir) .expect(&format!("Failed to create debug output directory {}", debug_dir)); @@ -71,10 +76,11 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec, device_config: DeviceCo fs::write(&output_path, &audio_bytes).unwrap(); } + let output_path: String = format!("{}/{}.aac", output_dir, recording_date_time.format("%Y-%m-%d--%H-%M-%S")); // If the file already exists, don't re-save it. - if !fs::metadata(&output_path).is_ok() { + if fs::metadata(&output_path).is_err() { let recording_date_time = format!("recordingDateTime={}", recording_date_time.to_rfc3339()); let latitude = format!("latitude={}", device_config.lat_lng().0);