From 7c4d7d2612dfb0f5364672be789db090c3cebeb0 Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Mon, 15 Aug 2022 13:21:47 +0200
Subject: [PATCH] Bugfix

---
 stpkg | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/stpkg b/stpkg
index 3a6c60c..275b14a 100755
--- a/stpkg
+++ b/stpkg
@@ -5,7 +5,7 @@ shopt -s nocasematch    # Case insensitive
 STPKG_CONF_FILE=~/.config/stpkg.sh
 BASE_DIR="$PWD"
 
-animatedFiletype=(gif apng)
+declare -A animatedFiletype=(gif apng)
 
 # Detect install
 if [ "x$1" = "x-install" ]; then
@@ -56,7 +56,7 @@ require() {
 }
 require \
     md5sum mktemp column uuidgen convert montage identify python3 curl \
-    sed tr git sponge ls find jq chmod cat expr iconv
+    sed tr git sponge ls find jq chmod cat expr iconv bc
 __grep=`which egrep`
 if [ -z $__grep ]; then
     __grep=`which grep`
@@ -175,7 +175,7 @@ __upload_file() {
     local NAME=$3   # The pretty name for the sticker
     # TODO: Use jq here
     curl -s -X POST -H \
-        "Content-Type: image/$TYPE" --data-binary "@$DEST" "https://$STPKG_HOMESERV/_matrix/media/r0/upload?access_token=$STPKG_TOKEN" | \
+        "Content-Type: $TYPE" --data-binary "@$FILE" "https://$STPKG_HOMESERV/_matrix/media/r0/upload?access_token=$STPKG_TOKEN" | \
         tee /tmp/toto | python -c "import sys, json; print(json.load(sys.stdin)['content_uri'])"
     [ $? -ne 0 ] && die "Failed to upload sticker $NAME for pack $PACK_NAME to $STPKG_HOMESERV"
 }
@@ -274,6 +274,9 @@ STPKG_MAX_WIDTH=256
 STPKG_GENERATE_PREVIEW="no"
 STPKG_IMAGE_TARGET_FILETYPE="png"
 STPKG_ANIMATED_TARGET_FILETYPE="gif"
+
+__do_mount() { :; }
+
 EOF
     chmod 00600 $STPKG_CONF_FILE
 
@@ -578,8 +581,8 @@ process_sticker_file() {
     local INIT_WIDTH=$(file_get_width "$FILE")
     local INIT_HEIGHT=$(file_get_height "$FILE")
 
-    local WIDTH_RATIO="$(echo "scale=10; $INIT_WIDTH/$STPKG_MAX_WIDTH")"
-    local HEIGHT_RATIO="$(echo "scale=10; $INIT_HEIGHT/$STPKG_MAX_HEIGHT")"
+    local WIDTH_RATIO="$(echo "scale=10; $INIT_WIDTH/$STPKG_MAX_WIDTH" | bc)"
+    local HEIGHT_RATIO="$(echo "scale=10; $INIT_HEIGHT/$STPKG_MAX_HEIGHT" | bc)"
 
     if [ $(echo "$HEIGHT_RATIO >= $WIDTH_RATIO" | bc) = "1" ]; then
         if [ $(echo "$HEIGHT_RATIO > 1" | bc) = "1" ]; then
@@ -589,18 +592,17 @@ process_sticker_file() {
             local DEST_HEIGHT=$INIT_HEIGHT
             local DEST_WIDTH=$INIT_WIDTH
         fi
+    elif [ $(echo "$WIDTH_RATIO > 1" | bc) = "1" ]; then
+        local DEST_HEIGHT=""
+        local DEST_WIDTH=$STPKG_MAX_WIDTH
     else
-        if [ $(echo "$WIDTH_RATIO > 1" | bc) = "1" ]; then
-            local DEST_HEIGHT=""
-            local DEST_WIDTH=$STPKG_MAX_WIDTH
-        else
-            local DEST_HEIGHT=$INIT_HEIGHT
-            local DEST_WIDTH=$INIT_WIDTH
-        fi
+        local DEST_HEIGHT=$INIT_HEIGHT
+        local DEST_WIDTH=$INIT_WIDTH
     fi
 
     # Get the names and extensions
     local INIT_TYPE=$(file_get_type "$FILE")
+    local INIT_EXT=$(file_get_ext_from_type "$INIT_TYPE")
     local NAME=$(file_get_name "$FILE")
 
     local TEMP=`mktemp --suffix=.stpkg`
@@ -625,7 +627,7 @@ process_sticker_file() {
         progress_dot
 
     else
-        if [[ ${animatedFiletype[$INIT_TYPE]} ]]; then
+        if [[ ${animatedFiletype[$INIT_EXT]} ]]; then
             #
             # Convert animated image
             #
@@ -637,11 +639,11 @@ process_sticker_file() {
             # Convert fixed image
             #
             local DEST_TYPE=$STPKG_IMAGE_TARGET_FILETYPE
-            local BONUS_OPT="+repage"
+            local BONUS_OPT="-trim +repage"
         fi
 
         local DEST_EXT=$(file_get_ext_from_type "$DEST_TYPE")
-        convert "$FILE" -bordercolor none -border 1 -background none -gravity center -resize ${DEST_WIDTH}x${DEST_HEIGHT} ${BONUS_OPT} "${DEST_EXT}:${TEMP}" 1>&2 \
+        convert "$FILE" -bordercolor none -border 1 ${BONUS_OPT} -background none -gravity center -resize ${DEST_WIDTH}x${DEST_HEIGHT} "${DEST_EXT}:${TEMP}" 1>&2 \
             && progress_dot \
             || die "$NAME failed converting the sticker"
 
@@ -781,7 +783,7 @@ __pack() {
         # For the report
         [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
         local FIRST_IN_ARRAY=","
-        echo -e "$ST_JSON" | jq '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'
+        echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'
 
         progress_reset
     done | column -t
@@ -900,7 +902,7 @@ __do_command() {
         *)
             ;;
     esac
-    $*
+    __$*
 }
 
 ######################
-- 
GitLab