-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload2ipfs.sh
executable file
·171 lines (142 loc) · 5.39 KB
/
upload2ipfs.sh
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
#!/bin/bash
####################################################
## Receives a file and a temp file path
## Add to IPFS produce a json with all details
######################################################
## NIP-96/NIP-94 compatibility
## Add https://domain.tld/.well-known/nostr/nip96.json
## { "api_url": "https://u.domain.tld/upload2ipfs" }
####################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
ME="${0##*/}"
[[ -s "${HOME}/.zen/Astroport.ONE/tools/my.sh" ]] \
&& source "${HOME}/.zen/Astroport.ONE/tools/my.sh"
FILE_PATH="$1"
OUTPUT_FILE="$2"
if [ -z "$FILE_PATH" ]; then
echo '{"status": "error", "message": "No file path provided.", "debug": "FILE_PATH is empty"}' > "$OUTPUT_FILE"
exit 1
fi
if [ -z "$OUTPUT_FILE" ]; then
echo '{"status": "error", "message": "No temporary file path provided.", "debug": "OUTPUT_FILE is empty"}' > "$OUTPUT_FILE"
exit 1
fi
if [ ! -f "$FILE_PATH" ]; then
echo '{"status": "error", "message": "File not found.", "debug": "File does not exist"}' > "$OUTPUT_FILE"
exit 1
fi
# Get file information
FILE_SIZE=$(stat -c%s "$FILE_PATH")
FILE_TYPE=$(file -b --mime-type "$FILE_PATH")
FILE_NAME=$(basename "$FILE_PATH")
# Log file information
echo "DEBUG: FILE_SIZE: $FILE_SIZE, FILE_TYPE: $FILE_TYPE, FILE_NAME: $FILE_NAME" >&2
# Check if file size exceeds 100MB
MAX_FILE_SIZE=$((100 * 1024 * 1024)) # 100MB in bytes
if [ "$FILE_SIZE" -gt "$MAX_FILE_SIZE" ]; then
echo '{"status": "error", "message": "File size exceeds 100MB limit.", "debug": "File too large", "fileSize": "'"$FILE_SIZE"'"}' > "$OUTPUT_FILE"
exit 1
fi
# Attempt to add the file to IPFS and capture the output
CID_OUTPUT=$(ipfs add -wq "$FILE_PATH" 2>&1)
# Extract the CID
CID=$(echo "$CID_OUTPUT" | tail -n 1)
# Check if ipfs command worked
if [ -z "$CID" ]; then
echo '{"status": "error", "message": "IPFS add failed.", "debug": "CID is empty", "ipfs_output": "'"$CID_OUTPUT"'"}' > "$OUTPUT_FILE"
exit 1
fi
# Log the CID
echo "DEBUG: CID: $CID" >&2
# Get current date
DATE=$(date +"%Y-%m-%d %H:%M %z")
# Initialize the description
DESCRIPTION=""
# Initialize additional nip94 tags
NIP94_TAGS=""
# Text file check
if [[ "$FILE_TYPE" == "text/"* ]]; then
DESCRIPTION="Plain text file" && IDISK="text"
# PDF file check
elif [[ "$FILE_TYPE" == "application/pdf" ]]; then
DESCRIPTION="PDF Document" && IDISK="pdf"
# Image file check
elif [[ "$FILE_TYPE" == "image/"* ]]; then
IMAGE_DIMENSIONS=$(identify -format "%wx%h" "$FILE_PATH" 2>/dev/null)
DESCRIPTION="Image, Dimensions: $IMAGE_DIMENSIONS"
IDISK="image"
NIP94_TAGS="$NIP94_TAGS, [\"dim\", \"$IMAGE_DIMENSIONS\"]"
# Video file check (using ffprobe)
elif [[ "$FILE_TYPE" == "video/"* ]]; then
if command -v ffprobe &> /dev/null; then
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$FILE_PATH" 2>/dev/null)
if [[ -z "$DURATION" ]]; then
DURATION="0"
fi
VIDEO_CODECS=$(ffprobe -v error -select_streams v -show_entries stream=codec_name -of csv=p=0 "$FILE_PATH" 2>/dev/null | sed -z 's/\n/, /g;s/, $//')
VIDEO_DIMENSIONS=$(ffprobe -v error -select_streams v -show_entries stream=width,height -of csv=s=x:p=0 "$FILE_PATH" 2>/dev/null)
DESCRIPTION="Video, Duration: $DURATION seconds, Codecs: $VIDEO_CODECS"
IDISK="video"
if [[ -n "$VIDEO_DIMENSIONS" ]]; then
NIP94_TAGS="$NIP94_TAGS, [\"dim\", \"$VIDEO_DIMENSIONS\"]"
fi
else
DURATION="0"
DESCRIPTION="Video, Could not get duration (ffprobe missing)"
fi
# Audio file check (using ffprobe)
elif [[ "$FILE_TYPE" == "audio/"* ]]; then
if command -v ffprobe &> /dev/null; then
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$FILE_PATH" 2>/dev/null)
if [[ -z "$DURATION" ]]; then
DURATION="0"
fi
AUDIO_CODECS=$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of csv=p=0 "$FILE_PATH" 2>/dev/null | sed -z 's/\n/, /g;s/, $//')
DESCRIPTION="Audio, Duration: $DURATION seconds, Codecs: $AUDIO_CODECS"
IDISK="audio"
else
DURATION="0"
DESCRIPTION="Audio, Could not get duration (ffprobe missing)"
fi
# Generic file check
else
DESCRIPTION="Other file type"
IDISK="other"
fi
# Calculate file hash (ox)
FILE_HASH=$(sha256sum "$FILE_PATH" | awk '{print $1}')
# Construct JSON output
NIP94_JSON="{
\"tags\": [
[\"url\", \"$myIPFS/ipfs/$CID/$FILE_NAME\" ],
[\"x\", \"$FILE_HASH\" ],
[\"ox\", \"$FILE_HASH\" ],
[\"m\", \"$FILE_TYPE\"]
$NIP94_TAGS
],
\"content\": \"\"
}"
JSON_OUTPUT="{
\"status\": \"success\",
\"message\": \"Upload successful.\",
\"nip94_event\": $NIP94_JSON,
\"created\": \"$(date -u +"%Y%m%d%H%M%S%4N")\",
\"cid\": \"$CID\",
\"mimeType\": \"$FILE_TYPE\",
\"duration\": ${DURATION:-0},
\"fileSize\": ${FILE_SIZE:-0},
\"fileName\": \"$FILE_NAME\",
\"unode\": \"$IPFSNODEID\",
\"date\": \"$DATE\",
\"description\": \"$DESCRIPTION\",
\"text\": \"$TEXT\",
\"title\": \"\$:/$IDISK/$CID/$FILE_NAME\"
}"
# Log JSON output to stderr before writing to temp file
echo "DEBUG: JSON_OUTPUT: $JSON_OUTPUT" >&2
# UNPIN
ipfs pin rm "$CID" >&2 ## UNPIN
# Write the JSON to the temp file
echo "$JSON_OUTPUT" > "$OUTPUT_FILE"
exit 0