mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-05-18 14:51:50 +02:00
Avoid keeping invalid files
This commit is contained in:
@@ -1,6 +1,16 @@
|
|||||||
#!/usr/bin/with-contenv bashio
|
#!/usr/bin/with-contenv bashio
|
||||||
# shellcheck shell=bash
|
# 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
|
if [ -d "$HOME"/BirdSongs/StreamData ]; then
|
||||||
bashio::log.fatal "Container stopping, saving temporary files."
|
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 for both services to stop
|
||||||
wait
|
wait
|
||||||
|
|
||||||
# Check if there are files in StreamData and move them to /data/StreamData
|
# Create the destination directory
|
||||||
mkdir -p /data/StreamData
|
mkdir -p /data/StreamData
|
||||||
if [ "$(ls -A "$HOME"/BirdSongs/StreamData)" ]; then
|
|
||||||
if mv -v "$HOME"/BirdSongs/StreamData/* /data/StreamData/; then
|
# Move only valid WAV files under 50MB
|
||||||
bashio::log.info "Files successfully moved to /data/StreamData."
|
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
|
else
|
||||||
bashio::log.error "Failed to move files to /data/StreamData."
|
bashio::log.warning "Skipping invalid or large file: $(basename "$file")"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
fi
|
done
|
||||||
|
|
||||||
bashio::log.info "... files safe, allowing container to stop."
|
bashio::log.info "... files safe, allowing container to stop."
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user