From 8fd92581dfcd126ce432b8832209f8cdf4a73ecd Mon Sep 17 00:00:00 2001 From: jakanakae-envangel Date: Wed, 22 Aug 2018 21:41:51 +0200 Subject: scope.sh: Implement image previews for archives Take the first alphabetically sorted png, jpg or gif image from the archive, or fallback to normal file listing if there are none. Supported formats: zip, cbz, rar, cbr, 7z, cb7, tar, cbt, txz, tgz, tbz2. Optional deps: tar or bsdtar (from libarchive) for cbt and t* formats, bsdtar or unzip for zip and cbz, bsdtar or unrar for rar and cbr, bsdtar for 7z and cb7. --- ranger/data/scope.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/ranger/data/scope.sh b/ranger/data/scope.sh index 25251533..ceb9e1ae 100755 --- a/ranger/data/scope.sh +++ b/ranger/data/scope.sh @@ -123,6 +123,53 @@ handle_image() { # -jpeg -tiffcompression jpeg \ # -- "${FILE_PATH}" "${IMAGE_CACHE_PATH%.*}" \ # && exit 6 || exit 1;; + + # Archive + application/zip|application/x-rar|application/x-7z-compressed|\ + application/x-xz|application/x-bzip2|application/x-gzip|application/x-tar) + local fn="" + local bsd="" + local zip=""; local rar=""; local z7=""; local tar="" + case "${MIMETYPE}" in + application/zip) zip=1 ;; + application/x-rar) rar=1 ;; + application/x-7z-compressed) z7=1 ;; + *) tar=1 ;; + esac + [ "$tar" ] && fn=$(tar --list --file "${FILE_PATH}") + [ $? != 0 ] && bsd=1 && fn=$(bsdtar --list --file "${FILE_PATH}") + [ $? != 0 ] && [ "$bsd" ] && bsd="" && [ "$tar" -o "$z7"] && return + if [ -z "$bsd" ] && [ -z "$tar" ]; then + if [ "$rar" ]; then + fn=$(unrar lb -p- -- "${FILE_PATH}") + elif [ "$zip" ]; then + fn=$(zipinfo -1 -- "${FILE_PATH}") + fi + [ $? != 0 ] && return + fi + + fn=$(echo -n "$fn" | grep '\.\(png\|jpe\?g\|gif\)$' | sort -V | head -n 1) + [ "$fn" = "" ] && return + [ "$bsd" ] && fn=$(printf '%b' "$fn") + + if [ "$tar" ] && [ -z "$bsd" ]; then + tar --extract --to-stdout --file "${FILE_PATH}" "$fn" > \ + "${IMAGE_CACHE_PATH}" && exit 6 + rm -- "${IMAGE_CACHE_PATH}" + return + fi + # bsdtar and unzip need escaiping + fne=$(echo -n "$fn" | sed 's/[][*?\]/\\\0/g') + bsdtar --extract --to-stdout --file "${FILE_PATH}" "$fne" > \ + "${IMAGE_CACHE_PATH}" && exit 6 + rm -- "${IMAGE_CACHE_PATH}" + if [ "$rar" ]; then + unrar p -p- -inul -- "${FILE_PATH}" "$fn" > "${IMAGE_CACHE_PATH}" && exit 6 + elif [ "$zip" ]; then + unzip -pP "" -- "${FILE_PATH}" "$fne" > "${IMAGE_CACHE_PATH}" && exit 6 + fi + rm -- "${IMAGE_CACHE_PATH}" + ;; esac } -- cgit 1.4.1-2-gfad0