modules: send_file support for URL

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-05 22:01:32 +01:00
parent 4582efd556
commit dd19f0f34e
3 changed files with 39 additions and 18 deletions

View File

@ -30,7 +30,7 @@
# 8 - curl/wget missing # 8 - curl/wget missing
# 10 - not bash! # 10 - not bash!
# #
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b #### $$VERSION$$ v1.25-dev-15-g4582efd
################################################################## ##################################################################
# emmbeded system may claim bash but it is not # emmbeded system may claim bash but it is not
@ -487,13 +487,13 @@ if detect_curl ; then
sendUpload() { sendUpload() {
[ "$#" -lt 4 ] && return [ "$#" -lt 4 ] && return
if [ -n "$5" ]; then if [ -n "$5" ]; then
[ -n "${BASHBOTDEBUG}" ] &&\ [ -n "${BASHBOTDEBUG}" ] &&\
log_update "sendUpload CHAT=$1 WHAT=$2 FILE=$3 CAPT=$5" log_update "sendUpload CHAT=$1 WHAT=$2 FILE=$3 CAPT=$5"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\
-F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -b -n 2>/dev/null )" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -b -n 2>/dev/null )"
else else
# shellcheck disable=SC2086 # shellcheck disable=SC2086
res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\
-F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -b -n 2>/dev/null )" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -b -n 2>/dev/null )"
fi fi

View File

@ -21,7 +21,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de # AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 25.12.2020 20:24 # CREATED: 25.12.2020 20:24
# #
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b #### $$VERSION$$ v1.25-dev-15-g4582efd
#=============================================================================== #===============================================================================
#### ####
@ -54,7 +54,8 @@ else
fi fi
FILE="$2" FILE="$2"
[[ "$2" != "/"* ]] && FILE="${PWD}/$2" # convert to absolute path if not start with / or http://
[[ ! ( "$2" == "/"* || "$2" =~ ^https*:// ) ]] && FILE="${PWD}/$2"
# send message in selected format # send message in selected format
"${SEND}" "${CHAT}" "${FILE}" "$3" "${SEND}" "${CHAT}" "${FILE}" "$3"

View File

@ -6,7 +6,7 @@
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
# shellcheck disable=SC1117 # shellcheck disable=SC1117
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b #### $$VERSION$$ v1.25-dev-15-g4582efd
# will be automatically sourced from bashbot # will be automatically sourced from bashbot
@ -205,19 +205,28 @@ send_file() {
fi fi
} }
# supports http and local file
# $1 chat, $2 file, $3 caption, $4 extension (optional)
upload_file(){ upload_file(){
local CUR_URL WHAT STATUS text=$3 file="$2" local CUR_URL WHAT STATUS media text file="$2" ext="$4"
# file access checks ... text="$(JsonEscape "$3")"
[[ "${file}" = *'..'* ]] && return 1 # no directory traversal if [[ "${file}" =~ ^https*:// ]]; then
[[ "${file}" = '.'* ]] && return 1 # no hidden or relative files media="URL"
if [[ "${file}" = '/'* ]] ; then
[[ ! "${file}" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX
else else
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR # we have a file, check file location ...
media="FILE"
[[ "${file}" = *'..'* ]] && return 1 # no directory traversal
[[ "${file}" = '.'* ]] && return 1 # no hidden or relative files
if [[ "${file}" = '/'* ]] ; then
[[ ! "${file}" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX
else
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR
fi
[ ! -r "${file}" ] && return 3 # and file must exits of course
fi fi
[ ! -r "${file}" ] && return 3 # and file must exits of course
case "${file##*.}" in [ -z "${ext}" ] && ext="${file##*.}"
case "${ext}" in
mp3|flac) mp3|flac)
CUR_URL="${AUDIO_URL}" CUR_URL="${AUDIO_URL}"
WHAT="audio" WHAT="audio"
@ -250,8 +259,19 @@ upload_file(){
STATUS="upload_document" STATUS="upload_document"
;; ;;
esac esac
# prepare to send FILE / URL
send_action "$1" "${STATUS}" send_action "$1" "${STATUS}"
sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "${text//\\n/$'\n'}" # select method to use
case "${media}" in
FILE) # send local file ...
sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "${text//\\n/$'\n'}";;
URL) # send URL, should also work for file_id ...
# e.g. '"photo":"https://dealz.rrr.de/assets/images/rbofd-1.gif","caption":"some text"'
sendJson "$1" '"'"${WHAT}"'":"'"${file}"'","caption":"'"${text//\\n/$'\n'}"'"' "${CUR_URL}"
esac
return 0
} }
# typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location # typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location