Skip to content

Commit

Permalink
add check for byte length
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainrax committed Nov 22, 2024
1 parent 2b32209 commit 96d0774
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/save_audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, 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));
Expand All @@ -71,10 +76,11 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, 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);
Expand Down

0 comments on commit 96d0774

Please sign in to comment.