Code simplification, move to wizard style UI, JSON file cache (scans every 15 mins)

This commit is contained in:
markmental 2026-03-02 13:45:28 -05:00
commit 92362efd47
11 changed files with 1027 additions and 438 deletions

View file

@ -6,6 +6,8 @@ USER=""
PASS=""
DOCROOT=""
MEDIA_ROOT_ARG=""
MEDIA_CACHE_DIR_ARG=""
MEDIA_CACHE_INTERVAL_ARG=""
read_pass_stdin=false
prompt=false
@ -14,7 +16,8 @@ usage() {
cat <<'EOF'
Usage:
start-media-server.sh --user <username> [--pass <password> | --pass-stdin | --prompt]
[--media-root <path>] [--port <port>] [--dir <docroot>]
[--media-root <path>] [--cache-dir <path>] [--cache-interval <seconds>]
[--port <port>] [--dir <docroot>]
Options:
--user <u> Username to set in MEDIA_USER (required)
@ -22,12 +25,15 @@ Options:
--pass-stdin Read password from stdin (safer)
--prompt Prompt for password (safer; hidden input)
--media-root <p> Base directory containing Videos/ and Music/ (recommended)
--cache-dir <p> Directory where media cache JSON and lock files are stored
--cache-interval <s>
Cache rebuild interval in seconds (default: 900)
--port <p> Listen port (default: 9000)
--dir <path> cd into docroot before starting (optional)
-h, --help Show help
Examples:
./start-media-server.sh --user admin --prompt --media-root /mnt/media --port 9000 --dir /home/me/samba-serv
./start-media-server.sh --user admin --prompt --media-root /mnt/media --cache-dir var/cache/media --port 9000 --dir /home/me/samba-serv
printf '%s\n' 'test123' | ./start-media-server.sh --user admin --pass-stdin --media-root /mnt/media
EOF
}
@ -41,7 +47,7 @@ pick_php_runner() {
fi
if have_cmd frankenphp; then
if frankenphp php-cli -v >/dev/null 2>&1; then
if frankenphp -v >/dev/null 2>&1; then
echo "frankenphp php-cli"
return 0
fi
@ -57,6 +63,8 @@ while (($#)); do
--pass-stdin) read_pass_stdin=true; shift ;;
--prompt) prompt=true; shift ;;
--media-root) MEDIA_ROOT_ARG="${2-}"; shift 2 ;;
--cache-dir) MEDIA_CACHE_DIR_ARG="${2-}"; shift 2 ;;
--cache-interval) MEDIA_CACHE_INTERVAL_ARG="${2-}"; shift 2 ;;
--port) PORT="${2-}"; shift 2 ;;
--dir) DOCROOT="${2-}"; shift 2 ;;
-h|--help) usage; exit 0 ;;
@ -97,6 +105,13 @@ if ! [[ "$PORT" =~ ^[0-9]+$ ]] || ((PORT < 1 || PORT > 65535)); then
exit 2
fi
if [[ -n "$MEDIA_CACHE_INTERVAL_ARG" ]]; then
if ! [[ "$MEDIA_CACHE_INTERVAL_ARG" =~ ^[0-9]+$ ]] || ((MEDIA_CACHE_INTERVAL_ARG < 1)); then
echo "Error: invalid --cache-interval '$MEDIA_CACHE_INTERVAL_ARG'" >&2
exit 2
fi
fi
if ! have_cmd frankenphp; then
echo "Error: frankenphp not found in PATH" >&2
exit 127
@ -120,6 +135,14 @@ if [[ -n "${MEDIA_ROOT_ARG}" ]]; then
export MEDIA_ROOT="${MEDIA_ROOT_ARG}"
fi
if [[ -n "${MEDIA_CACHE_DIR_ARG}" ]]; then
export MEDIA_CACHE_DIR="${MEDIA_CACHE_DIR_ARG}"
fi
if [[ -n "${MEDIA_CACHE_INTERVAL_ARG}" ]]; then
export MEDIA_CACHE_INTERVAL="${MEDIA_CACHE_INTERVAL_ARG}"
fi
if [[ -n "$DOCROOT" ]]; then
cd "$DOCROOT"
fi
@ -133,7 +156,49 @@ if [[ -n "${MEDIA_ROOT:-}" ]]; then
else
echo "MEDIA_ROOT not set (get_files.php / serve_media.php will error)"
fi
if [[ -n "${MEDIA_CACHE_DIR:-}" ]]; then
echo "MEDIA_CACHE_DIR=$MEDIA_CACHE_DIR"
else
echo "MEDIA_CACHE_DIR=$(pwd)/var/cache/media"
fi
echo "MEDIA_CACHE_INTERVAL=${MEDIA_CACHE_INTERVAL:-900}s"
echo
exec frankenphp php-server --listen ":$PORT"
run_cache_builder() {
if $PHP_RUNNER "$(pwd)/build_media_cache.php"; then
return 0
fi
echo "Warning: media cache build failed" >&2
return 1
}
cache_loop() {
local interval="${MEDIA_CACHE_INTERVAL:-900}"
while true; do
run_cache_builder || true
sleep "$interval"
done
}
cleanup() {
if [[ -n "${CACHE_LOOP_PID:-}" ]]; then
kill "${CACHE_LOOP_PID}" >/dev/null 2>&1 || true
wait "${CACHE_LOOP_PID}" >/dev/null 2>&1 || true
fi
if [[ -n "${SERVER_PID:-}" ]]; then
kill "${SERVER_PID}" >/dev/null 2>&1 || true
fi
}
run_cache_builder || true
cache_loop &
CACHE_LOOP_PID=$!
trap cleanup EXIT INT TERM
frankenphp php-server --listen ":$PORT" &
SERVER_PID=$!
wait "$SERVER_PID"