Skip to content

Commit

Permalink
feat: migrate check_tool
Browse files Browse the repository at this point in the history
Co-authored-by: NotAShelf <[email protected]>
  • Loading branch information
tycrek and NotAShelf committed Dec 4, 2023
1 parent 9205f18 commit f4f5997
Showing 1 changed file with 58 additions and 39 deletions.
97 changes: 58 additions & 39 deletions flameshot-v2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
# ! Upload mode (ass=0,cheek=1)
MODE=0

# Function to check if a tool is installed
check_tool() {
command -v "$1" >/dev/null 2>&1
}

# Mode string switcher
get_mode() {
if [[ $MODE -eq 0 ]];
then echo "ass"
else echo "cheek"
then echo "ass"
else echo "cheek"
fi
}

Expand All @@ -29,42 +34,56 @@ FILE="$IMGPATH/$(get_mode)-$(date +%s).png"
TOKEN=$(cat $IMGPATH/.token)
DOMAIN=$(cat $IMGPATH/.domain)

# Build dynamic Flameshot user-agent
USERAGENT=$(flameshot -v | sed -n -E 's/(Flameshot) (v[0-9]+\.[0-9]+\.[0-9]+) .+/\1-\2/p')

# Take screenshot with Flameshot
flameshot gui -r -p "$FILE" > /dev/null # Append the random gibberish to /dev/null

# Upload file
if [ -f "$FILE" ]; then
echo "Uploading $FILE to $(get_mode)..."

# Configure upload fields
FIELD="$([[ $MODE -eq 0 ]] && echo "file" || echo "image")=@$FILE"
[[ "${DOMAIN%%:*}" = "127.0.0.1" ]] && PROTOCOL="http" || PROTOCOL="https"
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
takeScreenshot() {
REQUIRED_TOOLS=("flameshot" "curl" "xclip" "notify-send")

# Check if the proper tools are installed
for tool in "${REQUIRED_TOOLS[@]}"; do
if ! check_tool "$tool"; then
echo "Error: $tool is missing!"
exit 1
fi
done

# Build dynamic Flameshot user-agent
USERAGENT=$(flameshot -v | sed -n -E 's/(Flameshot) (v[0-9]+\.[0-9]+\.[0-9]+) .+/\1-\2/p')

# Take screenshot with Flameshot
flameshot gui -r -p "$FILE" > /dev/null # Append the random gibberish to /dev/null

# Upload file
if [ -f "$FILE" ]; then
echo "Uploading $FILE to $(get_mode)..."

# 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 2000 "URL copied to clipboard" "<a href=\"$URL\">View in browser</a>"
# Configure upload fields
FIELD="$([[ $MODE -eq 0 ]] && echo "file" || echo "image")=@$FILE"
[[ "${DOMAIN%%:*}" = "127.0.0.1" ]] && PROTOCOL="http" || PROTOCOL="https"
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
rm "$FILE"
else
echo "Aborted."
fi
takeScreenshot

0 comments on commit f4f5997

Please sign in to comment.