[Add] Added options for some different screenshot tools.

[Change] A bit of housekeeping because I made a mess.
This commit is contained in:
Ritchie Cunningham 2025-04-25 22:52:32 +01:00
parent a89ce0b2c4
commit b4ff80b9d0

51
imgbb
View File

@ -14,6 +14,7 @@
# === Default Configuration & Argument Parsing. === # === Default Configuration & Argument Parsing. ===
config_file="$HOME/.config/imgbb_uploader.conf" config_file="$HOME/.config/imgbb_uploader.conf"
imgbb_api_url="https://api.imgbb.com/1/upload"
# imgbb first reads defaults from ~/.config/imgbb_uploader.conf. # imgbb first reads defaults from ~/.config/imgbb_uploader.conf.
# If defaults not found in the config file, it reads them from the values # If defaults not found in the config file, it reads them from the values
# assigned below. # assigned below.
@ -270,6 +271,9 @@ temp_file_to_clean="" # Track temp file for cleanup if created.
# === Determine Input and Upload === # === Determine Input and Upload ===
# --- Define base curl options ---
curl_opts_base=(-s -S -f -L -X POST --form "key=$api_key")
# --- Set default trap (will be cleared or modified below) --- # --- Set default trap (will be cleared or modified below) ---
trap 'echo "Cleaning up temp files..."; rm -f "$temp_file_to_clean"' EXIT trap 'echo "Cleaning up temp files..."; rm -f "$temp_file_to_clean"' EXIT
@ -279,18 +283,27 @@ if [ "$select_mode" = "true" ]; then
screenshot_tool="scrot" screenshot_tool="scrot"
notify_cmd "ImgBB Uploader" "Select area for screenshot using $screenshot_tool..." notify_cmd "ImgBB Uploader" "Select area for screenshot using $screenshot_tool..."
# Build base curl options (key, expiration, name). screenshot_cmd=""
curl_opts_base=(-s -S -f -L -X POST --form "key=$api_key") case "$screenshot_tool" in
maim) screenshot_cmd="maim -s -f png /dev/stdout";;
scrot) screenshot_cmd="scrot -s -o /dev/stdout";;
flameshot) screenshot_cmd="flameshot gui -r";;
*) die "Unsupported screenshot tool configured: '$screenshot_tool'";;
esac
# Build full options for this mode, starting with base.
curl_opts=("${curl_opts_base[@]}")
if [ -n "$expire_seconds" ]; then curl_opts_base+=(--form "expiration=$expire_seconds"); fi if [ -n "$expire_seconds" ]; then curl_opts_base+=(--form "expiration=$expire_seconds"); fi
if [ -n "$custom_name" ]; then curl_opts_base+=(--form "name=$custom_name"); fi if [ -n "$custom_name" ]; then curl_opts_base+=(--form "name=$custom_name"); fi
notify_cmd "ImgBB Uploader" "Uploading $image_source_description..." # Add mode-specific image source (stdin) and the API URL.
curl_opts+=(--form "image=@-;filename=screenshot.png")
curl_opts+=("$imgbb_api_url")
# Execute scrot -s -o and pipe to curl. notify_cmd "ImgBB Uploader" "Uploading $image_source_description..."
# Curl reads image from stdin (-) and we provide a default filename.
# Capture the response and potential curl errors. # Capture the response and potential curl errors.
temp_stderr=$(mktemp) temp_stderr=$(mktemp)
response=$(scrot -s -o /dev/stdout | curl "${curl_opts_base[@]}" --form "image=@-;filename=screenshot.png" 'https://api.imgbb.com/1/upload' 2>"$temp_stderr") response=$(eval "$screenshot_cmd" | curl "${curl_opts[@]}" 2>"$temp_stderr")
# Check the status of both commands in the pipeline. # Check the status of both commands in the pipeline.
pipeline_status=("${PIPESTATUS[@]}") pipeline_status=("${PIPESTATUS[@]}")
@ -300,8 +313,12 @@ if [ "$select_mode" = "true" ]; then
rm -rf "$temp_stderr" rm -rf "$temp_stderr"
# Check for errors in the pipeline. # Check for errors in the pipeline.
if [ "$screenshot_status" -ne 0 ]; then die "Screenshot command ($screenshot_tool) failed (status $screenshot_status)."; fi if [ "$screenshot_status" -ne 0 ]; then
if (( upload_status != 0 )); then die "curl command failed during upload (status $upload_status). Check network? Curl stderr: $curl_stderr"; fi die "Screenshot command ($screenshot_tool) failed (status $screenshot_status).";
fi
if (( upload_status != 0 )); then
die "curl command failed during upload (status $upload_status). Check network? Curl stderr: $curl_stderr";
fi
# Clear the trap, no temp file used in this mode. # Clear the trap, no temp file used in this mode.
trap '' EXIT trap '' EXIT
@ -312,16 +329,16 @@ elif [ -n "$filepath" ]; then
echo "Using image from file: $filepath." echo "Using image from file: $filepath."
trap '' EXIT # Clear trap if we are not using temp files. trap '' EXIT # Clear trap if we are not using temp files.
# Build curl options for file upload. # Build full options for this mode, starting with base.
curl_opts=(-s -S -f -L -X POST) curl_opts=("${curl_opts_base[@]}")
curl_opts+=(--form "key=$api_key")
curl_opts+=(--form "image=@$TMP_IMG") # Use @filepath curl_opts+=(--form "image=@$TMP_IMG") # Use @filepath
if [ -n "$expire_seconds" ]; then curl_opts+=(--form "expiration=$expire_seconds"); fi if [ -n "$expire_seconds" ]; then curl_opts+=(--form "expiration=$expire_seconds"); fi
if [ -n "$custom_name" ]; then curl_opts+=(--form "name=$custom_name"); fi if [ -n "$custom_name" ]; then curl_opts+=(--form "name=$custom_name"); fi
curl_opts+=("$imgbb_api_url")
# Upload the image using curl. # Upload the image using curl.
notify_cmd "ImgBB Uploader" "Uploading $image_source_description..." notify_cmd "ImgBB Uploader" "Uploading $image_source_description..."
response=$(curl "${curl_opts[@]}" 'https://api.imgbb.com/1/upload') response=$(curl "${curl_opts[@]}")
#Check curl exit status. #Check curl exit status.
if [ $? -ne 0 ]; then die "curl command failed during upload. Check network?"; fi if [ $? -ne 0 ]; then die "curl command failed during upload. Check network?"; fi
else else
@ -335,18 +352,18 @@ else
esac || exit 1 # Exit if paste function failed. esac || exit 1 # Exit if paste function failed.
temp_file_to_clean="$TMP_IMG" # Mark temp file for cleanup by trap. temp_file_to_clean="$TMP_IMG" # Mark temp file for cleanup by trap.
# Ensure trap is set correctly. # Ensure trap is set correctly.
trap 'echo "Cleaning up temp file ($temp_file_to_clean)..."; rm -f "$temp_file_to_clean"' EXIT trap 'rm -f "$temp_file_to_clean"' EXIT
# Build curl options for temp file upload. # Build full options for this mode, starting with base.
curl_opts=(-s -S -f -L -X POST) curl_opts=("${curl_opts_base[@]}")
curl_opts+=(--form "key=$api_key")
curl_opts+=(--form "image=@$TMP_IMG") # Upload from temp file path. curl_opts+=(--form "image=@$TMP_IMG") # Upload from temp file path.
if [ -n "$expire_seconds" ]; then curl_opts+=(--form "expiration=$expire_seconds"); fi if [ -n "$expire_seconds" ]; then curl_opts+=(--form "expiration=$expire_seconds"); fi
if [ -n "$custom_name" ]; then curl_opts+=(--form "name=$custom_name"); fi if [ -n "$custom_name" ]; then curl_opts+=(--form "name=$custom_name"); fi
curl_opts+=("$imgbb_api_url")
# Upload the image using curl. # Upload the image using curl.
notify_cmd "ImgBB Uploader" "Uploading $image_source_description..." notify_cmd "ImgBB Uploader" "Uploading $image_source_description..."
response=$(curl "${curl_opts[@]}" 'https://api.imgbb.com/1/upload') response=$(curl "${curl_opts[@]}")
# Check curl exit status. # Check curl exit status.
if [ $? -ne 0 ]; then die "curl command failed during upload. Check network?"; fi if [ $? -ne 0 ]; then die "curl command failed during upload. Check network?"; fi
fi fi