From 638f7e3dd94332409af6bc2bd80a12aa2017a600 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sat, 24 Oct 2026 22:08:49 +0200 Subject: [PATCH] Run discover-overlay-wait as a user unit and rework the vidthumb cache vidthumb names thumbnails by a hash of path, size and mtime instead of a jq index that broke on quotes in filenames; systemd-tmpfiles removes ones unused for 30 days. discover-overlay-wait now runs from a systemd user unit started by xmonad and logs to the journal. --- .../user/discover-overlay-wait.service | 7 +++ .config/user-tmpfiles.d/vidthumb.conf | 2 + .config/xmonad/xmonad.hs | 2 +- .local/bin/discover-overlay-wait | 5 +- .local/bin/vidthumb | 57 ++++++++----------- 5 files changed, 39 insertions(+), 34 deletions(-) create mode 100644 .config/systemd/user/discover-overlay-wait.service create mode 100644 .config/user-tmpfiles.d/vidthumb.conf diff --git a/.config/systemd/user/discover-overlay-wait.service b/.config/systemd/user/discover-overlay-wait.service new file mode 100644 index 0000000..0b3f03e --- /dev/null +++ b/.config/systemd/user/discover-overlay-wait.service @@ -0,0 +1,7 @@ +[Unit] +Description=Run discover-overlay while Discord is running + +[Service] +ExecStart=%h/.local/bin/discover-overlay-wait +Restart=on-failure +RestartSec=5 diff --git a/.config/user-tmpfiles.d/vidthumb.conf b/.config/user-tmpfiles.d/vidthumb.conf new file mode 100644 index 0000000..5c65cf8 --- /dev/null +++ b/.config/user-tmpfiles.d/vidthumb.conf @@ -0,0 +1,2 @@ +# Remove vidthumb thumbnails (and leftover temp files) not used for 30 days; %C is $XDG_CACHE_HOME. +e %C/vidthumb - - - 30d diff --git a/.config/xmonad/xmonad.hs b/.config/xmonad/xmonad.hs index f84a330..df48f03 100644 --- a/.config/xmonad/xmonad.hs +++ b/.config/xmonad/xmonad.hs @@ -274,7 +274,7 @@ myStartupHook = do spawnOnce "numlockx" spawnOnce "blueman-applet" spawnOnce "adb start-server" - spawnOnce "/home/fuxino/.local/bin/discover-overlay-wait" + spawnOnce "systemctl --user start discover-overlay-wait" -- Manage hook myManageHook :: ManageHook diff --git a/.local/bin/discover-overlay-wait b/.local/bin/discover-overlay-wait index 254853b..c5689a5 100755 --- a/.local/bin/discover-overlay-wait +++ b/.local/bin/discover-overlay-wait @@ -1,4 +1,7 @@ #!/bin/sh +# Runs discover-overlay while Discord's RPC port is up and stops it when Discord exits. +# Run by discover-overlay-wait.service (started from xmonad); output goes to the journal. + port_up() { ss -ltn | grep -qE '127\.0\.0\.1:64[67][0-9]' } @@ -8,7 +11,7 @@ while true; do sleep 5 port_up || continue # port vanished again, Discord was still restarting - discover-overlay >> /tmp/discover-overlay.log 2>&1 & + discover-overlay & pid=$! while port_up && kill -0 "$pid" 2>/dev/null; do sleep 3; done diff --git a/.local/bin/vidthumb b/.local/bin/vidthumb index 2266e73..7649433 100755 --- a/.local/bin/vidthumb +++ b/.local/bin/vidthumb @@ -1,36 +1,29 @@ -#!/usr/bin/env bash +#!/bin/bash +# Prints a cached thumbnail for the video in $1 (used by the lf preview), creating it if needed. +# Thumbnails are named by a hash of path, size and mtime, so a changed file gets a new one. +# Hits touch the file; systemd-tmpfiles (~/.config/user-tmpfiles.d/vidthumb.conf) removes unused ones. -if ! [ -f "$1" ]; then - exit 1 +[[ -f $1 ]] || exit 1 + +cache="${XDG_CACHE_HOME:-$HOME/.cache}/vidthumb" +video=$(realpath -- "$1") || exit 1 +stamp=$(stat -c '%s.%Y' -- "$video") || exit 1 +key=$(printf '%s\0%s' "$video" "$stamp" | sha256sum) +thumb="$cache/${key%% *}.jpg" + +if [[ -s $thumb ]]; then + touch -- "$thumb" + printf '%s\n' "$thumb" + exit 0 fi -cache="$HOME/.cache/vidthumb" -index="$cache/index.json" -movie="$(realpath "$1")" - -mkdir -p "$cache" - -if [ -f "$index" ]; then - thumbnail="$(jq -r ". \"$movie\"" <"$index")" - if [[ "$thumbnail" != "null" ]]; then - if [[ ! -f "$cache/$thumbnail" ]]; then - exit 1 - fi - echo "$cache/$thumbnail" - exit 0 - fi +mkdir -p -- "$cache" || exit 1 +# Write to a temp file first, so an interrupted or concurrent run never leaves a broken thumbnail. +tmp=$(mktemp --suffix=.jpg "$cache/.tmp.XXXXXX") || exit 1 +if ffmpegthumbnailer -i "$video" -o "$tmp" -s 0 2> /dev/null && [[ -s $tmp ]]; then + mv -f -- "$tmp" "$thumb" + printf '%s\n' "$thumb" +else + rm -f -- "$tmp" + exit 1 fi - -thumbnail="$(uuidgen).jpg" - -if ! ffmpegthumbnailer -i "$movie" -o "$cache/$thumbnail" -s 0 2>/dev/null; then - exit 1 -fi - -if [[ ! -f "$index" ]]; then - echo "{\"$movie\": \"$thumbnail\"}" >"$index" -fi -json="$(jq -r --arg "$movie" "$thumbnail" ". + {\"$movie\": \"$thumbnail\"}" <"$index")" -echo "$json" >"$index" - -echo "$cache/$thumbnail"