Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions scripts/check-ipfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

######################################################

# Can change if you want!

# Timeout for curl requests in seconds
TIMEOUT=5

# Gateways to check if file is already hosted on IPFS
DEFAULT_GATEWAY_1="https://ipfs.io/ipfs/"
DEFAULT_GATEWAY_2="https://gateway.pinata.cloud/ipfs/"
# DEFAULT_GATEWAY_3="https://w3s.link/ipfs"

# Other gateways like Dweb.link, 4everland, w3s.link don't seem to work well with curl
# in future we can fix them by reading the https header and checking if the file is content is returned

# for more gateways see:
# https://ipfs.github.io/public-gateway-checker/

######################################################

# check if user has ipfs cli installed
if ! command -v ipfs >/dev/null 2>&1; then
echo "Error: ipfs cli is not installed or not in your PATH." >&2
exit 1
fi

# Usage message
usage() {
echo "Usage: $0 <file>"
echo "Check if a file is discoverable via free IPFS gateways"
echo " "
echo "Options:"
echo " <file> Path to your file."
exit 1
}

# Initialize variables with defaults
input_path=""

# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
*)
if [ -z "$input_path" ]; then
input_path="$1"
fi
shift
;;
esac
done

# Generate CID from the given file
echo "Generating CID for the file..."

# use ipfs add to generate a CID
# use CIDv1
ipfs_cid=$(ipfs add -Q --cid-version 1 "$input_path")
echo "CID: $ipfs_cid"

check_file_via_gateway() {
local gateway="$1"
local cid="$2"
local timeout="$3"
echo " "
echo "Checking ${gateway}..."
if curl --silent --fail --max-time $timeout "${gateway}${cid}" >/dev/null; then
echo "File is accessible on IPFS via ${gateway}${cid}"
return 0
else
echo "File not found at: ${gateway}${cid}"
return 1
fi
}

# If file can be found via gateways then exit
if check_file_via_gateway "$DEFAULT_GATEWAY_1" "$ipfs_cid" "$TIMEOUT"; then
exit 0
fi
if check_file_via_gateway "$DEFAULT_GATEWAY_2" "$ipfs_cid" "$TIMEOUT"; then
exit 0
fi

# todo: add more gateways

# If file cannot be found via gateways then exit
echo " "
echo "File is cannot be found via gateways. Exiting."
exit 1
77 changes: 21 additions & 56 deletions scripts/ipfs.sh → scripts/pin-to-ipfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

# Can change if you want!

# used to by pass waiting for gateway checks
JUST_PIN="false"
JUST_CHECK="false"

# Gateways to check if file is already hosted on IPFS
DEFAULT_GATEWAY_1="https://ipfs.io/ipfs/"
DEFAULT_GATEWAY_2="https://gateway.pinata.cloud/ipfs/"
# Default behavior is to not check if file is discoverable on IPFS
CHECK_TOO="false"

# Pinning services to host the file on IPFS
DEFAULT_HOST_ON_LOCAL_NODE="true"
Expand All @@ -31,24 +26,22 @@ fi

