feat: Improve download progress in container logs (#1294)

This commit is contained in:
Kroese
2026-07-17 22:29:10 +02:00
committed by GitHub
parent 3eec8e03ed
commit 1eb0db66ba
2 changed files with 95 additions and 29 deletions
+6 -9
View File
@@ -154,28 +154,25 @@ else
SIZE=0
REASON=""
PROGRESS=()
DOTBYTES=2097152
OUTPUT=""
LOG=$(mktemp)
[[ "${URL,,}" == *"_72806.pat" ]] && SIZE=361010261
[[ "${URL,,}" == *"_69057.pat" ]] && SIZE=363837333
[[ "${URL,,}" == *"_42218.pat" ]] && SIZE=379637760
# Check if output is to interactive TTY or redirected to docker log
# Use Wget's progress bar in a terminal and progress.sh in container logs.
if [ -t 1 ]; then
PROGRESS=( --progress=bar:noscroll )
PROGRESS=( --show-progress --progress=bar:noscroll )
else
if (( SIZE > 0 )); then
DOTBYTES=$(( (SIZE + 199) / 200 ))
fi
PROGRESS=( --progress=dot --execute "dotbytes=$DOTBYTES" )
OUTPUT="log"
fi
/run/progress.sh "$PAT" "$SIZE" "$MSG ([P])..." &
/run/progress.sh "$PAT" "$SIZE" "$MSG ([P])..." "$OUTPUT" &
{
LC_ALL=C wget "$URL" -O "$PAT" --no-verbose --no-check-certificate \
--timeout=30 --no-http-keep-alive --show-progress "${PROGRESS[@]}" \
--timeout=30 --no-http-keep-alive "${PROGRESS[@]}" \
--output-file="$LOG"
rc=$?
} || :
+89 -20
View File
@@ -3,43 +3,112 @@ set -Eeuo pipefail
info="/run/shm/msg.html"
escape () {
local s
s=${1//&/\&}
s=${s//</\&lt;}
s=${s//>/\&gt;}
s=${s//'"'/\&quot;}
printf -- %s "$s"
escape() {
local s
s=${1//&/\&amp;}
s=${s//</\&lt;}
s=${s//>/\&gt;}
s=${s//'"'/\&quot;}
printf -- %s "$s"
return 0
}
getBytes() {
local path="$1"
local bytes
if [ ! -s "$path" ] && [ ! -d "$path" ]; then
echo "0"
return 0
fi
bytes=$(du -sb "$path" 2>/dev/null | cut -f1) || bytes="0"
echo "$bytes"
return 0
}
printPercentProgress() {
local percent="$1"
while (( next_percent <= percent && next_percent <= 100 )); do
printf '%s%% ' "$next_percent"
printed="Y"
next_percent=$((next_percent + 10))
done
return 0
}
printSizeProgress() {
local bytes="$1"
local size
while (( bytes >= next_bytes )); do
size=$(numfmt --to=iec --suffix=B "$next_bytes" | sed -r 's/([A-Z])/ \1/') || size="${next_bytes} bytes"
printf '%s ' "$size"
printed="Y"
next_bytes=$((next_bytes + 536870912))
done
return 0
}
finishLogProgress() {
if [[ "$output" == "log" && "$printed" == "Y" ]]; then
printf '\n'
fi
return 0
}
path="$1"
total="$2"
body=$(escape "$3")
output="${4:-}"
printed="N"
next_percent=10
next_bytes=536870912
trap finishLogProgress EXIT
if [[ "$body" == *"..." ]]; then
body="<p class=\"loading\">${body::-3}</p>"
fi
while true
do
while true; do
bytes=$(getBytes "$path")
if [ ! -s "$path" ] && [ ! -d "$path" ]; then
bytes="0"
else
bytes=$(du -sb "$path" 2>/dev/null | cut -f1) || bytes="0"
fi
if (( bytes > 4096 )); then
if [ -z "$total" ] || [[ "$total" == "0" ]] || [ "$bytes" -gt "$total" ]; then
if [ -z "$total" ] || [[ "$total" == "0" ]] || (( bytes > total )); then
size=$(numfmt --to=iec --suffix=B "$bytes" | sed -r 's/([A-Z])/ \1/') || size="${bytes} bytes"
if [[ "$output" == "log" ]]; then
printSizeProgress "$bytes"
fi
else
size="$(echo "$bytes" "$total" | awk '{printf "%.1f", $1 * 100 / $2}')"
size="$size%"
progress=$((bytes * 1000 / total))
(( progress > 1000 )) && progress=1000
percent=$((progress / 10))
printf -v size '%d.%d%%' "$((progress / 10))" "$((progress % 10))"
if [[ "$output" == "log" ]]; then
printPercentProgress "$percent"
fi
fi
[[ "$size" != "0.0%" ]] && echo "${body//(\[P\])/($size)}"> "$info"
[[ "$size" != "0.0%" ]] && echo "${body//(\[P\])/($size)}" > "$info"
fi
sleep 1 & wait $!
done