download_file: use mktemp doc: do not download manually

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-02-06 15:30:31 +01:00
parent 54673ac396
commit 0702d58ab0
2 changed files with 26 additions and 19 deletions

View File

@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb
# 8 - curl/wget missing # 8 - curl/wget missing
# 10 - not bash! # 10 - not bash!
# #
#### $$VERSION$$ v1.41-dev-2-g5a689d2 #### $$VERSION$$ v1.41-dev-5-g54673ac
################################################################## ##################################################################
# are we running in a terminal? # are we running in a terminal?
@ -443,17 +443,20 @@ download_file() {
local url="$1" file="${2:-$1}" local url="$1" file="${2:-$1}"
# old mode if full URL is given # old mode if full URL is given
if [[ "${1}" =~ ^https*:// ]]; then if [[ "${1}" =~ ^https*:// ]]; then
# download full URL with random filename # random filename if not given for http
file="${RANDOM}" if [ -z "$2" ]; then
: "$(mktemp -u -p . "XXXXXXXXXX" 2>/dev/null)"
file="download-${_#./}"
fi
else else
# prefix https://api.telegram... # prefix https://api.telegram...
url="${URL}/${url}" url="${URL}/${url}"
fi fi
# filename: replace "/" with "-", prefix random number if file exist # filename: replace "/" with "-", use mktemp if exist
file="${file//\//-}" file="${DATADIR:-.}/${file//\//-}"
while [ -f "${DATADIR:-.}/${file}" ] ; do file="${RANDOM}-${file}"; done [ -f "${file}" ] && file="$(mktemp -p "${DATADIR:-.}" "XXXXX-${file##*/}" )"
getJson "${url}" >"${DATADIR:-.}/${file}" || return getJson "${url}" >"${file}" || return
printf '%s\n' "${DATADIR:-.}/${file}" printf '%s\n' "${file}"
} }

View File

@ -1419,33 +1419,37 @@ echo "$photo" -> photo/file_1234.jpg
file="$(download_file "${photo}" file="$(download_file "${photo}"
echo "$file" -> ./data-bot-bash/photo-file_1234.jpg echo "$file" -> ./data-bot-bash/photo-file_1234.jpg
# second, third etc. download # second download
file="$(download_file "${photo}" file="$(download_file "${photo}"
echo "$file" -> ./data-bot-bash/jkdfhihsdkf-photo-file_1234.jpg echo "$file" -> ./data-bot-bash/jkdfhi-photo-file_1234.jpg
ls data-bot-bash/*.jpg ls data-bot-bash/*.jpg
photo-file_1234.jpg jkdfhihsdkf-photo-file_1234.jpg photo-file_1234.jpg jkdfhi-photo-file_1234.jpg
######## ########
# download from other sources (full URL) # download from other sources (full URL)
file="$(download "https://avatars.githubusercontent.com/u/13046303")"
echo "$file" -> ./data-bot-bash/download-askjgftGJGdh1Z
file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")" file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")"
echo "$file" -> ./data-bot-bash/avatar.jpg echo "$file" -> ./data-bot-bash/avatar.jpg
file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")" file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")"
echo "$file" -> ./data-bot-bash/jhsdfjkakshdfkja-avatar.jpg echo "$file" -> ./data-bot-bash/jhsdf-avatar.jpg
ls data-bot-bash/
avatar.jpg jhsdf-avatar.jpg download-askjgftGJGdh1Z
ls data-bot-bash/*.jpg
photo-file_1234.jpg jkdfhihsdkf-photo-file_1234.jpg
avatar.jpg jhsdfjkakshdfkja-avatar.jpg
####### #######
# manually download files to current directory (not recommended) # manually download files to current directory (not recommended)
getJson "https://avatars.githubusercontent.com/u/13046303" >"avatar.jpg" getJson "https://avatars.githubusercontent.com/u/13046303" >"avatar.jpg"
getJson "i${URL}/${photo}" >"downloaded_photo.jpg" getJson "i${URL}/${photo}" >"downloaded_photo.jpg"
ls * ls -F
JSON.sh/ addons/ bin/ data-bot_bash/ doc/ modules/ scripts/ JSON.sh/ bin/ modules/ data-bot-bash/
avatar.jpg bashbot.sh commands.sh downloaded_photo.jpg LICENSE mycommands.conf mycommands.sh mycommands.sh avatar.jpg bashbot.sh* botconfig.jssh commands.sh count.jssh downloaded_photo.jpg mycommands.sh ...
``` ```
@ -1613,5 +1617,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca
#### [Prev Best Practice](5_practice.md) #### [Prev Best Practice](5_practice.md)
#### [Next Notes for Developers](7_develop.md) #### [Next Notes for Developers](7_develop.md)
#### $$VERSION$$ v1.41-dev-4-gbaa4e14 #### $$VERSION$$ v1.41-dev-5-g54673ac