Skip to content

Commit

Permalink
Fix fabianonline#17 spaces in file names prevent file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
efelon committed Nov 29, 2022
1 parent af1c286 commit 5923fe9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions matrix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}" )
}

Expand All @@ -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."
Expand Down Expand Up @@ -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"
}
Expand Down

0 comments on commit 5923fe9

Please sign in to comment.