Skip to content
Extraits de code Groupes Projets
Valider 267b8cf5 rédigé par Elliu's avatar Elliu
Parcourir les fichiers

WIP: Refactor sticker processing and uploading

Still needs debugging and all
parent b62e839e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,6 +5,8 @@ shopt -s nocasematch # Case insensitive ...@@ -5,6 +5,8 @@ shopt -s nocasematch # Case insensitive
STPKG_CONF_FILE=~/.config/stpkg.sh STPKG_CONF_FILE=~/.config/stpkg.sh
BASE_DIR="$PWD" BASE_DIR="$PWD"
animatedFiletype=(gif apng)
# Detect install # Detect install
if [ "x$1" = "x-install" ]; then if [ "x$1" = "x-install" ]; then
INSTALL="yes" INSTALL="yes"
...@@ -154,24 +156,20 @@ validate_homeserv() { ...@@ -154,24 +156,20 @@ validate_homeserv() {
file_get_width() { file_get_width() {
local WIDTH=$(identify -format "%w" "$1[0]") >/dev/null 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" echo "$WIDTH"
} }
file_get_height() { file_get_height() {
local HEIGHT=$(identify -format "%h" "$1[0]") >/dev/null 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" 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'.'; } file_get_name() { echo "$1" | cut -f1 -d'.'; }
upload_file() { __upload_file() {
local TYPE=$1 # The type of file (png/gif) local TYPE=$1 # The type of file (png/gif)
local FILE=$2 # The file local FILE=$2 # The file
local NAME=$3 # The pretty name for the sticker local NAME=$3 # The pretty name for the sticker
...@@ -192,11 +190,6 @@ picture_process_sticker() { ...@@ -192,11 +190,6 @@ picture_process_sticker() {
local TYPE=$6 # The type of the file (png/gif/webp) local TYPE=$6 # The type of the file (png/gif/webp)
local DSTTYPE=$7 # The type of the destination file (png/gif) 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 repage=`[ "x${TYPE}" != "xgif" ] && echo '+repage' || echo ''`
local resize=`[ "x${TYPE}" != "xgif" ] && echo "-resize ${WIDTH}x${HEIGHT}" || echo ''` local resize=`[ "x${TYPE}" != "xgif" ] && echo "-resize ${WIDTH}x${HEIGHT}" || echo ''`
local OPTS=`[ "$TYPE" = "png" ] && echo '-type TrueColor PNG32:' || echo ' -coalesce '` local OPTS=`[ "$TYPE" = "png" ] && echo '-type TrueColor PNG32:' || echo ' -coalesce '`
...@@ -265,11 +258,11 @@ __install() { ...@@ -265,11 +258,11 @@ __install() {
;; ;;
esac esac
done done
STPKG_STICKER_REPO=${STPKG_STICKER_REPO:="https://github.com/maunium/stickerpicker.git"}
# Create the ~/.config/stpkg.sh # Create the ~/.config/stpkg.sh
[ ! -d ~/.config ] && mkdir ~/.config [ ! -d ~/.config ] && mkdir ~/.config
cat > $STPKG_CONF_FILE << EOF cat > $STPKG_CONF_FILE << EOF
# Please, only modify the 'STPKG_TOKEN'
if [ ! "x\$INSTALL" = "xyes" ]; then if [ ! "x\$INSTALL" = "xyes" ]; then
STPKG_BASE="$BASE_DIR" STPKG_BASE="$BASE_DIR"
STPKG_HOMESERV='matrix.org' STPKG_HOMESERV='matrix.org'
...@@ -573,6 +566,98 @@ __list() { ...@@ -573,6 +566,98 @@ __list() {
done 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() { __edit() {
# <pack name> <add|del> <sticker> [image file to use] # <pack name> <add|del> <sticker> [image file to use]
local PACK_NAME="$1" local PACK_NAME="$1"
...@@ -593,26 +678,10 @@ __edit() { ...@@ -593,26 +678,10 @@ __edit() {
local FILE="$1" local FILE="$1"
[ ! -r "$FILE" ] && die "Failed to find file '$FILE'" [ ! -r "$FILE" ] && die "Failed to find file '$FILE'"
validate_homeserv # Check if homeserv exists (see __pack) local ST_JSON=""
validate_token # Validate token, will exit if invalid (see __pack) process_sticker_file "$FILE" ST_JSON
# 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)
jq "del(.stickers[] | select(.body == \"$STICKER_NAME\"))" < "$TEMP" | sponge "$TEMP" 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 jq ".stickers += [$ST_JSON]" < $TEMP | sponge $TEMP
;; ;;
...@@ -702,58 +771,25 @@ __pack() { ...@@ -702,58 +771,25 @@ __pack() {
local CURRENT_FILE=1 local CURRENT_FILE=1
local FIRST_IN_ARRAY="" local FIRST_IN_ARRAY=""
for FILE in *; do 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 # Add a ',' only if it's not the first in the array
echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX
# Upload the transformed file local ST_JSON=""
local MXC=$(upload_file "$DSTTYPE" "$DEST" "$NAME") process_sticker_file $FILE ST_JSON
progress_dot echo "$ST_JSON" >> $DEST_INDEX
# 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
# For the report # For the report
[ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height" [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
local FIRST_IN_ARRAY="," 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 progress_reset
done | column -t done | column -t
# Create the preview # Create the preview
montage "$DEST_FOLDER/*.{png,gif}" -background none "$DEST_FOLDER/preview.png" #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" #echo -ne "# $PACK_NAME\n![Preview of $PACK_NAME](preview.png)" > "$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 {} \; find $DEST_FOLDER -type f \( ! -name "preview.png" -and ! -name "$PACK_NAME.json" \) -exec rm {} \;
echo -n "]}" >> $DEST_INDEX echo -n "]}" >> $DEST_INDEX
...@@ -784,8 +820,6 @@ __do_command() { ...@@ -784,8 +820,6 @@ __do_command() {
[ "x$STPKG_BASE" = "x" ] && die \ [ "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" "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) edit|pack)
...@@ -843,6 +877,7 @@ __do_command() { ...@@ -843,6 +877,7 @@ __do_command() {
# Generate preview of the sticker pack # Generate preview of the sticker pack
-preview) -preview)
STPKG_GENERATE_PREVIEW="yes" STPKG_GENERATE_PREVIEW="yes"
die "Asking for a preview generation, while this feature has been dropped for the moment"
shift shift
;; ;;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter