diff --git a/stpkg b/stpkg index db22cb43360459a2aecfa5f50637cfd80fd187f7..2db0b83398103be81d694846321f3b2c002d3e6f 100755 --- a/stpkg +++ b/stpkg @@ -154,6 +154,21 @@ validate_token() { info "Token is valid on homeserv '$STPKG_HOMESERV'" } +file_get_width() { + local WIDTH=$(identify -format "%w" "$1") >/dev/null + local WIDTH=$(( $WIDTH > 256 ? 256 : $WIDTH )) + echo "$WIDTH" +} + +file_get_height() { + local HEIGHT=$(identify -format "%h" "$1") >/dev/null + local HEIGHT=$(( $HEIGHT > 256 ? 256 : $HEIGHT )) + echo "$HEIGHT" +} + +file_get_type() { [[ "$1" =~ .*\.gif ]] && echo 'gif' || echo 'png'; } +file_get_name() { echo "$1" | cut -f1 -d'.'; } + upload_file() { local TYPE=$1 # The type of file (png/gif) local FILE=$2 # The file @@ -543,7 +558,6 @@ __list() { __edit() { # <pack name> <add|del> <sticker> [image file to use] - die "Not implemented yet" local PACK_NAME="$1" local ACTION="$2" local STICKER_NAME="$3" @@ -559,12 +573,21 @@ __edit() { case "$ACTION" in add) [ $# -eq 0 ] && die "You must specify an image file for the add action" - jq "del(.stickers[] | select(.body == \"$STICKER_NAME\"))" < "$TEMP" | sponge "$TEMP" - # jq ".stickers += [{\"$STICKER_NAME\"}]" < $TEMP | sponge $TEMP # 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'" + + # 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") + + jq "del(.stickers[] | select(.body == \"$STICKER_NAME\"))" < "$TEMP" | sponge "$TEMP" + # jq ".stickers += [{\"$STICKER_NAME\"}]" < $TEMP | sponge $TEMP ;; del) [ $# -ne 0 ] && die "Extra argument are present: $*" @@ -669,14 +692,12 @@ __pack() { # if the stderr of stpkg is redirected, progress messages will be lost. # Get sizes - local WIDTH=$( identify -format "%w" "$FILE") >/dev/null - local HEIGHT=$(identify -format "%h" "$FILE") >/dev/null - local WIDTH=$(( $WIDTH > 256 ? 256 : $WIDTH )) - local HEIGHT=$(( $HEIGHT > 256 ? 256 : $HEIGHT )) + local WIDTH=$(file_get_width "$FILE") + local HEIGHT=$(file_get_height "$FILE") # Get the names and extentions - local TYPE=`[[ "$FILE" =~ .*\.gif ]] && echo 'gif' || echo 'png'` - local NAME=`echo "$FILE" | cut -f1 -d'.'` + local TYPE=$(file_get_type "$FILE") + local NAME=$(file_get_name "$FILE") local DEST="$DEST_FOLDER/$NAME.$TYPE" # For the progress @@ -689,8 +710,8 @@ __pack() { # Add a ',' only if it's not the first in the array echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX - # Upload - upload_file "$TYPE" "$FILE" "$NAME" + # Upload the transformed file + upload_file "$TYPE" "$DEST" "$NAME" progress_dot # Calculate the 128x128 format