feat: migrate `check_tool`

Co-authored-by: NotAShelf <raf@notashelf.dev>
pull/249/head
Sylvie 1 year ago
parent 9205f18b28
commit f4f5997a0b
No known key found for this signature in database
GPG Key ID: 75AB0FE5B983A3AF

@ -13,11 +13,16 @@
# ! Upload mode (ass=0,cheek=1) # ! Upload mode (ass=0,cheek=1)
MODE=0 MODE=0
# Function to check if a tool is installed
check_tool() {
command -v "$1" >/dev/null 2>&1
}
# Mode string switcher # Mode string switcher
get_mode() { get_mode() {
if [[ $MODE -eq 0 ]]; if [[ $MODE -eq 0 ]];
then echo "ass" then echo "ass"
else echo "cheek" else echo "cheek"
fi fi
} }
@ -29,42 +34,56 @@ FILE="$IMGPATH/$(get_mode)-$(date +%s).png"
TOKEN=$(cat $IMGPATH/.token) TOKEN=$(cat $IMGPATH/.token)
DOMAIN=$(cat $IMGPATH/.domain) DOMAIN=$(cat $IMGPATH/.domain)
# Build dynamic Flameshot user-agent takeScreenshot() {
USERAGENT=$(flameshot -v | sed -n -E 's/(Flameshot) (v[0-9]+\.[0-9]+\.[0-9]+) .+/\1-\2/p') REQUIRED_TOOLS=("flameshot" "curl" "xclip" "notify-send")
# Take screenshot with Flameshot # Check if the proper tools are installed
flameshot gui -r -p "$FILE" > /dev/null # Append the random gibberish to /dev/null for tool in "${REQUIRED_TOOLS[@]}"; do
if ! check_tool "$tool"; then
# Upload file echo "Error: $tool is missing!"
if [ -f "$FILE" ]; then exit 1
echo "Uploading $FILE to $(get_mode)..." fi
done
# Configure upload fields
FIELD="$([[ $MODE -eq 0 ]] && echo "file" || echo "image")=@$FILE" # Build dynamic Flameshot user-agent
[[ "${DOMAIN%%:*}" = "127.0.0.1" ]] && PROTOCOL="http" || PROTOCOL="https" USERAGENT=$(flameshot -v | sed -n -E 's/(Flameshot) (v[0-9]+\.[0-9]+\.[0-9]+) .+/\1-\2/p')
POSTTO="$PROTOCOL://$DOMAIN/$([[ $MODE -eq 0 ]] && echo "" || echo "upload")"
# Take screenshot with Flameshot
# Upload the file flameshot gui -r -p "$FILE" > /dev/null # Append the random gibberish to /dev/null
URL=$(curl -sS -X POST \
-H "Content-Type: multipart/form-data" \ # Upload file
-H "Accept: application/json" \ if [ -f "$FILE" ]; then
-H "User-Agent: $USERAGENT" \ echo "Uploading $FILE to $(get_mode)..."
-H "Authorization: $TOKEN" \
-F $FIELD $POSTTO
)
# Response parser unique to ass
if [[ $MODE -eq 0 ]]; then
URL=$(echo $URL | grep -Po '(?<="resource":")[^"]+')
fi
# Copy the URL to clipboard (using printf instead of echo to avoid a newline) # Configure upload fields
printf "%s" "$URL" | xclip -sel clip FIELD="$([[ $MODE -eq 0 ]] && echo "file" || echo "image")=@$FILE"
echo "URL copied: $URL" [[ "${DOMAIN%%:*}" = "127.0.0.1" ]] && PROTOCOL="http" || PROTOCOL="https"
notify-send -a $(get_mode) -t 2000 "URL copied to clipboard" "<a href=\"$URL\">View in browser</a>" POSTTO="$PROTOCOL://$DOMAIN/$([[ $MODE -eq 0 ]] && echo "" || echo "upload")"
# Upload the file
URL=$(curl -sS -X POST \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/json" \
-H "User-Agent: $USERAGENT" \
-H "Authorization: $TOKEN" \
-F $FIELD $POSTTO
)
# Response parser unique to ass
if [[ $MODE -eq 0 ]]; then
URL=$(echo $URL | grep -Po '(?<="resource":")[^"]+')
fi
# Copy the URL to clipboard (using printf instead of echo to avoid a newline)
printf "%s" "$URL" | xclip -sel clip
echo "URL copied: $URL"
notify-send -a $(get_mode) -t 4000 "URL copied to clipboard" "<a href=\"$URL\">View in browser</a>"
# Delete local file
rm "$FILE"
else
echo "Aborted."
fi
}
# Delete local file takeScreenshot
rm "$FILE"
else
echo "Aborted."
fi

Loading…
Cancel
Save