feat: Refactor download code

This commit is contained in:
Kroese 2024-05-16 19:42:17 +02:00 committed by GitHub
parent 609fd5213a
commit 1085f638d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -209,200 +209,6 @@ detectCustom() {
return 0
}
getESD() {
local dir="$1"
local version="$2"
local editionName
local winCatalog size
if ! isESD "${version,,}"; then
error "Invalid VERSION specified, value \"$version\" is not recognized!" && return 1
fi
winCatalog=$(getCatalog "$version" "url")
editionName=$(getCatalog "$version" "edition")
local msg="Downloading product information from Microsoft..."
info "$msg" && html "$msg"
rm -rf "$dir"
mkdir -p "$dir"
local wFile="catalog.cab"
local xFile="products.xml"
local eFile="esd_edition.xml"
local fFile="products_filter.xml"
{ wget "$winCatalog" -O "$dir/$wFile" -q --timeout=10; rc=$?; } || :
(( rc != 0 )) && error "Failed to download $winCatalog , reason: $rc" && return 1
cd "$dir"
if ! cabextract "$wFile" > /dev/null; then
cd /run
error "Failed to extract $wFile!" && return 1
fi
cd /run
if [ ! -s "$dir/$xFile" ]; then
error "Failed to find $xFile in $wFile!" && return 1
fi
local esdLang="en-us"
local edQuery='//File[Architecture="'${PLATFORM}'"][Edition="'${editionName}'"]'
echo -e '<Catalog>' > "$dir/$fFile"
xmllint --nonet --xpath "${edQuery}" "$dir/$xFile" >> "$dir/$fFile" 2>/dev/null
echo -e '</Catalog>'>> "$dir/$fFile"
xmllint --nonet --xpath '//File[LanguageCode="'${esdLang}'"]' "$dir/$fFile" >"$dir/$eFile"
size=$(stat -c%s "$dir/$eFile")
if ((size<20)); then
error "Failed to find Windows product in $eFile!" && return 1
fi
local tag="FilePath"
ESD=$(xmllint --nonet --xpath "//$tag" "$dir/$eFile" | sed -E -e "s/<[\/]?$tag>//g")
if [ -z "$ESD" ]; then
error "Failed to find ESD URL in $eFile!" && return 1
fi
tag="Sha1"
ESD_SUM=$(xmllint --nonet --xpath "//$tag" "$dir/$eFile" | sed -E -e "s/<[\/]?$tag>//g")
tag="Size"
ESD_SIZE=$(xmllint --nonet --xpath "//$tag" "$dir/$eFile" | sed -E -e "s/<[\/]?$tag>//g")
rm -rf "$dir"
return 0
}
verifyFile() {
local iso="$1"
local size="$2"
local total="$3"
local check="$4"
if [ -n "$size" ] && [[ "$total" != "$size" ]] && [[ "$size" != "0" ]]; then
warn "The downloaded file has an unexpected size: $total bytes, while expected value was: $size bytes. Please report this at $SUPPORT/issues"
fi
local hash=""
local algo="SHA256"
[ -z "$check" ] && return 0
[[ "$VERIFY" != [Yy1]* ]] && return 0
[[ "${#check}" == "40" ]] && algo="SHA1"
local msg="Verifying downloaded ISO..."
info "$msg" && html "$msg"
if [[ "${algo,,}" != "sha256" ]]; then
hash=$(sha1sum "$iso" | cut -f1 -d' ')
else
hash=$(sha256sum "$iso" | cut -f1 -d' ')
fi
if [[ "$hash" == "$check" ]]; then
info "Succesfully verified ISO!" && return 0
fi
error "The downloaded file has an invalid $algo checksum: $hash , while expected value was: $check. Please report this at $SUPPORT/issues"
rm -f "$iso"
return 1
}
doMido() {
local iso="$1"
local version="$2"
local desc="$3"
local rc sum size total
rm -f "$iso"
rm -f "$iso.PART"
size=$(getMido "$version" "size")
sum=$(getMido "$version" "sum")
local msg="Downloading $desc..."
info "$msg" && html "$msg"
/run/progress.sh "$iso.PART" "$size" "Downloading $desc ([P])..." &
cd "$TMP"
{ /run/mido.sh "${version,,}"; rc=$?; } || :
cd /run
fKill "progress.sh"
if (( rc == 0 )) && [ -f "$iso" ]; then
total=$(stat -c%s "$iso")
if [ "$total" -gt 100000000 ]; then
! verifyFile "$iso" "$size" "$total" "$sum" && return 1
html "Download finished successfully..." && return 0
fi
fi
rm -f "$iso"
rm -f "$iso.PART"
return 1
}
downloadFile() {
local iso="$1"
local url="$2"
local sum="$3"
local size="$4"
local desc="$5"
local rc total progress domain dots
rm -f "$iso"
# Check if running with interactive TTY or redirected to docker log
if [ -t 1 ]; then
progress="--progress=bar:noscroll"
else
progress="--progress=dot:giga"
fi
local msg="Downloading $desc..."
html "$msg"
domain=$(echo "$url" | awk -F/ '{print $3}')
dots=$(echo "$domain" | tr -cd '.' | wc -c)
(( dots > 1 )) && domain=$(expr "$domain" : '.*\.\(.*\..*\)')
if [ -n "$domain" ] && [[ "${domain,,}" != *"microsoft.com" ]]; then
msg="Downloading $desc from $domain..."
fi
info "$msg"
/run/progress.sh "$iso" "$size" "Downloading $desc ([P])..." &
{ wget "$url" -O "$iso" -q --timeout=10 --show-progress "$progress"; rc=$?; } || :
fKill "progress.sh"
if (( rc == 0 )) && [ -f "$iso" ]; then
total=$(stat -c%s "$iso")
if [ "$total" -gt 100000000 ]; then
! verifyFile "$iso" "$size" "$total" "$sum" && return 1
html "Download finished successfully..." && return 0
fi
fi
error "Failed to download $url , reason: $rc"
rm -f "$iso"
return 1
}
downloadImage() {
local iso="$1"