#!/usr/bin/env bash
FILE_PATH="${1:-}"
# If no file path provided, use fzf to select one
if [[ -z "$FILE_PATH" ]]; then
FILE_PATH=$(
find . -maxdepth 1 -type f -printf '%f\n' |
fzf \
--prompt=" Select file to upload: " \
--border \
--reverse \
--header="Use / to select, <Enter> to confirm" \
--pointer="➤" \
--color="bg+:#1e1e2e,bg:#181825,spinner:#f5c2e7,hl:#f38ba8,fg:#cdd6f4,header:#89b4fa,info:#f9e2af,pointer:#a6e3a1,marker:#f5c2e7,fg+:#cdd6f4,prompt:#89b4fa,hl+:#f38a8a" \
--preview='bat --style=numbers --color=always {}'
)
if [[ -z "$FILE_PATH" ]]; then
echo -e "\033[33mNo file selected.\033[0m"
exit 1
fi
fi
# Validate file
if [[ ! -f "$FILE_PATH" ]]; then
echo -e "\033[31mFile path does not exist: $FILE_PATH\033[0m"
exit 1
fi
# Check required commands
for cmd in curl jq; do
if ! command -v "$cmd" &>/dev/null; then
echo -e "\033[31mRequired command not found: $cmd\033[0m"
exit 1
fi
done
# Upload
RESPONSE=$(curl -fsS \
-X POST \
-H "Content-Type: text/plain" \
--data-binary @"$FILE_PATH" \
"https://pste.us/documents") || {
echo -e "\033[31mFailed to upload the document.\033[0m"
exit 1
}
# Extract Hastebin key
HASTE_KEY=$(printf '%s' "$RESPONSE" | jq -r '.key')
if [[ -z "$HASTE_KEY" || "$HASTE_KEY" == "null" ]]; then
echo -e "\033[31mFailed to get Hastebin key.\033[0m"
exit 1
fi
URL="https://pste.us/$HASTE_KEY"
# Copy URL to clipboard
COPIED=false
if command -v wl-copy &>/dev/null; then
printf '%s' "$URL" | wl-copy
COPIED=true
elif command -v xclip &>/dev/null; then
printf '%s' "$URL" | xclip -selection clipboard
COPIED=true
elif command -v xsel &>/dev/null; then
printf '%s' "$URL" | xsel --clipboard --input
COPIED=true
elif command -v pbcopy &>/dev/null; then
printf '%s' "$URL" | pbcopy
COPIED=true
fi
# Output
echo
echo -e "\033[35m$(basename "$FILE_PATH")\033[0m"
echo -e "\033[36m Hastebin URL:\033[0m \033[32m$URL\033[0m"
if [[ "$COPIED" == true ]]; then
echo -e "\033[33m (Copied to clipboard)\033[0m"
else
echo -e "\033[33m Clipboard utility not found; URL was not copied.\033[0m"
fi
# Print QR code
echo
if command -v qrencode &>/dev/null; then
echo -e "\033[36m QR Code:\033[0m"
echo
qrencode -t ANSIUTF8 "$URL"
else
echo -e "\033[33m qrencode is not installed.\033[0m"
echo -e "\033[33m Install it with:\033[0m"
echo -e "\033[32m sudo pacman -S qrencode\033[0m"
fi
echo