talk2me/maintenance.sh
Adolfo Delorenzo 343bfbf1de Fix temporary file accumulation to prevent disk space exhaustion
Automatic Cleanup System:
- Background thread cleans files older than 5 minutes every minute
- Tracks all temporary files in a registry with creation timestamps
- Automatic cleanup on app shutdown with atexit handler
- Orphaned file detection and removal
- Thread-safe cleanup implementation

File Management:
- Unique filenames with timestamps prevent collisions
- Configurable upload folder via UPLOAD_FOLDER environment variable
- Automatic folder creation with proper permissions
- Fallback to system temp if primary folder fails
- File registration for all uploads and generated audio

Health Monitoring:
- /health/storage endpoint shows temp file statistics
- Tracks file count, total size, oldest file age
- Disk space monitoring and warnings
- Real-time cleanup status information
- Warning when files exceed thresholds

Administrative Tools:
- maintenance.sh script for manual operations
- Status checking, manual cleanup, real-time monitoring
- /admin/cleanup endpoint for emergency cleanup (requires auth token)
- Configurable retention period (default 5 minutes)

Security Improvements:
- Filename sanitization in get_audio endpoint
- Directory traversal prevention
- Cache headers to reduce repeated downloads
- Proper file existence checks

Performance:
- Efficient batch cleanup operations
- Minimal overhead with background thread
- Smart registry management
- Automatic garbage collection after operations

This prevents disk space exhaustion by ensuring temporary files are
automatically cleaned up after use, with multiple failsafes.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-02 23:27:59 -06:00

117 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# Maintenance script for Talk2Me application
# This script helps manage temporary files and disk space
UPLOAD_FOLDER="${UPLOAD_FOLDER:-/tmp/talk2me_uploads}"
MAX_AGE_MINUTES=5
echo "Talk2Me Maintenance Script"
echo "========================="
# Function to check disk usage
check_disk_usage() {
echo -e "\nDisk Usage:"
df -h "$UPLOAD_FOLDER" 2>/dev/null || df -h /tmp
}
# Function to show temp file stats
show_temp_stats() {
echo -e "\nTemporary File Statistics:"
if [ -d "$UPLOAD_FOLDER" ]; then
file_count=$(find "$UPLOAD_FOLDER" -type f 2>/dev/null | wc -l)
total_size=$(du -sh "$UPLOAD_FOLDER" 2>/dev/null | cut -f1)
echo " Upload folder: $UPLOAD_FOLDER"
echo " File count: $file_count"
echo " Total size: ${total_size:-0}"
if [ $file_count -gt 0 ]; then
echo -e "\n Oldest files:"
find "$UPLOAD_FOLDER" -type f -printf '%T+ %p\n' 2>/dev/null | sort | head -5
fi
else
echo " Upload folder does not exist: $UPLOAD_FOLDER"
fi
}
# Function to clean old temp files
clean_temp_files() {
echo -e "\nCleaning temporary files older than $MAX_AGE_MINUTES minutes..."
if [ -d "$UPLOAD_FOLDER" ]; then
# Count files before cleanup
before_count=$(find "$UPLOAD_FOLDER" -type f 2>/dev/null | wc -l)
# Remove old files
find "$UPLOAD_FOLDER" -type f -mmin +$MAX_AGE_MINUTES -delete 2>/dev/null
# Count files after cleanup
after_count=$(find "$UPLOAD_FOLDER" -type f 2>/dev/null | wc -l)
removed=$((before_count - after_count))
echo " Removed $removed files"
else
echo " Upload folder does not exist: $UPLOAD_FOLDER"
fi
}
# Function to setup upload folder
setup_upload_folder() {
echo -e "\nSetting up upload folder..."
if [ ! -d "$UPLOAD_FOLDER" ]; then
mkdir -p "$UPLOAD_FOLDER"
chmod 755 "$UPLOAD_FOLDER"
echo " Created: $UPLOAD_FOLDER"
else
echo " Exists: $UPLOAD_FOLDER"
fi
}
# Function to monitor in real-time
monitor_realtime() {
echo -e "\nMonitoring temporary files (Press Ctrl+C to stop)..."
while true; do
clear
echo "Talk2Me File Monitor - $(date)"
echo "================================"
show_temp_stats
check_disk_usage
sleep 5
done
}
# Main menu
case "${1:-help}" in
status)
show_temp_stats
check_disk_usage
;;
clean)
clean_temp_files
show_temp_stats
;;
setup)
setup_upload_folder
;;
monitor)
monitor_realtime
;;
all)
setup_upload_folder
clean_temp_files
show_temp_stats
check_disk_usage
;;
*)
echo "Usage: $0 {status|clean|setup|monitor|all}"
echo ""
echo "Commands:"
echo " status - Show current temp file statistics"
echo " clean - Clean old temporary files"
echo " setup - Create upload folder if needed"
echo " monitor - Real-time monitoring"
echo " all - Run setup, clean, and show status"
echo ""
echo "Environment Variables:"
echo " UPLOAD_FOLDER - Set custom upload folder (default: /tmp/talk2me_uploads)"
;;
esac