dotfiles/.config/zsh/functions/mxc2http

26 lines
802 B
Bash
Executable File

#!/usr/bin/env zsh
# Converts mxc:// URLs into https:// URLs
# <https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3downloadservernamemediaid>
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
zmodload zsh/zutil
local -a o_help=()
local -a o_server=()
zparseopts -D -K -- h=o_help -help=o_help -server:=o_server
if [[ ${#o_help} -ne 0 || ! -v 1 ]]; then
local ret=$(( ${#o_help} ^ 1 ))
print -u $(( 1 + ${ret} )) "usage: ${0} [-h|--help] [--server HOST] <URL> …"
return ${ret}
fi
local -a urls=(${@})
for url in ${urls}; do
local domain=${${url%/*}##*/}
local server=${domain}
[[ ${#o_server} -eq 2 ]] && server=${o_server[2]}
local id=${url##*/}
print '\e[1;95m→\e[0m' \
"https://${server}/_matrix/media/v3/download/${domain}/${id}"
done