| 23 Jul 2021 |
@grahamc:nixos.org | cool | 14:59:06 |
@grahamc:nixos.org | thanks! | 14:59:07 |
@grahamc:nixos.org | I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃 | 14:59:36 |
andi- | One of the famous matrix bots has a command/plugin for converting an image to an MXC URI | 15:07:56 |
CRTified | Redacted or Malformed Event | 15:11:15 |
CRTified | In reply to @grahamc:nixos.org I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃 #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: image/jpeg" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" | jq '.content_uri')
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $mxc }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:29:53 |
@grahamc:nixos.org | 🥳 | 15:30:42 |
CRTified | Oh, Content-type might be wrong and needs a fix | 15:33:09 |
CRTified | But it works even if it's wrong | 15:33:19 |
CRTified | In reply to @grahamc:nixos.org I don't suppose anyone wants to make a little script which accepts a room id and local file path, to upload the image and set the avatar? 😃 * #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: image/png" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" | jq '.content_uri')
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $mxc }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:34:10 |
CRTified | * Oh, Content-type might be wrong and needs a fix (Edited it to image/png) | 15:34:37 |
CRTified | And note that there's no error handling 👀 | 15:35:25 |
sumner | Alternative is:
mimetype=$(file --mime-type $avatar_path | sed 's/.*: //g')
| 15:37:27 |
CRTified | That's significantly cleaner | 15:37:52 |
sumner | * Alternative is:
mimetype=$(file --mime-type $FILEPATH | sed 's/.*: //g')
| 15:38:14 |
CRTified | * #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')
echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:40:31 |
CRTified | Added it :) | 15:40:45 |
CRTified | Just the filename that's wrong now :D | 15:41:15 |
Leon | You should add file as a build input ;) | 15:41:21 |
CRTified | * #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl file
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')
echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload?filename=image.png")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:41:29 |
CRTified | In reply to @leons:is.currently.online You should add file as a build input ;) you're right. Didn't copy the whole file again as my token is in the local version 🙈 | 15:41:50 |
sumner | In reply to @schnecfk:ruhr-uni-bochum.de Just the filename that's wrong now :D filename is optional | 15:42:06 |
CRTified | * #!/usr/bin/env nix-shell
#!nix-shell -i sh -p jq curl file
# Configure this:
HOMESERVER="" # Without trailing slash
TOKEN="" # Access token here
# Pass this as argv:
ROOMID="${1}"
FILEPATH="${2}"
# Thanks to sumner
MIMETYPE=$(file --mime-type $FILEPATH | sed 's/.*: //g')
echo MIME is $MIMETYPE
# Upload image to image.png
mxc=$(curl --silent -H "Content-Type: ${MIMETYPE}" -H "Authorization: Bearer ${TOKEN}" -X POST --data-binary "@${FILEPATH}" "${HOMESERVER}/_matrix/media/r0/upload")
echo $mxc
# Build m.room.avatar event
EVENT_SET_AVATAR="{ \"url\": $(echo $mxc | jq '.content_uri') }"
# Push event
curl --silent -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" -X PUT --data "${EVENT_SET_AVATAR}" "${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
| 15:42:18 |
CRTified | In that case, poof :D | 15:42:25 |
sumner | In reply to @schnecfk:ruhr-uni-bochum.de Just the filename that's wrong now :D * filename is optional. Required fields are marked as such. | 15:42:37 |
sumner | May be worth uploading that as a file so that it's easy to find in the sidebar. | 15:43:59 |
CRTified | In that case I'll run shellcheck on it | 15:44:36 |
@grahamc:nixos.org | https://matrix.org/_matrix/media/r0/download/nixos.org/ab7db8fb43cc002b48b03de4cf04f0ec7c755585 | 15:48:02 |
@grahamc:nixos.org | nice! | 15:48:25 |
@grahamc:nixos.org | here is what I've put together:
make_avatar() (
room_id=$1
roomhash=$(md5sum <<<"$room_id" | cut -d' ' -f1)
filename="$scratch/$roomhash.avatar"
curl "https://www.gravatar.com/avatar/${roomhash}?d=identicon&f=y&s=2048" > "$filename"
echo "$filename"
)
upload_media() (
FILEPATH="${1}"
mime=$(file --brief --mime "$FILEPATH")
# Upload image to image.png
curl \
--silent \
-H "Content-Type: $mime" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-X POST \
--data-binary "@${FILEPATH}" \
"${HOMESERVER}/_matrix/media/r0/upload?filename=image.png" \
| jq '.content_uri'
)
set_room_avatar() (
ROOMID=$1
MXC=$2
# Build m.room.avatar event
EVENT_SET_AVATAR=$(jq -n '{ url: $mxc }' --arg mxc "$MXC")
# Push event
curl \
--silent \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-X PUT \
--data "${EVENT_SET_AVATAR}" \
"${HOMESERVER}/_matrix/client/r0/rooms/${ROOMID}/state/m.room.avatar"
)
| 15:48:52 |