custodisco-kiosk/ssb-post.sh

147 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# custodisco posting
#GIVE and MINT options
## ok we need to be passed a number of arguments
## item description/GIVE to account ID = $2
## image path/messageID of existing item = $3
if [ -z "$1" ]
then
echo "need arguments pls"
fi
## mint
if [ "$1" == "mint" ]
then
itemDescription=$2
itemImagePath=$3
# remove meta-data from the image and compress a bit
jpegoptim --strip-all --overwrite "$itemImagePath" --size=200k > /dev/null 2>&1
#add blob
blobID=$(cat $itemImagePath | ssb-server blobs.add) || exit 1
# publish the item
ssb-server publish . <<BLAAB
{
"type":"post",
"text":"![photo.jpg]($blobID)\\n\\n$itemDescription\\n\\n a #custodisco item ",
"custodisco":"true",
"nft": "mint",
"mentions": [
{
"name": "photo.jpg",
"type": "image/jpeg",
"link": "$blobID"
}
]
}
BLAAB
fi
## give
if [ "$1" == "give" ]
then
account=$2
messageID=$3
ssb-server publish . <<BLAAB
{
"type":"post",
"text":"This item is stewarded by $account",
"custodisco":"true",
"nft": "give",
"target": "$account",
"root": "$messageID",
"branch": "$messageID"
}
BLAAB
fi
## get_message_content
if [ "$1" == "get_message_content" ]
then
messageID=$2
echo "Attempting to retrieve message: $messageID" >&2
# Get message content
message_content=$(ssb-server get "$messageID" 2>&1)
# Check if ssb-server get was successful
if [ $? -ne 0 ]; then
echo "Error: ssb-server get failed" >&2
echo "$message_content" >&2
exit 1
fi
echo "Successfully retrieved message content" >&2
# Extract blob information
blobs=$(echo "$message_content" | jq -r '.content.mentions[] | select(.link | startswith("&")) | .link')
# Process blobs
if [ -n "$blobs" ]; then
echo "Processing blobs..." >&2
echo "$message_content" | jq -c '.content.mentions[]' | while read -r mention; do
blob=$(echo "$mention" | jq -r '.link')
if [[ "$blob" == \&* ]]; then
echo "Found a blob: $blob" >&2
# First, try to get the mime type from the message content
mime_type=$(echo "$mention" | jq -r '.type')
# If mime type is not in the message content, try to get it from blobs.meta
if [[ "$mime_type" == "null" || -z "$mime_type" ]]; then
if ssb-server blobs.has "$blob"; then
mime_type=$(ssb-server blobs.meta "$blob" | jq -r '.type')
else
echo "Blob not available locally: $blob" >&2
continue
fi
fi
echo "Blob mime type: $mime_type" >&2
if [[ "$mime_type" == "image/jpeg" || "$mime_type" == "image/png" ]]; then
# Create tmp directory if it doesn't exist
mkdir -p tmp
# Get blob and save it to a file
file_extension="${mime_type#image/}"
short_name=$(echo "$blob" | md5sum | cut -d' ' -f1)
file_name="${short_name}.$file_extension"
full_path="tmp/$file_name"
echo "Attempting to save blob to: $full_path" >&2
echo "Executing command: ssb-server blobs.get $blob > $full_path" >&2
if ssb-server blobs.get "$blob" > "$full_path" 2>/dev/null; then
echo "Blob saved: $full_path" >&2
echo "IMAGE_PATH:$full_path"
else
echo "Failed to retrieve blob: $blob" >&2
echo "ssb-server blobs.get exit code: $?" >&2
# If the file wasn't created, let's try to output the blob data directly
echo "Attempting to output blob data directly:" >&2
ssb-server blobs.get "$blob" | head -c 100 | xxd >&2
fi
else
echo "Skipping non-image blob: $blob (type: $mime_type)" >&2
fi
fi
done
else
echo "No blobs found in the message." >&2
fi
# Output message content
echo "$message_content"
fi