From c4064db6e6e509b9a95fed15e33d60827c01af37 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 2 Dec 2020 18:25:13 +0100
Subject: [PATCH] Finished pack command, with working upload

---
 matrixpack.sh | 10 ++++-----
 stpkg         | 58 +++++++++++++++++++++------------------------------
 2 files changed, 29 insertions(+), 39 deletions(-)

diff --git a/matrixpack.sh b/matrixpack.sh
index 2b81610..84add5f 100755
--- a/matrixpack.sh
+++ b/matrixpack.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 # Usage
 # matrixpack token homeserver directory...
-# 
+#
 # Pack name is the directory name
 # Sticker name is the file name
 # /!\ Token is a sensitive information
@@ -58,7 +58,7 @@ else
 fi
 
 # Checking if the token exist.
-# Save the error value on file "headers" and 
+# Save the error value on file "headers" and
 # evaluate it to verify the token
 curl -s -X POST  "https://$homeserver/_matrix/media/r0/upload?access_token=$token" | python3 -c "import sys, json; print(json.load(sys.stdin)['errcode'])" >> headers
 while read line; do
@@ -132,10 +132,10 @@ do
         echo -n "."
         convert -background none -gravity center "$slug/tmp/$f.$type" -resize "${width}x$height" $opts"$slug/tmp/$f.$type"
         echo -ne ". \033[92mdone\033[0m! "
-        
+
         # First item in array
         echo -n "$first" >> "$slug/$slug.json"
-        
+
         # Uploading image
         echo -n "uploading."
         mxc=$(curl -s -X POST -H "Content-Type: image/$type" --data-binary "@$slug/tmp/$f.$type" "https://$homeserver/_matrix/media/r0/upload?access_token=$token" | python -c "import sys, json; print(json.load(sys.stdin)['content_uri'])")
@@ -152,7 +152,7 @@ do
         echo -e ". \033[92msuccess\033[0m!"
       fi
     done
-    
+
     rm "$slug/tmp/size"
     montage "$slug/tmp/*"[0] -background none "$slug/preview.png"
     rm -r "$slug/tmp"
diff --git a/stpkg b/stpkg
index 89e7c3c..34e0cd9 100755
--- a/stpkg
+++ b/stpkg
@@ -380,11 +380,8 @@ __pack() {
         local NAME=`echo "$FILE" | cut -f1 -d'.'`
         local DEST="$DEST_FOLDER/$NAME.$TYPE"
 
-        # For the report
-        echo -e "$NAME $TYPE $WIDTH $HEIGHT"
-
         # For the progress
-        progress "($CURRENT_FILE/$TOTAL_FILES) $NAME"
+        progress "($CURRENT_FILE/$TOTAL_FILES) $NAME "
         CURRENT_FILE=$[ $CURRENT_FILE + 1 ]
 
         # Transform sticker source picture
@@ -397,12 +394,18 @@ __pack() {
 
         # Add a ',' only if it's not the first in the array
         echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX
-        FIRST_IN_ARRAY=","
 
-        # Upload TODO
-        local MXC=1
+        # Upload
+        # TODO: Use jq here
+        local MXC=`curl -s -X POST -H "Content-Type: image/$TYPE" --data-binary "@$DEST" \
+                    "https://$STPKG_HOMESERV/_matrix/media/r0/upload?access_token=$STPKG_TOKEN" | \
+                    python -c "import sys, json; print(json.load(sys.stdin)['content_uri'])" \
+                    || die "Failed to upload sticker $NAME for pack $PACK_NAME to $STPKG_HOMESERV"`
+        progress_dot
 
         # Calculate the 128x128 format
+        local INIT_WIDTH=$WIDTH
+        local INIT_HEIGHT=$HEIGHT
         convert "$DEST" -resize "128x128" "$DEST" 1>&2 \
             && progress_dot || die "$NAME failed at 128x128 resize"
         WIDTH=$( identify -format "%w" "$DEST") >/dev/null
@@ -413,13 +416,18 @@ __pack() {
                 "\"w\":$WIDTH,\"thumbnail_url\":\"$MXC\"},\"msgtype\":\"m.sticker\",\"url\":\"$MXC\"" \
                 ",\"id\":\"$PACK_NAME-$NAME\"}" >> $DEST_INDEX
 
+        # For the report
+        [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
+        FIRST_IN_ARRAY=","
+        echo -e "$NAME ($MXC) $TYPE $INIT_WIDTH $INIT_HEIGHT"
+
         progress_reset
-    done | column -t | sort
+    done | column -t
 
     # Create the preview
     montage "$DEST_FOLDER/*.{png,gif}" -background none "$DEST_FOLDER/preview.png"
     echo -ne "# $PACK_NAME\n![Preview of $PACK_NAME](preview.png)" > "$DEST_FOLDER/README.md"
-    find $DEST_FOLDER -type f \( ! -name "preview.png" \) -exec rm {} \;
+    find $DEST_FOLDER -type f \( ! -name "preview.png" -and ! -name "$PACK_NAME.json" \) -exec rm {} \;
 
     echo -n "]}" >> $DEST_INDEX
     info "Pack created, you can now commit it and create a MR to share it with other users"
@@ -431,29 +439,11 @@ __pack() {
 
 [ "x$1" = "x" ] && usage
 case "$1" in
-    update)
-        __update
-        ;;
-    list)
-        shift
-        __list $*
-        ;;
-    add)
-        shift
-        __add $*
-        ;;
-    del)
-        shift
-        __del $*
-        ;;
-    default)
-        __default
-        ;;
-    pack)
-        shift
-        __pack $*
-        ;;
-    *)
-        usage
-        ;;
+    update)         __update    ;;
+    list)   shift;  __list $*   ;;
+    add)    shift;  __add $*    ;;
+    del)    shift;  __del $*    ;;
+    default)        __default   ;;
+    pack)   shift;  __pack $*   ;;
+    *)              usage       ;;
 esac
-- 
GitLab