Skip to content

Commit 06fbe02

Browse files
committed
timeout ipfsWget & check fix with flags
1 parent 7537868 commit 06fbe02

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

bash-utils/bash-utils.sh

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -642,54 +642,65 @@ function getLocalIp() {
642642
# Host list: https://ipfs.github.io/public-gateway-checker
643643
# Given file CID downloads content from a known public IPFS gateway
644644
# ipfsGet <file> <CID>
645+
# ipfsGet --file=<file> --cid=<CID> --timeout="30" --url="https://ipfs.example.com/ipfs"
645646
function ipfsGet() {
646-
local OUT_PATH=$1
647-
local FILE_CID=$2
648-
local PUB_URL=""
647+
local cid=""
648+
local file=""
649+
local url=""
650+
local timeout=""
649651

650-
local TIMEOUT=30
652+
getArgs --gargs_throw=false --gargs_verbose=false "$1" "$2" "$3" "$4"
651653

652-
if ($(isCID "$FILE_CID")) ; then
653-
echoInfo "INFO: Cleaning up '$OUT_PATH' and searching for available gatewys..."
654+
[ -z "$file" ] && file="$1"
655+
[ -z "$cid" ] && cid="$2"
656+
[ -z $timeout ] && timeout="30"
654657

655-
PUB_URL="https://gateway.ipfs.io/ipfs/${FILE_CID}"
656-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $TIMEOUT) -gt 1 ]] ) ; then
657-
wget --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$OUT_PATH" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from gateway.ipfs.io :("
658+
local PUB_URL=""
659+
local DOWNLOAD_SUCCESS="false"
660+
661+
if ($(isCID "$cid")) ; then
662+
echoInfo "INFO: Cleaning up '$file' and searching for available gatewys..."
663+
664+
if [ ! -z "$url" ] ; then
665+
PUB_URL="${url}/${cid}"
666+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $timeout) -gt 1 ]] ) ; then
667+
wget --timeout="$timeout" --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$file" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ${url} :("
668+
fi
658669
fi
659670

660-
PUB_URL="https://dweb.link/ipfs/${FILE_CID}"
661-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $TIMEOUT) -gt 1 ]] ) ; then
662-
wget --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$OUT_PATH" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from dweb.link :("
671+
PUB_URL="https://gateway.ipfs.io/ipfs/${cid}"
672+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $timeout) -gt 1 ]] ) ; then
673+
wget --timeout="$timeout" --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$file" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from gateway.ipfs.io :("
663674
fi
664675

665-
PUB_URL="https://ipfs.joaoleitao.org/ipfs/${FILE_CID}"
666-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $TIMEOUT) -gt 1 ]] ) ; then
667-
wget --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$OUT_PATH" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.joaoleitao.org :("
676+
PUB_URL="https://dweb.link/ipfs/${cid}"
677+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $timeout) -gt 1 ]] ) ; then
678+
wget --timeout="$timeout" --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$file" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from dweb.link :("
668679
fi
669680

670-
PUB_URL="https://ipfs.kira.network/ipfs/${FILE_CID}"
671-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $TIMEOUT) -gt 1 ]] ) ; then
672-
wget --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$OUT_PATH" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.joaoleitao.org :("
681+
PUB_URL="https://ipfs.joaoleitao.org/ipfs/${cid}"
682+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $timeout) -gt 1 ]] ) ; then
683+
wget --timeout="$timeout" --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$file" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.joaoleitao.org :("
673684
fi
674685

675-
PUB_URL="https://ipfs.kira.network/ipfs/${FILE_CID}"
676-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $TIMEOUT) -gt 1 ]] ) ; then
677-
wget --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$OUT_PATH" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.joaoleitao.org :("
686+
PUB_URL="https://ipfs.kira.network/ipfs/${cid}"
687+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $timeout) -gt 1 ]] ) ; then
688+
wget --timeout="$timeout" --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$file" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.kira.network :("
678689
fi
679690

680-
PUB_URL="https://ipfs.snggle.com/ipfs/${FILE_CID}"
681-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $TIMEOUT) -gt 1 ]] ) ; then
682-
wget --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$OUT_PATH" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.joaoleitao.org :("
691+
PUB_URL="https://ipfs.snggle.com/ipfs/${cid}"
692+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] && [[ $(urlContentLength "$PUB_URL" $timeout) -gt 1 ]] ) ; then
693+
wget --timeout="$timeout" --user-agent="$UBUNTU_AGENT" "$PUB_URL" -O "$file" && DOWNLOAD_SUCCESS="true" || echoWarn "WARNING: Faild download from ipfs.snggle.com :("
683694
fi
684695

685-
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] || [ ! -f "$OUT_PATH" ] ) ; then
686-
echoErr "ERROR: Failed to locate or download '$FILE_CID' file from any public IPFS gateway :("
696+
if ( [ "$DOWNLOAD_SUCCESS" != "true" ] || [ ! -f "$file" ] ) ; then
697+
echoErr "ERROR: Failed to locate or download '$cid' file from any public IPFS gateway :("
687698
return 1
688699
else
689-
echoInfo "INFO: Success, file '$FILE_CID' was downloaded to '$OUT_PATH' from '$PUB_URL'"
700+
echoInfo "INFO: Success, file '$cid' was downloaded to '$file' from '$PUB_URL'"
690701
fi
691702
else
692-
echoErr "ERROR: Specified file CID '$FILE_CID' is NOT valid"
703+
echoErr "ERROR: Specified file CID '$cid' is NOT valid"
693704
return 1
694705
fi
695706
}
@@ -726,15 +737,15 @@ function safeWget() {
726737
local DOWNLOAD_SUCCESS="false"
727738

728739
if (! $(isCommand cosign)) ; then
729-
echoErr "ERROR: Cosign tool is not installed, please install version v1.13.1 or later."
740+
echoErr "ERROR: Cosign tool is not installed, please install version v2.0.0 or later."
730741
return 1
731742
fi
732743

733744
if (! $(isSHA256 "$EXPECTED_HASH_FIRST")) ; then
734745
if ($(isCID "$EXPECTED_HASH_FIRST")) ; then
735746
echoInfo "INFO: Detected IPFS CID, searching available gatewys..."
736747
COSIGN_PUB_KEY="$TMP_PATH_PUB"
737-
ipfsGet "$COSIGN_PUB_KEY" "$EXPECTED_HASH_FIRST"
748+
ipfsGet --file="$COSIGN_PUB_KEY" --cid="$EXPECTED_HASH_FIRST" --timeout="30"
738749

739750
if ($(isFileEmpty $COSIGN_PUB_KEY)); then
740751
echoErr "ERROR: Failed to locate or download public key file '$EXPECTED_HASH_FIRST' from any public IPFS gateway :("

bash-utils/scripts/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ if ($(isFileEmpty $TEST_FILE)); then
100100
fi
101101

102102
rm -fv $TEST_FILE
103-
ipfsGet "$TEST_FILE" "QmNPG6RQSDa6jKqaPbNDyP9iM9CRxmv1kaHaPMCw2aQcec" || :
103+
ipfsGet --timeout="30" --file="$TEST_FILE" --cid="QmNPG6RQSDa6jKqaPbNDyP9iM9CRxmv1kaHaPMCw2aQcec" --url="https://ipfs.snggle.com/ipfs" || :
104104

105105
if (! $(isFileEmpty $TEST_FILE)); then
106106
echoErr "ERROR: Expected non existent file to be empty when attempted to be downloaded from IPFS"

0 commit comments

Comments
 (0)