diff --git a/stpkg b/stpkg index 48bd925912e4b2d96f535f8b42550aaacf924306..3a6c60c22442feff414fea597dff29862faa91b4 100755 --- a/stpkg +++ b/stpkg @@ -5,6 +5,8 @@ shopt -s nocasematch # Case insensitive STPKG_CONF_FILE=~/.config/stpkg.sh BASE_DIR="$PWD" +animatedFiletype=(gif apng) + # Detect install if [ "x$1" = "x-install" ]; then INSTALL="yes" @@ -154,24 +156,20 @@ validate_homeserv() { file_get_width() { local WIDTH=$(identify -format "%w" "$1[0]") >/dev/null - if [ "x$STPKG_TRUST_SOURCE" = "x" ]; then - local WIDTH=$(( $WIDTH > 256 ? 256 : $WIDTH )) - fi echo "$WIDTH" } file_get_height() { local HEIGHT=$(identify -format "%h" "$1[0]") >/dev/null - if [ "x$STPKG_TRUST_SOURCE" = "x" ]; then - local WIDTH=$(( $WIDTH > 256 ? 256 : $WIDTH )) - fi echo "$HEIGHT" } -file_get_type() { xdg-mime query filetype "$1" | sed 's+^.*/++'; } +file_get_type() { xdg-mime query filetype "$1"; } +file_get_ext() { xdg-mime query filetype "$1" | sed 's+^.*/++'; } +file_get_ext_from_type() { echo "$1" | sed 's+^.*/++'; } file_get_name() { echo "$1" | cut -f1 -d'.'; } -upload_file() { +__upload_file() { local TYPE=$1 # The type of file (png/gif) local FILE=$2 # The file local NAME=$3 # The pretty name for the sticker @@ -192,11 +190,6 @@ picture_process_sticker() { local TYPE=$6 # The type of the file (png/gif/webp) local DSTTYPE=$7 # The type of the destination file (png/gif) - if [ "x$STPKG_TRUST_SOURCE" != "x" ] && [ "$TYPE" == "$DSTTYPE" ]; then - cp "$FILE" "$DEST" - return - fi - local repage=`[ "x${TYPE}" != "xgif" ] && echo '+repage' || echo ''` local resize=`[ "x${TYPE}" != "xgif" ] && echo "-resize ${WIDTH}x${HEIGHT}" || echo ''` local OPTS=`[ "$TYPE" = "png" ] && echo '-type TrueColor PNG32:' || echo ' -coalesce '` @@ -265,11 +258,11 @@ __install() { ;; esac done + STPKG_STICKER_REPO=${STPKG_STICKER_REPO:="https://github.com/maunium/stickerpicker.git"} # Create the ~/.config/stpkg.sh [ ! -d ~/.config ] && mkdir ~/.config cat > $STPKG_CONF_FILE << EOF -# Please, only modify the 'STPKG_TOKEN' if [ ! "x\$INSTALL" = "xyes" ]; then STPKG_BASE="$BASE_DIR" STPKG_HOMESERV='matrix.org' @@ -573,6 +566,98 @@ __list() { done } +# Results are on stdout and progress on stderr. Note that +# if the stderr of stpkg is redirected, progress messages will be lost. +# Usage: process_sticker_file <filename> <&JSON> +process_sticker_file() { + FILE="$1" + + local -n JSON_REF="$2" + + # Get sizes + 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")" + + if [ $(echo "$HEIGHT_RATIO >= $WIDTH_RATIO" | bc) = "1" ]; then + if [ $(echo "$HEIGHT_RATIO > 1" | bc) = "1" ]; then + local DEST_HEIGHT=$STPKG_MAX_HEIGHT + local DEST_WIDTH="" + else + local DEST_HEIGHT=$INIT_HEIGHT + local DEST_WIDTH=$INIT_WIDTH + fi + 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 + fi + + # Get the names and extensions + local INIT_TYPE=$(file_get_type "$FILE") + local NAME=$(file_get_name "$FILE") + + local TEMP=`mktemp --suffix=.stpkg` + + if [ "x$TYPE" != "xgif" ] && [ "x$TYPE" != "xpng" ]; then + local DSTTYPE="png" + else + local DSTTYPE="$TYPE" + fi + local DEST="$DEST_FOLDER/$NAME.$DSTTYPE" + + # For the progress + #progress "($CURRENT_FILE/$TOTAL_FILES) $NAME " + #local CURRENT_FILE=$[ $CURRENT_FILE + 1 ] + + if [ "x${STPKG_TRUST_SOURCE}" = "xyes" ]; then + # + # Don't convert image, upload it as it is + # + local DEST_TYPE=$INIT_TYPE + cp ${FILE} ${TEMP} + progress_dot + + else + if [[ ${animatedFiletype[$INIT_TYPE]} ]]; then + # + # Convert animated image + # + local DEST_TYPE=$STPKG_ANIMATED_TARGET_FILETYPE + local BONUS_OPT="" + + else + # + # Convert fixed image + # + local DEST_TYPE=$STPKG_IMAGE_TARGET_FILETYPE + local BONUS_OPT="+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 \ + && progress_dot \ + || die "$NAME failed converting the sticker" + + DEST_WIDTH=$(file_get_width "$TEMP") + DEST_HEIGHT=$(file_get_height "$TEMP") + fi + + # TODO: get real width/height from generated file + + # Upload the transformed file + local MXC=$(__upload_file "$DEST_TYPE" "$TEMP" "$NAME") + progress_dot + + JSON_REF=$(create_sticker_json "$PACK_NAME" "$NAME" "$DEST_WIDTH" "$DEST_HEIGHT" "$DEST_TYPE" "$MXC") +} + __edit() { # <pack name> <add|del> <sticker> [image file to use] local PACK_NAME="$1" @@ -593,26 +678,10 @@ __edit() { 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") - if [ "x$TYPE" == "xwebp" ]; then - local DSTTYPE="png" - else - local DSTTYPE="$TYPE" - fi - local DEST=$(mktemp --suffix=".$DSTTYPE") - - picture_process_sticker "$FILE" "$DEST" "$NAME" "$WIDTH" "$HEIGHT" "$TYPE" "$DSTTYPE" # Transform sticker picture (see __pack) - local MXC=$(upload_file "$DSTTYPE" "$DEST" "$NAME") # Upload transformed file (see __pack) + local ST_JSON="" + process_sticker_file "$FILE" ST_JSON jq "del(.stickers[] | select(.body == \"$STICKER_NAME\"))" < "$TEMP" | sponge "$TEMP" - local ST_JSON=$(create_sticker_json "$PACK_NAME" "$NAME" "$WIDTH" "$HEIGHT" "$DSTTYPE" "$MXC") jq ".stickers += [$ST_JSON]" < $TEMP | sponge $TEMP ;; @@ -702,58 +771,25 @@ __pack() { local CURRENT_FILE=1 local FIRST_IN_ARRAY="" for FILE in *; do - # In this loop, results are on stdout and progress on stderr. Note that - # if the stderr of stpkg is redirected, progress messages will be lost. - - # Get sizes - local WIDTH=$(file_get_width "$FILE") - local HEIGHT=$(file_get_height "$FILE") - - # Get the names and extensions - local TYPE=$(file_get_type "$FILE") - local NAME=$(file_get_name "$FILE") - if [ "x$TYPE" != "xgif" ] && [ "x$TYPE" != "xpng" ]; then - local DSTTYPE="png" - else - local DSTTYPE="$TYPE" - fi - local DEST="$DEST_FOLDER/$NAME.$DSTTYPE" - - # For the progress - progress "($CURRENT_FILE/$TOTAL_FILES) $NAME " - local CURRENT_FILE=$[ $CURRENT_FILE + 1 ] - - # Transform sticker source picture - picture_process_sticker "$FILE" "$DEST" "$NAME" "$WIDTH" "$HEIGHT" "$TYPE" "$DSTTYPE" - # Add a ',' only if it's not the first in the array echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX - # Upload the transformed file - local MXC=$(upload_file "$DSTTYPE" "$DEST" "$NAME") - progress_dot - - # Calculate the 128x128 format - local INIT_WIDTH=$WIDTH - local INIT_HEIGHT=$HEIGHT - picture_resize_preview "$DEST" "$NAME" - local WIDTH=$( identify -format "%w" "$DEST[0]") >/dev/null - local HEIGHT=$(identify -format "%h" "$DEST[0]") >/dev/null - - # Add the sticker to the index file - create_sticker_json "$PACK_NAME" "$NAME" "$WIDTH" "$HEIGHT" "$DSTTYPE" "$MXC" >> $DEST_INDEX + local ST_JSON="" + process_sticker_file $FILE ST_JSON + echo "$ST_JSON" >> $DEST_INDEX # For the report [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height" local FIRST_IN_ARRAY="," - echo -e "$NAME ($MXC) $DSTTYPE $INIT_WIDTH $INIT_HEIGHT" + echo -e "$ST_JSON" | jq '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")' progress_reset done | column -t # Create the preview - montage "$DEST_FOLDER/*.{png,gif}" -background none "$DEST_FOLDER/preview.png" - echo -ne "# $PACK_NAME\n" > "$DEST_FOLDER/README.md" + #montage "$DEST_FOLDER/*.{png,gif}" -background none "$DEST_FOLDER/preview.png" + #echo -ne "# $PACK_NAME\n" > "$DEST_FOLDER/README.md" + echo -ne "# $PACK_NAME\n(no preview)" > "$DEST_FOLDER/README.md" find $DEST_FOLDER -type f \( ! -name "preview.png" -and ! -name "$PACK_NAME.json" \) -exec rm {} \; echo -n "]}" >> $DEST_INDEX @@ -784,8 +820,6 @@ __do_command() { [ "x$STPKG_BASE" = "x" ] && die \ "The STPKG_BASE env var is mandatory, it must point to the sticker repo. It should have been given by the install script" - - STPKG_STICKER_REPO=${STPKG_STICKER_REPO:="https://github.com/maunium/stickerpicker.git"} ;;& edit|pack) @@ -843,6 +877,7 @@ __do_command() { # Generate preview of the sticker pack -preview) STPKG_GENERATE_PREVIEW="yes" + die "Asking for a preview generation, while this feature has been dropped for the moment" shift ;;