diff --git a/.env.example b/.env.example deleted file mode 100644 index 8b3b1aa..0000000 --- a/.env.example +++ /dev/null @@ -1,5 +0,0 @@ -UPLOAD_AUTH="" -UPLOAD_URL="https://atums.world/api/upload" - -UPLOAD_HEADERS="x-zipline-format: date;x-zipline-original-name: true;x-zipline-domain: atums.world,your-mommy.org;" -# https://github.com/diced/zipline/blob/e1256db6612ea3fb5f83091ca415a3b58a1edd55/src/lib/uploader/parseHeaders.ts#L42 \ No newline at end of file diff --git a/README.md b/README.md index dab300c..981338c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# grabit +# grabIT A simple script to take a screenshot, upload it to atums.world, and copy the link to the clipboard. @@ -17,6 +17,7 @@ A simple script to take a screenshot, upload it to atums.world, and copy the lin ### Other - `xdg-utils` +- tk - Python 3 ## Setup diff --git a/helpers/config.sh b/helpers/config.sh new file mode 100644 index 0000000..65e5fe7 --- /dev/null +++ b/helpers/config.sh @@ -0,0 +1,186 @@ +#!/bin/bash + +[[ ! -d "$CONFIG_DIR" ]] && mkdir -p "$CONFIG_DIR" +if [[ ! -f "$CONFIG_FILE" || ! -s "$CONFIG_FILE" ]]; then + echo "{}" > "$CONFIG_FILE" + NEEDS_SETUP=1 +fi + +validate_config() { + local is_valid=true + local temp_file=$(mktemp) + + local keys + keys=$(jq -r 'keys[]' "$CONFIG_FILE") + + for key in $keys; do + if [[ -z "${ALLOWED_KEYS[$key]}" ]]; then + jq "del(.\"$key\")" "$CONFIG_FILE" > "$temp_file" && mv "$temp_file" "$CONFIG_FILE" + else + local value + value=$(jq -r --arg key "$key" '.[$key]' "$CONFIG_FILE") + if ! [[ "$value" =~ ${ALLOWED_KEYS[$key]} ]]; then + jq "del(.\"$key\")" "$CONFIG_FILE" > "$temp_file" && mv "$temp_file" "$CONFIG_FILE" + fi + fi + done + + for req_key in "${REQUIRED_KEYS[@]}"; do + if ! jq -e --arg key "$req_key" 'has($key)' "$CONFIG_FILE" >/dev/null; then + NEEDS_SETUP=1 + fi + done +} + +get_value() { + local key="$1" + jq -r ".${key} // empty" "$CONFIG_FILE" +} + +set_value() { + local key="$1" + local value="$2" + + if [[ -z "$key" || -z "$value" ]]; then + echo "Usage: $0 set " + exit 1 + fi + if [[ -z "${ALLOWED_KEYS[$key]}" ]]; then + echo "Invalid key: $key" + exit 1 + fi + if ! [[ "$value" =~ ${ALLOWED_KEYS[$key]} ]]; then + echo "Invalid value for $key: $value" + exit 1 + fi + + jq ". + {\"$key\": \"$value\"}" "$CONFIG_FILE" > temp.json && mv temp.json "$CONFIG_FILE" +} + +remove_value() { + local key="$1" + jq "del(.${key})" "$CONFIG_FILE" > temp.json && mv temp.json "$CONFIG_FILE" + echo "Removed $key." +} + +list_values() { + cat "$CONFIG_FILE" | jq . +} + +setup_domain() { + local domain + local service + local auth + + if [[ -z "$SERVICE" ]]; then + while true; do + echo -ne "${CYAN}Choose a service (${valid_services[*]}): ${RESET}" + read service + + if [[ " ${valid_services[*]} " =~ " $service " ]]; then + break + else + echo -e "${RED}Invalid service. Please choose from: ${valid_services[*]}${RESET}" + fi + done + + set_value "SERVICE" "$service" + else + service="$SERVICE" + fi + + if [[ "$service" == "zipline" ]]; then + while true; do + echo -ne "${CYAN}Enter the domain (e.g. example.com): ${RESET}" + read domain + + if [[ "$domain" =~ ^[a-zA-Z0-9.-]+$ ]]; then + break + else + echo -e "${RED}Invalid domain format. Please enter a valid domain (e.g. example.com).${RESET}" + fi + done + + domain="https://$domain" + domain="${domain%/}" + [[ "$domain" != */api/upload ]] && domain="$domain/api/upload" + + set_value "DOMAIN" "$domain" + echo -e "${GREEN}Domain set to: $domain${RESET}" + fi + + while [[ -z "$auth" ]]; do + echo -ne "${CYAN}Enter your auth token: ${RESET}" + read auth + [[ -z "$auth" ]] && echo -e "${RED}Auth token cannot be empty. Please enter a valid token.${RESET}" + done + + set_value "${service}_auth" "$auth" + echo -e "${GREEN}Auth token set successfully.${RESET}" +} + +setup_config() { + local save_dir + local show_capture_preview + local show_notifications + local save_images + local default_option + + echo -ne "${CYAN}What do you want the default option to be? (upload/save/copy): ${RESET}" + read default_option + while ! [[ "$default_option" =~ ^(upload|save|copy)$ ]]; do + echo -e "${RED}Invalid option. Please enter 'upload', 'save', or 'copy'.${RESET}" + echo -ne "${CYAN}Choose (upload/save/copy): ${RESET}" + read default_option + done + set_value "DEFAULT_OPTION" "$default_option" + + [[ "$default_option" == "upload" ]] && setup_domain + + echo -ne "${CYAN}Do you want to save images? (y/n): ${RESET}" + read save_images + while ! [[ "$save_images" =~ ^(y|Y|n|N|true|false)$ ]]; do + echo -e "${RED}Invalid option. Please enter 'y' or 'n'.${RESET}" + echo -ne "${CYAN}Do you want to save images? (y/n): ${RESET}" + read save_images + done + [[ "$save_images" =~ ^(y|Y)$ ]] && save_images="true" || save_images="false" + set_value "SAVE_IMAGES" "$save_images" + + if [[ "$save_images" == "true" ]]; then + echo -ne "${CYAN}Enter the directory to save images (default: $HOME/Pictures): ${RESET}" + read save_dir + save_dir="${save_dir:-$HOME/Pictures}" + while [[ ! -d "$save_dir" ]]; do + echo -e "${RED}Directory does not exist. Please enter a valid directory.${RESET}" + echo -ne "${CYAN}Enter the directory to save images: ${RESET}" + read save_dir + done + set_value "SAVE_DIR" "$save_dir" + fi + + echo -ne "${CYAN}Do you want to show capture notifications? (y/n): ${RESET}" + read show_notifications + while ! [[ "$show_notifications" =~ ^(y|Y|n|N|true|false)$ ]]; do + echo -e "${RED}Invalid option. Please enter 'y' or 'n'.${RESET}" + echo -ne "${CYAN}Do you want to show capture notifications? (y/n): ${RESET}" + read show_notifications + done + [[ "$show_notifications" =~ ^(y|Y)$ ]] && show_notifications="true" || show_notifications="false" + set_value "SHOW_NOTIFICATIONS" "$show_notifications" + + echo -ne "${CYAN}Do you want to show capture preview? (y/n): ${RESET}" + read show_capture_preview + while ! [[ "$show_capture_preview" =~ ^(y|Y|n|N|true|false)$ ]]; do + echo -e "${RED}Invalid option. Please enter 'y' or 'n'.${RESET}" + echo -ne "${CYAN}Do you want to show capture preview? (y/n): ${RESET}" + read show_capture_preview + done + [[ "$show_capture_preview" =~ ^(y|Y)$ ]] && show_capture_preview="true" || show_capture_preview="false" + set_value "SHOW_CAPTURE_PREVIEW" "$show_capture_preview" + + echo -e "${GREEN}${BOLD}Configuration completed successfully!${RESET}" + echo -e "${YELLOW}You can change the configuration later by running the script with the --config option.${RESET}" + + [[ "$show_capture_preview" == "true" ]] && setup_venv +} \ No newline at end of file diff --git a/helpers/installer.sh b/helpers/installer.sh new file mode 100644 index 0000000..2d31775 --- /dev/null +++ b/helpers/installer.sh @@ -0,0 +1,17 @@ +VENV_DIR="$SCRIPT_DIR/.venv" + +setup_venv() { + if [[ ! -d "$VENV_DIR" ]]; then + python3 -m venv "$VENV_DIR" >/dev/null 2>&1 + "$VENV_DIR/bin/pip" install --upgrade pip >/dev/null 2>&1 + "$VENV_DIR/bin/pip" install pillow screeninfo >/dev/null 2>&1 + + PYTHON_EXEC="$VENV_DIR/bin/python3" + if [[ ! -f "$PYTHON_EXEC" ]]; then + echo -e "${RED}Python executable not found in virtual environment.${RESET}" + exit 1 + fi + else + PYTHON_EXEC="$VENV_DIR/bin/python3" + fi +} \ No newline at end of file diff --git a/show_image.py b/helpers/show_image.py similarity index 100% rename from show_image.py rename to helpers/show_image.py diff --git a/helpers/variables.sh b/helpers/variables.sh new file mode 100644 index 0000000..5d51d33 --- /dev/null +++ b/helpers/variables.sh @@ -0,0 +1,62 @@ +GREEN="\e[32m" +YELLOW="\e[33m" +RED="\e[31m" +CYAN="\e[36m" +BOLD="\e[1m" +RESET="\e[0m" + +CONFIG_DIR="$HOME/.config/grabIT" +CONFIG_FILE="$CONFIG_DIR/config.json" +NEEDS_SETUP=0 + +declare -A ALLOWED_KEYS=( + ["log_level"]="^(DEBUG|INFO|WARN|ERROR)$" + ["SHOW_CAPTURE_PREVIEW"]="^(true|false)$" + ["SHOW_NOTIFICATIONS"]="^(true|false)$" + ["SAVE_IMAGES"]="^(true|false)$" + ["SAVE_DIR"]="^.+$" + ["DOMAIN"]="^.+$" + ["DEFAULT_OPTION"]="^(upload|save|copy)$" + ["SERVICE"]="^(zipline|nest|fakecrime|ez|guns|pixelvault)$" +) + +REQUIRED_KEYS=( + "SHOW_CAPTURE_PREVIEW" + "SHOW_NOTIFICATIONS" + "SAVE_IMAGES" + "DEFAULT_OPTION" +) + +valid_services=("zipline" "nest" "fakecrime" "ez" "guns" "pixelvault") +valid_args=("--help" "--version" "--config" "-c" "-u" "-f" "-d" "--silent") + +for service in "${valid_services[@]}"; do + valid_args+=("--$service") + ALLOWED_KEYS["${service}_auth"]="^.+$" +done + +declare -A UPLOADERS=( + ["pixelvault"]="https://pixelvault.co" + ["guns"]="https://guns.lol/api/upload" + ["ez"]="https://api.e-z.host/files" + ["fakecrime"]="https://upload.fakecrime.bio" + ["nest"]="https://nest.rip/api/files/upload" +) + +declare -A UPLOAD_HEADERS=( + ["pixelvault"]="https://pixelvault.co|Authorization" + ["guns"]="key" + ["ez"]="key" + ["fakecrime"]="Authorization" + ["nest"]="Authorization" + ["zipline"]="authorization" +) + +declare -A UPLOAD_JSON_KEYS=( + ["pixelvault"]="resource" + ["nest"]="fileURL" + ["guns"]="link" + ["ez"]="imageUrl" + ["fakecrime"]="url" + ["zipline"]="files[0].url" +) \ No newline at end of file diff --git a/main.sh b/main.sh new file mode 100755 index 0000000..00a3ad0 --- /dev/null +++ b/main.sh @@ -0,0 +1,344 @@ +#!/bin/bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +source "$SCRIPT_DIR/helpers/variables.sh" +source "$SCRIPT_DIR/helpers/installer.sh" +source "$SCRIPT_DIR/helpers/config.sh" + +validate_config + +if [[ "$NEEDS_SETUP" -eq 1 ]]; then setup_config; fi +if [[ $(get_value "SHOW_CAPTURE_PREVIEW") == "true" ]]; then setup_venv; fi + +arguments=() +while [[ $# -gt 0 ]]; do + arg="$1" + case "$arg" in + --help) + echo "Usage: $0 [options]" + echo "--help Show this help message" + echo "--version Show version information" + echo "--config Allows you to set up the config file" + echo "-c Copies the screenshot to clipboard" + echo "-u Uploads the screenshot" + echo "-f Uploads the given file instead of taking a screenshot" + echo "-d Debug mode" + echo "--silent Suppresses Image preview and notifications" + echo + echo "Domain Specific Options:" + for service in "${valid_services[@]}"; do + echo "--$service" + done + exit 0 + ;; + --version) + echo "Version: $VERSION" + exit 0 + ;; + --config) + setup_config + exit 0 + ;; + -c) + arguments+=("-c") + shift + ;; + -u) + arguments+=("-u") + shift + ;; + -f) + shift + if [[ -n "$1" && "$1" != -* ]]; then + arguments+=("-f" "$1") + shift + else + echo -e "${RED}Error: No valid file specified for -f option.${RESET}" + exit 1 + fi + ;; + -d) + DEBUG=true + shift + ;; + --silent) + SHOW_NOTIFICATIONS=false + SHOW_CAPTURE_PREVIEW=false + shift + ;; + --*) + service="${arg:2}" + if [[ " ${valid_services[*]} " =~ " ${service} " ]]; then + SHOULD_UPLOAD=true + SHOULD_COPY=false + SERVICE="${service}" + shift + else + echo -e "${RED}Error: Invalid service: $service${RESET}" + exit 1 + fi + ;; + *) + echo -e "${RED}Error: Invalid argument: $arg${RESET}" + echo "Use --help for usage information." + exit 1 + ;; + esac +done + +REQUIRED_COMMANDS=("notify-send" "curl" "jq") + +SHOULD_UPLOAD=$(get_value "DEFAULT_OPTION") +[[ "$SHOULD_UPLOAD" == "upload" ]] && SHOULD_UPLOAD=true || SHOULD_UPLOAD=false + +SHOW_NOTIFICATIONS=$(get_value "SHOW_NOTIFICATIONS") +[[ "$SHOW_NOTIFICATIONS" == "true" ]] && SHOW_NOTIFICATIONS=true || SHOW_NOTIFICATIONS=false + +SHOW_CAPTURE_PREVIEW=$(get_value "SHOW_CAPTURE_PREVIEW") +[[ "$SHOW_CAPTURE_PREVIEW" == "true" ]] && SHOW_CAPTURE_PREVIEW=true || SHOW_CAPTURE_PREVIEW=false + +if [[ -n "$WAYLAND_DISPLAY" ]]; then + SCREENSHOT_CMD=("grim" "-g") + SELECT_REGION="slurp" + REQUIRED_COMMANDS+=("wl-copy" "grim" "slurp") +elif [[ -n "$DISPLAY" ]]; then + SCREENSHOT_CMD=("flameshot" "gui" "-p") + REQUIRED_COMMANDS+=("xclip" "flameshot") +else + echo "Error: Unable to detect display server (Wayland/X11)." + exit 1 +fi + +MISSING_COMMANDS=() +for cmd in "${REQUIRED_COMMANDS[@]}"; do + command -v "$cmd" &>/dev/null || MISSING_COMMANDS+=("$cmd") +done + +# Check for xdg-utils for xdg-open +if ! command -v xdg-open &>/dev/null; then + MISSING_COMMANDS+=("xdg-utils") +fi + +if [[ ${#MISSING_COMMANDS[@]} -gt 0 ]]; then + echo -e "${RED}Error: The following commands are not installed: ${MISSING_COMMANDS[*]}${RESET}" + echo -e "${RED}Please install them" + exit 1 +fi + +send_notification() { + [[ "$SHOW_NOTIFICATIONS" == "false" ]] && return + + local title="$1" + local message="$2" + local image="$3" + + if [[ -z "$title" && -z "$message" ]]; then + return + fi + + local notify_cmd=("notify-send") + + [[ -n "$image" ]] && notify_cmd+=("-i" "$image") + [[ -n "$title" ]] && notify_cmd+=("$title") + [[ -n "$message" ]] && notify_cmd+=("$message") + + "${notify_cmd[@]}" +} + +SHOULD_UPLOAD=false +SHOULD_SAVE="$(get_value "SAVE_IMAGES")" +SHOULD_COPY=false + +case "$(get_value "DEFAULT_OPTION")" in + upload) SHOULD_UPLOAD=true ;; + copy) SHOULD_COPY=true ;; +esac + +for arg in "${arguments[@]}"; do + case "$arg" in + -c) + SHOULD_UPLOAD=false + SHOULD_COPY=true + ;; + -u|-f) + SHOULD_UPLOAD=true + SHOULD_COPY=false + ;; + esac +done + +CURRENT_DATE=$(date +%Y-%m-%d-%H-%M-%S) +RANDOM_FILE_NAME="${CURRENT_DATE}_$(date +%s%N | cut -b1-13)" + +if [[ "$SHOULD_COPY" == "true" ]]; then + if [[ "$SHOULD_SAVE" == "true" ]]; then + SAVE_DIR=$(get_value "SAVE_DIR") + if [[ ! -d "$SAVE_DIR" ]]; then + mkdir -p "$SAVE_DIR" + fi + FILE="$SAVE_DIR/$CURRENT_DATE.png" + else + FILE="/tmp/$RANDOM_FILE_NAME.png" + fi + + if [[ -n "$WAYLAND_DISPLAY" ]]; then + "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$FILE" + else + "${SCREENSHOT_CMD[@]}" "$FILE" + fi + + if [[ $? -ne 0 ]]; then + echo "Error: Screenshot capture failed." >&2 + exit 1 + fi + + if [[ ! -f "$FILE" ]]; then + echo "Error: File not found: $FILE" >&2 + exit 1 + fi + + if [[ -n "$WAYLAND_DISPLAY" ]]; then + echo "Using wl-copy for clipboard." + cat "$FILE" | wl-copy + elif [[ -n "$DISPLAY" ]]; then + echo "Using xclip for clipboard." + xclip -selection clipboard -t image/png -i "$FILE" + else + echo "Error: Unable to detect display server (Wayland/X11)." + exit 1 + fi + + if [[ "$SHOW_CAPTURE_PREVIEW" == "true" ]]; then + "$PYTHON_EXEC" "$SCRIPT_DIR/helpers/show_image.py" "$FILE" "Copied to your clipboard" + fi + + if [[ "$SHOULD_SAVE" != "true" ]]; then + [[ -n "$FILE" ]] && rm -f "$FILE" + fi + + exit 0 +fi + +if [[ -z "$SERVICE" ]]; then + SERVICE=$(get_value "SERVICE") + if [[ -z "$SERVICE" ]]; then + echo -e "${RED}Error: No service specified and no default service set.${RESET}" + exit 1 + fi +fi + +if [[ "$SERVICE" == "zipline" ]]; then + DOMAIN=$(get_value "DOMAIN") + if [[ -z "$DOMAIN" ]]; then + echo -e "${RED}Error: Domain not set for zipline.${RESET}" + setup_domain + fi +else + DOMAIN="${UPLOADERS[$SERVICE]}" +fi + +AUTH=$(get_value "${SERVICE}_auth") +if [[ -z "$AUTH" ]]; then + echo -e "${RED}Error: Auth not set for $SERVICE.${RESET}" + setup_domain + + AUTH=$(get_value "${SERVICE}_auth") + if [[ -z "$AUTH" ]]; then + echo -e "${RED}Error: No auth key provided for $SERVICE.${RESET}" + exit 1 + fi +fi + +USER_FILE="" +if [[ "$SHOULD_UPLOAD" == "true" ]]; then + for ((i = 0; i < ${#arguments[@]}; i++)); do + if [[ "${arguments[i]}" == "-f" ]]; then + USER_FILE="${arguments[i+1]}" + break + fi + done +fi + +if [[ -z "$USER_FILE" ]]; then + if [[ "$SHOULD_SAVE" == "true" ]]; then + SAVE_DIR=$(get_value "SAVE_DIR") + if [[ ! -d "$SAVE_DIR" ]]; then + mkdir -p "$SAVE_DIR" + fi + FILE="$SAVE_DIR/$CURRENT_DATE.png" + else + FILE="/tmp/$RANDOM_FILE_NAME.png" + fi + + if [[ -n "$WAYLAND_DISPLAY" ]]; then + "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$FILE" + else + "${SCREENSHOT_CMD[@]}" "$FILE" + fi + + if [[ $? -ne 0 ]]; then + echo "Error: Screenshot capture failed." >&2 + exit 1 + fi + + if [[ ! -f "$FILE" ]]; then + echo "Error: File not found: $FILE" >&2 + exit 1 + fi + + if [[ "$SHOULD_SAVE" != "true" ]]; then + [[ -n "$FILE" ]] && rm -f "$FILE" + fi +else + FILE="$USER_FILE" +fi + +if [[ ! -f "$FILE" ]]; then + echo "Error: File not found: $FILE" >&2 + exit 1 +fi + +FILE_URL="" + +REPONSE=$(curl -s -X POST \ + -F "file=@$FILE" \ + -H "${UPLOAD_HEADERS[$SERVICE]}: $AUTH" \ + "$DOMAIN") + +if ! echo "$REPONSE" | jq . &>/dev/null; then + echo -e "${RED}Error: Invalid response from server.${RESET}" + exit 1 +fi +FILE_URL=$(echo "$REPONSE" | jq -r ".${UPLOAD_JSON_KEYS[$SERVICE]}") + +if [[ "$FILE_URL" == "null" ]]; then + echo -e "${RED}Error: File URL not found in response.${RESET}" + exit 1 +fi + +MIME_TYPE=$(file --mime-type -b "$FILE") +if [[ "$MIME_TYPE" == image/* ]]; then + if [[ "$SHOW_NOTIFICATIONS" == "true" ]]; then + send_notification "Uploaded to $SERVICE" "$FILE_URL" "$FILE" + fi + + if [[ "$SHOW_CAPTURE_PREVIEW" == "true" ]]; then + "$PYTHON_EXEC" "$SCRIPT_DIR/helpers/show_image.py" "$FILE" "Uploaded to $SERVICE" + fi +else + if [[ "$SHOW_NOTIFICATIONS" == "true" ]]; then + send_notification "Uploaded to $SERVICE" "$FILE_URL" + fi +fi + +if [[ -n "$WAYLAND_DISPLAY" ]]; then + echo "Using wl-copy for clipboard." + echo "$FILE_URL" | wl-copy +elif [[ -n "$DISPLAY" ]]; then + echo "Using xclip for clipboard." + echo "$FILE_URL" | xclip -selection clipboard +else + echo "Error: Unable to detect display server (Wayland/X11)." + exit 1 +fi \ No newline at end of file diff --git a/upload.sh b/upload.sh deleted file mode 100755 index e94a688..0000000 --- a/upload.sh +++ /dev/null @@ -1,154 +0,0 @@ -#!/bin/bash - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -VENV_DIR="$SCRIPT_DIR/.venv" -ENV_FILE="$SCRIPT_DIR/.env" - -if [[ ! -d "$VENV_DIR" ]]; then - echo "Creating Python virtual environment..." - python3 -m venv "$VENV_DIR" - "$VENV_DIR/bin/pip" install --upgrade pip - "$VENV_DIR/bin/pip" install pillow screeninfo -fi - -PYTHON_EXEC="$VENV_DIR/bin/python3" - -if [[ -f "$ENV_FILE" ]]; then - set -o allexport - source "$ENV_FILE" - set +o allexport -else - echo "Error: .env file not found in script directory ($SCRIPT_DIR)." - exit 1 -fi - -if [[ -n "$WAYLAND_DISPLAY" ]]; then - SCREENSHOT_CMD=("grim" "-g") - SELECT_REGION="slurp" - REQUIRED_COMMANDS=("curl" "jq" "wl-copy" "grim" "slurp") -elif [[ -n "$DISPLAY" ]]; then - SCREENSHOT_CMD=("flameshot" "gui" "-p") - REQUIRED_COMMANDS=("curl" "jq" "xclip" "flameshot") -else - echo "Error: Unable to detect display server (Wayland/X11)." - exit 1 -fi - -MISSING_COMMANDS=() -for cmd in "${REQUIRED_COMMANDS[@]}"; do - command -v "$cmd" &>/dev/null || MISSING_COMMANDS+=("$cmd") -done - -# Check for xdg-utils for xdg-open -if ! command -v xdg-open &>/dev/null; then - MISSING_COMMANDS+=("xdg-utils") -fi - -if [[ ${#MISSING_COMMANDS[@]} -gt 0 ]]; then - echo "Error: The following commands are not installed: ${MISSING_COMMANDS[*]}" - exit 1 -fi - -if [[ -z "$UPLOAD_AUTH" || -z "$UPLOAD_URL" ]]; then - echo "Error: Required environment variables UPLOAD_AUTH or UPLOAD_URL are not set in .env." - exit 1 -fi - -HEADERS=( - -H "authorization: $UPLOAD_AUTH" - -H "content-type: multipart/form-data" -) - -if [[ -n "$UPLOAD_HEADERS" ]]; then - IFS=';' read -ra ADDITIONAL_HEADERS <<< "$UPLOAD_HEADERS" - for HEADER in "${ADDITIONAL_HEADERS[@]}"; do - HEADERS+=(-H "$HEADER") - done -fi - -if [[ "$1" == "-h" || "$1" == "-help" ]]; then - echo "Usage: $0 [-c | -clipboard] [FILE]" - echo "Upload an image to atums.world and copy the URL to the clipboard" - echo - echo "Options:" - echo " -c, -clipboard Upload the image from the clipboard" - echo - exit 0 -fi - -TEMP_FILE="" - -if [[ "$1" == "-c" || "$1" == "-clipboard" ]]; then - FILE="$2" - - if [[ -z "$FILE" ]]; then - TEMP_FILE="/tmp/screenshot.png" - if [[ -n "$WAYLAND_DISPLAY" ]]; then - "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$TEMP_FILE" - else - "${SCREENSHOT_CMD[@]}" "$TEMP_FILE" - fi - if [[ $? -ne 0 ]]; then - echo "Error: Screenshot capture failed." >&2 - exit 1 - fi - FILE="$TEMP_FILE" - fi - - if [[ ! -f "$FILE" ]]; then - echo "Error: File not found: $FILE" >&2 - exit 1 - fi - - if [[ -n "$WAYLAND_DISPLAY" ]]; then - echo "Using wl-copy for clipboard." - cat "$FILE" | wl-copy - elif [[ -n "$DISPLAY" ]]; then - echo "Using xclip for clipboard." - xclip -selection clipboard -t image/png -i "$FILE" - else - echo "Error: Unable to detect display server (Wayland/X11)." - exit 1 - fi - "$PYTHON_EXEC" "$SCRIPT_DIR/show_image.py" "$FILE" "Copied to your clipboard" - - [[ -n "$TEMP_FILE" ]] && rm -f "$TEMP_FILE" - exit 0 -fi - -if [[ -z "$1" ]]; then - TEMP_FILE="/tmp/screenshot.png" - if [[ -n "$WAYLAND_DISPLAY" ]]; then - "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$TEMP_FILE" - else - "${SCREENSHOT_CMD[@]}" "$TEMP_FILE" - fi - if [[ $? -ne 0 ]]; then - echo "Error: Screenshot capture failed." >&2 - exit 1 - fi - FILE="$TEMP_FILE" -else - FILE="$1" -fi - -if [[ ! -f "$FILE" ]]; then - echo "Error: File not found: $FILE" >&2 - exit 1 -fi - -FILE_URL=$(curl -s "${HEADERS[@]}" -F file=@"$FILE" "$UPLOAD_URL" | jq -r .files[0].url | tr -d '\n') - -if [[ -n "$WAYLAND_DISPLAY" ]]; then - echo "Using wl-copy for clipboard." - echo -n "$FILE_URL" | wl-copy -elif [[ -n "$DISPLAY" ]]; then - echo "Using xclip for clipboard." - echo -n "$FILE_URL" | xclip -selection clipboard -else - echo "Error: Unable to detect display server (Wayland/X11)." - exit 1 -fi -"$PYTHON_EXEC" "$SCRIPT_DIR/show_image.py" "$FILE" "$FILE_URL" "$FILE_URL" - -[[ -n "$TEMP_FILE" ]] && rm -f "$TEMP_FILE"