# Usage message
usage() {
echo "Usage: $0 <file> [--just-pin] [--just-check] [--no-local] [--no-pinata] [--no-blockfrost] [--no-nmkr]"
echo "Check if a file is on IPFS, and also pin it locally and via Blockfrost and NMKR."
echo "Usage: $0 <file> [--check-too] [--no-local] [--no-pinata] [--no-blockfrost] [--no-nmkr]"
echo "Pin a file to local IPFS node and pin via Blockfrost, NMKR and Pinata. Optionally check if file is already discoverable on IPFS."
echo " "
echo "Options:"
echo " <file> Path to your file."
echo " --just-pin Don't look for the file, just pin it (default: $JUST_PIN)"
echo " --just-check Only look for the file don't try to pin it (default: $JUST_CHECK)"
echo " --no-local Don't try to pin file on local ipfs node? (default: $DEFAULT_HOST_ON_LOCAL_NODE)"
echo " --no-pinata Don't try to pin file on pinata service? (default: $DEFAULT_HOST_ON_PINATA)"
echo " --no-blockfrost Don't try to pin file on blockfrost service? (default: $DEFAULT_HOST_ON_BLOCKFROST)"
echo " --no-nmkr Don't try to pin file on NMKR service? (default: $DEFAULT_HOST_ON_NMKR_STORAGE)"
echo " --check-too Run a check if file is discoverable on ipfs, only pin if not discoverable (default: $CHECK_TOO)"
echo " --no-local Don't try to pin file on local ipfs node (default: $DEFAULT_HOST_ON_LOCAL_NODE)"
echo " --no-pinata Don't try to pin file on pinata service (default: $DEFAULT_HOST_ON_PINATA)"
echo " --no-blockfrost Don't try to pin file on blockfrost service (default: $DEFAULT_HOST_ON_BLOCKFROST)"
echo " --no-nmkr Don't try to pin file on NMKR service (default: $DEFAULT_HOST_ON_NMKR)"
exit 1
}

# Initialize variables with defaults
input_path=""
just_pin="$JUST_PIN"
just_check="$JUST_CHECK"
check_discoverable="$CHECK_TOO"
local_host="$DEFAULT_HOST_ON_LOCAL_NODE"
pinata_host="$DEFAULT_HOST_ON_PINATA"
blockfrost_host="$DEFAULT_HOST_ON_BLOCKFROST"
Expand All @@ -57,12 +50,8 @@ nmkr_host="$DEFAULT_HOST_ON_NMKR"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--just-pin)
just_pin="true"
shift
;;
--just-check)
just_check="true"
--check-too)
check_discoverable="true"
shift
;;
--no-local)
Expand Down Expand Up @@ -101,48 +90,24 @@ echo "Generating CID for the file..."
ipfs_cid=$(ipfs add -Q --cid-version 1 "$input_path")
echo "CID: $ipfs_cid"

# check two gateways if file can be accessed
echo " "
echo "Checking if file is already hosted on IPFS..."

check_file_on_gateway() {
local gateway="$1"
local cid="$2"
local timeout="$3"
echo "Checking ${gateway}..."
if curl --silent --fail "${gateway}${cid}" >/dev/null; then
echo "File is accessible on IPFS via ${gateway}${cid}"
return 0
# If user wants to check if file is discoverable on IPFS
if [ "$check_discoverable" = "true" ]; then
echo "Using ./scripts/check-ipfs.sh script to check if file is discoverable on IPFS..."
# check if file is discoverable on IPFS
if ! ./scripts/check-ipfs.sh "$input_path"; then
echo "File is not discoverable on IPFS. Proceeding to pin it."
else
echo "File not found at: ${gateway}${cid}"
return 1
fi
}

# If file can be found via gateways then exit
if [ "$just_pin" = "false" ]; then
echo "Checking if file is already hosted on IPFS..."
if check_file_on_gateway "$DEFAULT_GATEWAY_1" "$ipfs_cid" "TIMEOUT"; then
echo "File is already hosted on IPFS. No need to pin anywhere else."
exit 0
fi
if check_file_on_gateway "$DEFAULT_GATEWAY_2" "$ipfs_cid" "TIMEOUT"; then
echo "File is already hosted on IPFS. No need to pin anywhere else."
echo "File is already discoverable on IPFS. No need to pin it."
exit 0
fi

else
echo "Skipping check of file on ipfs..."
fi

# If just checking then exit
if [ "$just_check" = "true" ]; then
echo "File is not hosted on IPFS, but you requested to just check. Exiting."
exit 0
fi

# If file is not accessible then pin it!!
echo " "
echo "File is not hosted on IPFS, so pinning it..."
echo " "

# Pin on local node
if [ "$local_host" = "true" ]; then
Expand Down