summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorjakanakae-envangel <jakanakaevangeli@chiru.no>2018-08-22 21:41:51 +0200
committerjakanakae-envangel <jakanakaevangeli@chiru.no>2018-08-23 18:57:15 +0200
commit8fd92581dfcd126ce432b8832209f8cdf4a73ecd (patch)
tree32093dfe46aac846d066f3ea5647216ca73874c0
parent8d4808fc6a0f36960ff9999b99e9030d93286e41 (diff)
downloadranger-8fd92581dfcd126ce432b8832209f8cdf4a73ecd.tar.gz
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.
-rwxr-xr-xranger/data/scope.sh47
1 files changed, 47 insertions, 0 deletions
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
 }