-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathipfs-pin.sh
More file actions
executable file
·367 lines (321 loc) · 12.9 KB
/
ipfs-pin.sh
File metadata and controls
executable file
·367 lines (321 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
######################################################
# Can change if you want!
# Default behavior is to not check if file is discoverable on IPFS
CHECK_TOO="false"
JUST_JSONLD="false"
# Pinning services to host the file on IPFS
DEFAULT_HOST_ON_LOCAL_NODE="true"
DEFAULT_HOST_ON_NMKR="true"
DEFAULT_HOST_ON_BLOCKFROST="true"
DEFAULT_HOST_ON_PINATA="true"
# HOST_ON_STORACHA_STORAGE="true"
# https://docs.storacha.network/faq/
######################################################
# Exit immediately if a command exits with a non-zero status,
# treat unset variables as an error, and fail if any command in a pipeline fails
set -euo pipefail
# Colors
#BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BRIGHTWHITE='\033[0;37;1m'
NC='\033[0m'
UNDERLINE='\033[4m'
BOLD='\033[1m'
GRAY='\033[0;90m'
# check if user has ipfs cli installed
if ! command -v ipfs >/dev/null 2>&1; then
echo -e "${RED}Error: ipfs cli is not installed or not in your PATH.${NC}" >&2
exit 1
fi
# Usage message
usage() {
local col=50
echo -e "${UNDERLINE}${BOLD}Pin files to local IPFS node and via Blockfrost, NMKR and Pinata${NC}"
echo -e "\n"
echo -e "Syntax:${BOLD} $0 ${GREEN}<file|directory>${NC} [${GREEN}--check-too${NC}] [${GREEN}--no-local${NC}] [${GREEN}--no-pinata${NC}] [${GREEN}--no-blockfrost${NC}] [${GREEN}--no-nmkr${NC}]"
printf "Params: ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "<file|directory>" "- Path to your file or directory containing files"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--just-jsonld]" "- If a directory is provided, only .jsonld files will be processed (default: $JUST_JSONLD)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--check-too]" "- Run a check if file is discoverable on ipfs, only pin if not discoverable (default: $CHECK_TOO)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--no-local]" "- Don't try to pin file on local ipfs node (default: $DEFAULT_HOST_ON_LOCAL_NODE)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--no-pinata]" "- Don't try to pin file on pinata service (default: $DEFAULT_HOST_ON_PINATA)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--no-blockfrost]" "- Don't try to pin file on blockfrost service (default: $DEFAULT_HOST_ON_BLOCKFROST)"
printf " ${GREEN}%-*s${NC}${GRAY}%s${NC}\n" $((col-8)) "[--no-nmkr]" "- Don't try to pin file on NMKR service (default: $DEFAULT_HOST_ON_NMKR)"
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "-h, --help" "- Show this help message and exit"
exit 1
}
# Initialize variables with defaults
input_path=""
check_discoverable="$CHECK_TOO"
just_jsonld="$JUST_JSONLD"
local_host="$DEFAULT_HOST_ON_LOCAL_NODE"
pinata_host="$DEFAULT_HOST_ON_PINATA"
blockfrost_host="$DEFAULT_HOST_ON_BLOCKFROST"
nmkr_host="$DEFAULT_HOST_ON_NMKR"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--check-too)
check_discoverable="true"
shift
;;
--just-jsonld)
just_jsonld="true"
shift
;;
--no-local)
local_host="false"
shift
;;
--no-pinata)
pinata_host="false"
shift
;;
--no-blockfrost)
blockfrost_host="false"
shift
;;
--no-nmkr)
nmkr_host="false"
shift
;;
-h|--help)
usage
;;
*)
if [ -z "$input_path" ]; then
input_path="$1"
fi
shift
;;
esac
done
# If no input path provided, show usage
if [ -z "$input_path" ]; then
echo -e "${RED}Error: No file or directory specified${NC}" >&2
usage
fi
echo -e " "
echo -e "${CYAN}IPFS File Pinning Service${NC}"
echo -e "${CYAN}This script pins files to IPFS using multiple pinning services${NC}"
# if pinata is enabled ensure the API key is set
if [ "$pinata_host" = "true" ]; then
if [ -z "${PINATA_API_KEY:-}" ]; then
echo -e "${RED}Error: PINATA_API_KEY environment variable is not set, but pinning to Pinata is enabled.${NC}" >&2
exit 1
fi
fi
# if blockfrost is enabled ensure the API key is set
if [ "$blockfrost_host" = "true" ]; then
if [ -z "${BLOCKFROST_API_KEY:-}" ]; then
echo -e "${RED}Error: BLOCKFROST_API_KEY environment variable is not set, but pinning to Blockfrost is enabled.${NC}" >&2
exit 1
fi
fi
# if nmkr is enabled ensure the API key and user id is set
if [ "$nmkr_host" = "true" ]; then
if [ -z "${NMKR_API_KEY:-}" ]; then
echo -e "${RED}Error: NMKR_API_KEY environment variable is not set, but pinning to NMKR is enabled.${NC}" >&2
exit 1
fi
if [ -z "${NMKR_USER_ID:-}" ]; then
echo -e "${RED}Error: NMKR_USER_ID environment variable is not set, but pinning to NMKR is enabled.${NC}" >&2
exit 1
fi
fi
# Function to pin a single file
pin_single_file() {
local file="$1"
echo -e " "
echo -e "${CYAN}Processing file: ${YELLOW}$file${NC}"
# Generate CID from the given file
echo -e "${CYAN}Generating CID for the file...${NC}"
# use ipfs add to generate a CID
# use CIDv1
ipfs_cid=$(ipfs add -Q --cid-version 1 "$file")
echo -e "CID: ${YELLOW}$ipfs_cid${NC}"
# If user wants to check if file is discoverable on IPFS
if [ "$check_discoverable" = "true" ]; then
echo -e "${CYAN}Using ./scripts/ipfs-check.sh script to check if file is discoverable on IPFS...${NC}"
# check if file is discoverable on IPFS
if ! ./scripts/ipfs-check.sh "$file"; then
echo -e "${YELLOW}File is not discoverable on IPFS. Proceeding to pin it.${NC}"
else
echo -e "${GREEN}File is already discoverable on IPFS. No need to pin it.${NC}"
return 0
fi
else
echo -e "${CYAN}Skipping check of file on ipfs...${NC}"
fi
echo -e " "
echo -e "${CYAN}File is not hosted on IPFS, so pinning it...${NC}"
# Pin on local node
if [ "$local_host" = "true" ]; then
echo -e " "
echo -e "${CYAN}Pinning file on local IPFS node...${NC}"
if ipfs pin add "$ipfs_cid"; then
echo -e "${GREEN}File pinned successfully on local IPFS node.${NC}"
else
echo -e "${RED}Failed to pin file on local IPFS node.${NC}" >&2
return 1
fi
else
echo -e "${YELLOW}Skipping pinning on local IPFS node.${NC}"
fi
# Pin on Pinata
if [ "$pinata_host" = "true" ]; then
echo -e " "
echo -e "${CYAN}Pinning file to Pinata...${NC}"
# Check for secret environment variables
echo -e "${CYAN}Reading Pinata API key from environment variable...${NC}"
if [ -z "$PINATA_API_KEY" ]; then
echo -e "${RED}Error: PINATA_API_KEY environment variable is not set.${NC}" >&2
return 1
fi
echo -e "${CYAN}Uploading file to Pinata service...${NC}"
response=$(curl -s -X POST "https://uploads.pinata.cloud/v3/files" \
-H "Authorization: Bearer ${PINATA_API_KEY}" \
-F "file=@$file" \
-F "network=public" \
)
# Check response for errors
if echo "$response" | grep -q '"errors":'; then
echo -e "${RED}Error in Pinata response:${NC}" >&2
echo "$response" | jq . >&2
return 1
fi
echo -e "${GREEN}Pinata upload successful!${NC}"
else
echo -e "${YELLOW}Skipping pinning on Pinata.${NC}"
fi
# Pin on Blockfrost
if [ "$blockfrost_host" = "true" ]; then
echo -e " "
echo -e "${CYAN}Pinning file to Blockfrost...${NC}"
# Check for secret environment variables
echo -e "${CYAN}Reading Blockfrost API key from environment variable...${NC}"
if [ -z "$BLOCKFROST_API_KEY" ]; then
echo -e "${RED}Error: BLOCKFROST_API_KEY environment variable is not set.${NC}" >&2
return 1
fi
echo -e "${CYAN}Uploading file to Blockfrost service...${NC}"
response=$(curl -s -X POST "https://ipfs.blockfrost.io/api/v0/ipfs/add" \
-H "project_id: $BLOCKFROST_API_KEY" \
-F "file=@$file" \
)
# Check response for errors
if echo "$response" | grep -q '"errors":'; then
echo -e "${RED}Error in Blockfrost response:${NC}" >&2
echo "$response" | jq . >&2
return 1
fi
echo -e "${GREEN}Blockfrost upload successful!${NC}"
else
echo -e "${YELLOW}Skipping pinning on Blockfrost.${NC}"
fi
# Pin on NMKR
if [ "$nmkr_host" = "true" ]; then
echo -e " "
echo -e "${CYAN}Pinning file to NMKR...${NC}"
# Check for secret environment variables
echo -e "${CYAN}Reading NMKR API key from environment variable...${NC}"
if [ -z "$NMKR_API_KEY" ]; then
echo -e "${RED}Error: NMKR_API_KEY environment variable is not set.${NC}" >&2
return 1
fi
echo -e "${CYAN}Reading NMKR user id from environment variable...${NC}"
if [ -z "$NMKR_USER_ID" ]; then
echo -e "${RED}Error: NMKR_USER_ID environment variable is not set.${NC}" >&2
return 1
fi
# base64 encode the file because NMKR API requires it
echo -e "${CYAN}Encoding file to base64...${NC}"
base64_content=$(base64 -i "$file")
echo -e "${CYAN}Uploading file to NMKR service...${NC}"
response=$(curl -s -X POST "https://studio-api.nmkr.io/v2/UploadToIpfs/${NMKR_USER_ID}" \
-H 'accept: text/plain' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${NMKR_API_KEY}" \
-d @- <<EOF
{
"fileFromBase64": "$base64_content",
"name": "$(basename "$file")",
"mimetype": "application/json"
}
EOF
)
# Check response for errors
if echo "$response" | grep -q '"errors":'; then
echo -e "${RED}Error in NMKR response:${NC}" >&2
echo "$response" | jq . >&2
return 1
fi
echo -e "${GREEN}NMKR upload successful!${NC}"
else
echo -e "${YELLOW}Skipping pinning on NMKR.${NC}"
fi
echo -e " "
echo -e "${GREEN}File pinning completed: ${YELLOW}$file${NC}"
echo -e "CID: ${YELLOW}$ipfs_cid${NC}"
}
# Main processing logic
if [ -d "$input_path" ]; then
# If input is a directory: pin files (optionally only .jsonld files) including subdirectories
echo -e " "
echo -e "${CYAN}Processing directory: ${YELLOW}$input_path${NC}"
# if just jsonld is true, only process .jsonld files
files_to_process=()
if [ "$just_jsonld" = "true" ]; then
# Only .jsonld files
while IFS= read -r -d '' file; do
files_to_process+=("$file")
done < <(find "$input_path" -type f -name "*.jsonld" -print0)
else
# else do all files
while IFS= read -r -d '' file; do
files_to_process+=("$file")
done < <(find "$input_path" -type f -print0)
fi
# check if any files were found
if [ ${#files_to_process[@]} -eq 0 ]; then
if [ "$just_jsonld" = "true" ]; then
echo -e "${RED}Error: No .jsonld files found in directory (including subdirectories): ${YELLOW}$input_path${NC}" >&2
else
echo -e "${RED}Error: No files found in directory (including subdirectories): ${YELLOW}$input_path${NC}" >&2
fi
exit 1
fi
echo -e "${CYAN}Found ${YELLOW}${#files_to_process[@]}${NC}${CYAN} files to process${NC}"
# for each file in the directory, pin it
for file in "${files_to_process[@]}"; do
# ask user if they want to continue with the next file
# skip for the first file
if [ "$file" != "${files_to_process[0]}" ]; then
echo -e " "
echo -e "${CYAN}The next file is: ${YELLOW}$file${NC}"
read -p "Do you want to continue with the next file? (y/n): " choice
case "$choice" in
y|Y ) echo -e "${GREEN}Continuing with the next file...${NC}";;
n|N ) echo -e "${YELLOW}Exiting...${NC}"; exit 0;;
* ) echo -e "${RED}Invalid choice, exiting...${NC}"; exit 1;;
esac
fi
pin_single_file "$file"
done
echo -e " "
echo -e "${GREEN}All files processed successfully!${NC}"
elif [ -f "$input_path" ]; then
# Input is a single file
echo -e " "
echo -e "${CYAN}Processing single file: ${YELLOW}$input_path${NC}"
pin_single_file "$input_path"
echo -e " "
echo -e "${GREEN}File processed successfully!${NC}"
else
echo -e "${RED}Error: '$input_path' is not a valid file or directory.${NC}" >&2
exit 1
fi