diff --git a/birdnet-pi/rootfs/etc/cont-finish.d/savestreamdata.sh b/birdnet-pi/rootfs/etc/cont-finish.d/savestreamdata.sh index de9eaae80..662817120 100755 --- a/birdnet-pi/rootfs/etc/cont-finish.d/savestreamdata.sh +++ b/birdnet-pi/rootfs/etc/cont-finish.d/savestreamdata.sh @@ -1,6 +1,16 @@ #!/usr/bin/with-contenv bashio # shellcheck shell=bash +# Maximum file size in bytes (50MB) +MAX_SIZE=$((50 * 1024 * 1024)) + +# Function to check if a file is a valid WAV +is_valid_wav() { + local file="$1" + # Check if the file contains a valid WAV header + file "$file" | grep -qE 'WAVE audio' +} + if [ -d "$HOME"/BirdSongs/StreamData ]; then bashio::log.fatal "Container stopping, saving temporary files." @@ -18,16 +28,22 @@ if [ -d "$HOME"/BirdSongs/StreamData ]; then # Wait for both services to stop wait - # Check if there are files in StreamData and move them to /data/StreamData + # Create the destination directory mkdir -p /data/StreamData - if [ "$(ls -A "$HOME"/BirdSongs/StreamData)" ]; then - if mv -v "$HOME"/BirdSongs/StreamData/* /data/StreamData/; then - bashio::log.info "Files successfully moved to /data/StreamData." + + # Move only valid WAV files under 50MB + shopt -s nullglob # Prevent errors if no files match + for file in "$HOME"/BirdSongs/StreamData/*.wav; do + if [ -f "$file" ] && [ "$(stat --format="%s" "$file")" -lt "$MAX_SIZE" ] && is_valid_wav "$file"; then + if mv -v "$file" /data/StreamData/; then + bashio::log.info "Moved valid WAV file: $(basename "$file")" + else + bashio::log.error "Failed to move: $(basename "$file")" + fi else - bashio::log.error "Failed to move files to /data/StreamData." - exit 1 + bashio::log.warning "Skipping invalid or large file: $(basename "$file")" fi - fi + done bashio::log.info "... files safe, allowing container to stop." else