From 5923fe910ab7d99c0511983d4607a59667b876f9 Mon Sep 17 00:00:00 2001 From: Martin Winkler <10722552+efelon@users.noreply.github.com> Date: Wed, 30 Nov 2022 00:30:29 +0100 Subject: [PATCH] Fix #17 spaces in file names prevent file upload --- matrix.sh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/matrix.sh b/matrix.sh index 586b2f3..ee5d1e6 100755 --- a/matrix.sh +++ b/matrix.sh @@ -98,9 +98,10 @@ put() { } upload_file() { - file="$1" - content_type="$2" - filename="$3" + local file="$1" + local content_type="$2" + local filename= + filename="$(encode "$3")" response=$( _curl -XPOST --data-binary "@$file" -H "Content-Type: $content_type" "${MATRIX_HOMESERVER}/_matrix/media/r0/upload?filename=${filename}" ) } @@ -110,6 +111,12 @@ escape() { jq $multil -R . <<<"$1" } +encode() { + local multil= + [ $(echo "$1" | wc -l) -gt 1 ] && multil="-s" + jq $multil -Rr @uri <<<"$1" +} + ############## Check for dependencies hash jq >/dev/null 2>&1 || die "jq is required, but not installed." hash curl >/dev/null 2>&1 || die "curl is required, but not installed." @@ -258,13 +265,14 @@ send_file() { if (( size > max_size )); then die "File is too big. Size is $size, max_size is $max_size." fi - filename=$(basename "$FILE") + filename="$(basename "$FILE")" log "filename: $filename" - content_type=$(file --brief --mime-type "$FILE") + content_type="$(file --brief --mime-type "$FILE")" log "content-type: $content_type" upload_file "$FILE" "$content_type" "$filename" - uri=$(jq -r .content_uri <<<"$response") + [ -z "${response}" ] && return 1 + uri="$(jq -r .content_uri <<<"$response")" data="{\"body\":`escape "$filename"`, \"msgtype\":\"$FILE_TYPE\", \"filename\":`escape "$filename"`, \"url\":\"$uri\"}" _send_message "$data" }