diff --git a/stpkg b/stpkg index 3fdbb12a151cfff2f4ad6cfd7428d2c818381de3..b7bff716f347869fa8412d2b29ca4833a662c7bd 100755 --- a/stpkg +++ b/stpkg @@ -34,6 +34,7 @@ if [ "x$STPKG_COLORS" = "x" ]; then __red() { echo -ne '\e[31m'$*'\e[0m'; } __green() { echo -ne '\e[32m'$*'\e[0m'; } __yellow() { echo -ne '\e[33m'$*'\e[0m'; } + __magenta() { echo -ne '\e[35m'$*'\e[0m'; } __bold() { echo -ne '\033[1m'$*'\e[0m'; } __italic() { echo -ne '\033[3m'$*'\e[0m'; } @@ -110,6 +111,7 @@ cat << EOF $BASE_NAME pack [`__yellow "-t token"`] [`__yellow "name"`] <`__yellow "pack_folder"`> $BASE_NAME edit [`__yellow "-t token"`] <`__yellow "pack_name"`> add|del <`__yellow "sticker"`> [`__yellow "file"`] $BASE_NAME display [`__yellow "-dl folder"`] <`__yellow "pack_name"`> [`__yellow "sticker_regex"`] + $BASE_NAME migrate <`__yellow "pack_name"`> `__bold Flags:` `__yellow "-e"` / `__yellow "-ne"` The pack is enabled / disabled @@ -857,7 +859,11 @@ __pack() { ST_JSON="`jq --arg STICKER_NAME "$STICKER_NAME" '.stickers[] | select(.body == $STICKER_NAME)' < "$DEST_INDEX"`" else if [[ "x${updatedFiles[$FILE]}" != "x" ]]; then - STICKER_STATUS="`__yellow Updated`" + if [ "x$UNDER_MIGRATION" = "xyes" ]; then + STICKER_STATUS="`__magenta Migrated`" + else + STICKER_STATUS="`__yellow Updated`" + fi sed -i "/[0-9a-f]\{32\} $FILE/d" $SUM_FILE else STICKER_STATUS="`__green New`" @@ -901,9 +907,62 @@ __pack() { info "Pack created, you can now commit it and create a MR to share it with other users" } +download_image_from_server() { + local mxc_url="$1" + local name="$2" + local extension="$3" + + [ -z "$mxc_url" ] && die "Missing MXC URL before downloading." + [ -z "$2" ] && die "Missing name before downloading." + [ -z "$3" ] && die "Missing extension before downloading." + + local filename="$name.$extension" + local folder_destination="${4:-/tmp}" + [ ! -d "$folder_destination" ] && mkdir -p "$folder_destination" + + local destination=$(printf "%s/%s\n" "${folder_destination%/}" "${filename#/}") + echo $destination + + # Ideally, we'd check if the MXC is valid. Too lazy for a one-shot script. + local hostname="${mxc_url#*://}" + hostname="${hostname%%/*}" + local id="${mxc_url##*/}" + + curl "https://$STPKG_HOMESERV/_matrix/client/v1/media/download/$hostname/$id/" -H "Authorization: Bearer $STPKG_TOKEN" -o $destination +} + +# Allows to migrate stickers from a pack to the current homeserver +# Re-downloads the sticker images from their current server, and re-uploads to the new one +__migrate() { + local pack_name="$1" + [ "x$pack_name" = "x" ] && die "You must specify a pack for the 'migrate' command" + + local FILE="${STPKG_INSTALL}/web/packs/$pack_name.json" + [ ! -r "$FILE" ] && die "Pack '$pack_name' is not available (check with 'pack list'). The corresponding file should be '$FILE'" + echo "Migrate the sticker pack $(__green `jq '.title' < "$FILE"`):" + + if [ "x$STPKG_TOKEN" = "x" ]; then + read -sp "`__yellow 'Enter your access token:'`" STPKG_TOKEN + validate_token # Validate token, will exit if invalid + fi + + local temporary_destination="/tmp/$pack_name" + + jq '.stickers[] | "\(.body) \(.url) \(.info.mimetype)"' < "$FILE" | while IFS= read LINE; do + local NAME=`echo "$LINE" | awk -F '"| ' '{print $2}'` + local URL=` echo "$LINE" | awk -F '"| ' '{print $3}'` + local TYPE=`echo "$LINE" | awk -F '"| ' '{print $4}' | awk -F '/' '{print $2}'` + download_image_from_server $URL $NAME $TYPE $temporary_destination + done + + UNDER_MIGRATION="yes" + STPKG_REUPLOAD_SAME_FILE="yes" + __pack $pack_name $temporary_destination +} + __do_command() { case "$1" in - show|update|list|add|del|default|pack|display|edit) + show|update|list|add|del|default|pack|display|edit|migrate) # Source conf file, may have the sshfs hook if [ -f $STPKG_CONF_FILE ]; then source $STPKG_CONF_FILE @@ -1032,5 +1091,6 @@ case "$1" in pack) shift; __do_command pack $* ;; display) shift; __do_command display $*;; edit) shift; __do_command edit $* ;; + migrate) shift; __do_command migrate $*;; *) __do_command usage ;; esac