delete current image if upload is outdated

This commit is contained in:
2025-08-16 21:50:56 +02:00
parent 0d101dec1d
commit e78a5eae0b

View File

@ -5,12 +5,18 @@ CURRENT_DIR="$BASE_DIR/current"
mkdir -p "$CURRENT_DIR"
# Store last SHA256 checksums for each camera
# Store last SHA256 checksums and last update timestamps for each camera
declare -A LAST_SHA
declare -A LAST_UPDATE
# How long to wait before deleting stale images (in seconds)
STALE_TIMEOUT=$((15 * 60))
echo "[Watcher] Starting to monitor subfolders in $BASE_DIR ..."
while true; do
NOW=$(date +%s)
for CAM_DIR in "$BASE_DIR"/msvcam*; do
[ -d "$CAM_DIR" ] || continue
@ -18,7 +24,9 @@ while true; do
OUT_FILE="$CURRENT_DIR/${CAM_NAME}.avif"
# Find the newest image
NEWEST_FILE=$(find "$CAM_DIR" -type f -iregex ".*\.\(jpg\|jpeg\|png\|bmp\|tif\|tiff\)" -printf "%T@ %p\n" | sort -n | tail -1 | awk '{print $2}')
NEWEST_FILE=$(find "$CAM_DIR" -type f \
\( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.bmp" -o -iname "*.tif" -o -iname "*.tiff" \) \
-printf "%T@ %p\n" 2>/dev/null | sort -n | tail -1 | awk '{print $2}')
if [ -n "$NEWEST_FILE" ]; then
# Calculate SHA256 of the newest image
@ -27,13 +35,33 @@ while true; do
# Check if this file has already been converted
if [ "${LAST_SHA[$CAM_NAME]}" != "$SHA" ]; then
echo "[Watcher] New or changed file detected for $CAM_NAME: $NEWEST_FILE"
avifenc "$NEWEST_FILE" "$OUT_FILE"
sync
LAST_SHA[$CAM_NAME]="$SHA"
if avifenc "$NEWEST_FILE" "$OUT_FILE"; then
sync
LAST_SHA[$CAM_NAME]="$SHA"
LAST_UPDATE[$CAM_NAME]=$NOW
echo "[Watcher] Conversion successful: $OUT_FILE"
else
echo "[Watcher][ERROR] Conversion failed for $NEWEST_FILE"
fi
else
echo "[Watcher] No change detected for $CAM_NAME, skipping conversion."
fi
fi
# Check for staleness
if [ -n "${LAST_UPDATE[$CAM_NAME]}" ]; then
ELAPSED=$((NOW - LAST_UPDATE[$CAM_NAME]))
if [ $ELAPSED -ge $STALE_TIMEOUT ]; then
if [ -f "$OUT_FILE" ]; then
echo "[Watcher] No new image for $CAM_NAME in $((STALE_TIMEOUT/60)) minutes. Removing $OUT_FILE"
rm -f "$OUT_FILE"
fi
fi
fi
done
sleep 5
# Sleep before next check
SLEEP_INTERVAL="${SLEEP_INTERVAL:-5}"
sleep "$SLEEP_INTERVAL"
done