From 823a7c1d5bfad2b714e3076c5a66c1f16e871006 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 28 Dec 2020 16:33:36 +0100
Subject: [PATCH] STPKG: The edit should work, not tested. Also use a function
 to check homeserv

---
 stpkg | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/stpkg b/stpkg
index 2db0b83..4c51612 100755
--- a/stpkg
+++ b/stpkg
@@ -154,6 +154,11 @@ validate_token() {
     info "Token is valid on homeserv '$STPKG_HOMESERV'"
 }
 
+validate_homeserv() {
+    curl "https://$STPKG_HOMESERV" >/dev/null 2>&1
+    [ $? -ne 0 ] && die "Homeserv '$STPKG_HOMESERV' doesn't exist or is inaxessible"
+}
+
 file_get_width() {
     local WIDTH=$(identify -format "%w" "$1") >/dev/null
     local WIDTH=$(( $WIDTH  > 256 ? 256 : $WIDTH ))
@@ -573,21 +578,25 @@ __edit() {
     case "$ACTION" in
         add)
             [ $# -eq 0 ] && die "You must specify an image file for the add action"
-            # TODO:
-            # - Create the sticker from the picture (resize, etc)
-            # - Upload to homeserver
-            # - Create the json object and add it to the pack's json
             local FILE="$1"
             [ ! -r "$FILE" ] && die "Failed to find file '$FILE'"
 
+            validate_homeserv   # Check if homeserv exists (see __pack)
+            validate_token      # Validate token, will exit if invalid (see __pack)
+
             # Get sizes (see __pack)
             local WIDTH=$(file_get_width "$FILE")
             local HEIGHT=$(file_get_height "$FILE")
             local TYPE=$(file_get_type "$FILE")
             local NAME=$(file_get_name "$FILE")
+            local DEST=$(mktemp --suffix=".$TYPE")
+
+            picture_process_sticker "$FILE" "$DEST" "$NAME" "$WIDTH" "$HEIGHT" "$TYPE"  # Transform sticker picture (see __pack)
+            upload_file "$TYPE" "$DEST" "$NAME"                                         # Upload transformed file (see __pack)
 
             jq "del(.stickers[] | select(.body == \"$STICKER_NAME\"))" < "$TEMP" | sponge "$TEMP"
-            # jq ".stickers += [{\"$STICKER_NAME\"}]" < $TEMP | sponge $TEMP
+            local ST_JSON=$(create_sticker_json "$PACK_NAME" "$NAME" "$WIDTH" "$HEIGHT" "$TYPE" "$MXC")
+            jq ".stickers += [$ST_JSON]" < $TEMP | sponge $TEMP
             ;;
         del)
             [ $# -ne 0 ] && die "Extra argument are present: $*"
@@ -637,9 +646,7 @@ progress()       { echo 1>&2 -n "$*";       }
 progress_dot()   { progress '.';            }
 progress_reset() { echo 1>&2 -en "\r"; }
 __pack() {
-    # Check if homeserv exists
-    curl "https://$STPKG_HOMESERV" >/dev/null 2>&1
-    [ $? -ne 0 ] && die "Homeserv '$STPKG_HOMESERV' doesn't exist or is inaxessible"
+    validate_homeserv # Check if homeserv exists
 
     # Get the token
     if [ "x$1" = "x-t" ]; then
-- 
GitLab