From 79929d5009142cf545b05bdf9d9d3016c3ed7c51 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Thu, 23 Apr 2026 04:37:30 +0530 Subject: storing a temp file for each image preview generated, for lf file preview script --- modules/scripts/file-preview.sh | 59 ++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'modules') diff --git a/modules/scripts/file-preview.sh b/modules/scripts/file-preview.sh index 1f884ee..9af781d 100644 --- a/modules/scripts/file-preview.sh +++ b/modules/scripts/file-preview.sh @@ -13,12 +13,15 @@ X="$4" Y="$5" # Temp dir -TMP_DIR="/tmp/lf-preview-$(id -u)" +TMP_DIR="/tmp/preview-$(id -u)" mkdir -p "$TMP_DIR" -TMP_IMG="$TMP_DIR/pv-$(basename "$FILE_PATH" | tr -c '[:alnum:]' '_').png" + +# Unique temp file per actual file using inode +INODE=$(stat -c '%i' "$FILE_PATH" 2>/dev/null || echo "0") # Render image via kitty render_image() { + kitten icat --clear --stdin no --transfer-mode memory --place "${w}x${h}@${x}x${y}" /dev/null /dev/tty kitten icat --stdin no --transfer-mode memory --place "${w}x${h}@${x}x${y}" "$1" /dev/tty exit 1 } @@ -33,58 +36,67 @@ y="$5" [ ! -r "$FILE_PATH" ] && exit 0 MIMETYPE=$(file --dereference --brief --mime-type "$FILE_PATH") - case "$MIMETYPE" in # --- IMAGES --- image/svg+xml) - rsvg-convert "$FILE_PATH" -o "$TMP_IMG" >/dev/null 2>&1 && render_image "$TMP_IMG" + TMP_IMG="$TMP_DIR/svg-${INODE}.png" + [ ! -f "$TMP_IMG" ] && rsvg-convert "$FILE_PATH" -o "$TMP_IMG" >/dev/null 2>&1 + [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" ;; image/*) render_image "$FILE_PATH" ;; - # --- VIDEOS --- video/*) - if command -v ffmpegthumbnailer >/dev/null; then - ffmpegthumbnailer -i "$FILE_PATH" -o "$TMP_IMG" -s 0 -q 5 >/dev/null 2>&1 - else - "$FFMPEG" -y -i "$FILE_PATH" -ss 00:00:01 -vframes 1 "$TMP_IMG" >/dev/null 2>&1 + TMP_IMG="$TMP_DIR/vid-${INODE}.png" + if [ ! -f "$TMP_IMG" ]; then + if command -v ffmpegthumbnailer >/dev/null; then + ffmpegthumbnailer -i "$FILE_PATH" -o "$TMP_IMG" -s 0 -q 5 >/dev/null 2>&1 + else + "$FFMPEG" -y -i "$FILE_PATH" -ss 00:00:01 -vframes 1 "$TMP_IMG" >/dev/null 2>&1 + fi fi [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" ;; - # --- AUDIO --- audio/*) - "$FFMPEG" -y -i "$FILE_PATH" \ - -filter_complex "showwavespic=s=${WIDTH}x${HEIGHT}:colors=white" \ - -frames:v 1 "$TMP_IMG" >/dev/null 2>&1 - + TMP_IMG="$TMP_DIR/aud-${INODE}.png" + if [ ! -f "$TMP_IMG" ]; then + "$FFMPEG" -y -i "$FILE_PATH" \ + -filter_complex "showwavespic=s=${WIDTH}x${HEIGHT}:colors=white" \ + -frames:v 1 "$TMP_IMG" >/dev/null 2>&1 + fi "$FFPROBE" -hide_banner -v quiet -show_format -show_streams "$FILE_PATH" - [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" exit 0 ;; # --- PDF --- application/pdf) - "$PDFTOPPM" -f 1 -l 1 -png -singlefile "$FILE_PATH" "${TMP_IMG%.png}" >/dev/null 2>&1 + TMP_IMG="$TMP_DIR/pdf-${INODE}.png" + [ ! -f "$TMP_IMG" ] && "$PDFTOPPM" -f 1 -l 1 -png -singlefile "$FILE_PATH" "${TMP_IMG%.png}" >/dev/null 2>&1 [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" ;; # --- EPUB / MOBI --- application/epub+zip|application/x-mobipocket-ebook) - "$OUCH" decompress "$FILE_PATH" --limit "*/cover*.jpg" --dir /tmp && mv /tmp/cover*.jpg "$TMP_IMG" + TMP_IMG="$TMP_DIR/epub-${INODE}.png" + if [ ! -f "$TMP_IMG" ]; then + "$OUCH" decompress "$FILE_PATH" --limit "*/cover*.jpg" --dir "$TMP_DIR" >/dev/null 2>&1 + mv "$TMP_DIR"/cover*.jpg "$TMP_IMG" 2>/dev/null + fi [ -s "$TMP_IMG" ] && render_image "$TMP_IMG" ;; # --- OFFICE --- application/vnd.openxmlformats-officedocument.*|application/msword|application/vnd.oasis.opendocument.*) - libreoffice --headless --convert-to pdf --outdir "$TMP_DIR" "$FILE_PATH" >/dev/null 2>&1 - PDF_VER="${TMP_DIR}/$(basename "${FILE_PATH%.*}").pdf" - if [ -f "$PDF_VER" ]; then - "$PDFTOPPM" -f 1 -l 1 -png -singlefile "$PDF_VER" "${TMP_IMG%.png}" >/dev/null 2>&1 - render_image "$TMP_IMG" + TMP_IMG="$TMP_DIR/doc-${INODE}.png" + if [ ! -f "$TMP_IMG" ]; then + libreoffice --headless --convert-to pdf --outdir "$TMP_DIR" "$FILE_PATH" >/dev/null 2>&1 + PDF_VER="${TMP_DIR}/$(basename "${FILE_PATH%.*}").pdf" + [ -f "$PDF_VER" ] && "$PDFTOPPM" -f 1 -l 1 -png -singlefile "$PDF_VER" "${TMP_IMG%.png}" >/dev/null 2>&1 fi + [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" ;; # --- ARCHIVES --- @@ -95,7 +107,7 @@ case "$MIMETYPE" in # --- TEXT / CODE --- text/*|application/json|application/javascript|application/xml) - "$BAT" --color=always --style=numbers --line-range :200 "$FILE_PATH" + "$BAT" --color=always --style=numbers --line-range :200 "$FILE_PATH" 2>/dev/null exit 0 ;; @@ -114,5 +126,4 @@ case "$MIMETYPE" in ;; esac -# Always exit cleanly exit 0 -- cgit v1.2.3