diff --git a/helpers/config.sh b/helpers/config.sh index 65e5fe7..6336217 100644 --- a/helpers/config.sh +++ b/helpers/config.sh @@ -37,6 +37,11 @@ get_value() { jq -r ".${key} // empty" "$CONFIG_FILE" } +get_starting_with() { + local prefix="$1" + jq -r "to_entries | map(select(.key | startswith(\"$prefix\"))) | from_entries" "$CONFIG_FILE" +} + set_value() { local key="$1" local value="$2" @@ -107,6 +112,76 @@ setup_domain() { set_value "DOMAIN" "$domain" echo -e "${GREEN}Domain set to: $domain${RESET}" + + # extra options for zipline + echo -ne "${CYAN}Do you want to change upload options? (y/n): ${RESET}" + read change_options + while ! [[ "$change_options" =~ ^(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 change upload options? (y/n): ${RESET}" + read change_options + done + + if [[ "$change_options" =~ ^(y|Y)$ ]]; then + echo -ne "${CYAN}Enter the number of max views (default: 0): ${RESET}" + read max_views + while [[ -n "$max_views" && ! "$max_views" =~ ^[0-9]+$ ]]; do + echo -e "${RED}Invalid number. Please enter a valid number.${RESET}" + echo -ne "${CYAN}Enter the number of views (default: 0): ${RESET}" + read max_views + done + if [[ -n "$max_views" && "$max_views" -ne 0 ]]; then + set_value "x-zipline-max-views" "$max_views" + echo -e "${GREEN}Max views set to: $max_views${RESET}" + else + echo -e "${YELLOW}Using default max views (0). Not saving.${RESET}" + fi + + echo -ne "${CYAN}Enter the image compression percent (default: 0): ${RESET}" + read compression_percent + while [[ -n "$compression_percent" && ! "$compression_percent" =~ ^[0-9]+$ ]]; do + echo -e "${RED}Invalid number. Please enter a valid number.${RESET}" + echo -ne "${CYAN}Enter the image compression percent (default: 0): ${RESET}" + read compression_percent + done + if [[ -n "$compression_percent" && "$compression_percent" -ne 0 ]]; then + set_value "x-zipline-image-compression-percent" "$compression_percent" + echo -e "${GREEN}Image compression percent set to: $compression_percent${RESET}" + else + echo -e "${YELLOW}Using default compression percent (0). Not saving.${RESET}" + fi + + echo -ne "${CYAN}Do you want to keep the original name on download? (y/n): ${RESET}" + read keep_original_name + while [[ -n "$keep_original_name" && ! "$keep_original_name" =~ ^(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 keep the original name on download? (y/n): ${RESET}" + read keep_original_name + done + if [[ "$keep_original_name" =~ ^(y|Y)$ ]]; then + keep_original_name="true" + else + keep_original_name="false" + fi + set_value "x-zipline-original-name" "$keep_original_name" + echo -e "${GREEN}Keep original name set to: $keep_original_name${RESET}" + + echo -ne "${CYAN}What do you want the name format to be? (default: date): ${RESET}" + read name_format + while [[ -n "$name_format" && ! "$name_format" =~ ^(date|random|uuid|name|gfycat)$ ]]; do + echo -e "${RED}Invalid option. Please enter 'date', 'random', 'uuid', 'name', or 'gfycat'.${RESET}" + echo -ne "${CYAN}What do you want the name format to be? (default: date): ${RESET}" + read name_format + done + if [[ -n "$name_format" ]]; then + set_value "x-zipline-format" "$name_format" + echo -e "${GREEN}Name format set to: $name_format${RESET}" + else + echo -e "${YELLOW}Using default name format (date). Not saving.${RESET}" + fi + + echo -e "${GREEN}Upload options set successfully.${RESET}" + fi fi while [[ -z "$auth" ]]; do diff --git a/helpers/variables.sh b/helpers/variables.sh index 5d51d33..5a781df 100644 --- a/helpers/variables.sh +++ b/helpers/variables.sh @@ -18,6 +18,12 @@ declare -A ALLOWED_KEYS=( ["DOMAIN"]="^.+$" ["DEFAULT_OPTION"]="^(upload|save|copy)$" ["SERVICE"]="^(zipline|nest|fakecrime|ez|guns|pixelvault)$" + + # zipline specific + ["x-zipline-max-views"]="^.+$" + ["x-zipline-image-compression-percent"]="^.+$" + ["x-zipline-original-name"]="^.+$" + ["x-zipline-format"]="^.+$" ) REQUIRED_KEYS=( diff --git a/main.sh b/main.sh index 00a3ad0..f491018 100755 --- a/main.sh +++ b/main.sh @@ -87,8 +87,6 @@ while [[ $# -gt 0 ]]; do esac done -REQUIRED_COMMANDS=("notify-send" "curl" "jq") - SHOULD_UPLOAD=$(get_value "DEFAULT_OPTION") [[ "$SHOULD_UPLOAD" == "upload" ]] && SHOULD_UPLOAD=true || SHOULD_UPLOAD=false @@ -98,10 +96,17 @@ SHOW_NOTIFICATIONS=$(get_value "SHOW_NOTIFICATIONS") SHOW_CAPTURE_PREVIEW=$(get_value "SHOW_CAPTURE_PREVIEW") [[ "$SHOW_CAPTURE_PREVIEW" == "true" ]] && SHOW_CAPTURE_PREVIEW=true || SHOW_CAPTURE_PREVIEW=false +REQUIRED_COMMANDS=("notify-send" "curl" "jq") + if [[ -n "$WAYLAND_DISPLAY" ]]; then - SCREENSHOT_CMD=("grim" "-g") - SELECT_REGION="slurp" - REQUIRED_COMMANDS+=("wl-copy" "grim" "slurp") + if [[ "$XDG_CURRENT_DESKTOP" == "KDE" ]]; then + SCREENSHOT_CMD=("spectacle" "-rbn" "-o") + REQUIRED_COMMANDS+=("xclip" "spectacle") + else + SCREENSHOT_CMD=("grim" "-g") + SELECT_REGION="slurp" + REQUIRED_COMMANDS+=("wl-copy" "grim" "slurp") + fi elif [[ -n "$DISPLAY" ]]; then SCREENSHOT_CMD=("flameshot" "gui" "-p") REQUIRED_COMMANDS+=("xclip" "flameshot") @@ -183,7 +188,11 @@ if [[ "$SHOULD_COPY" == "true" ]]; then fi if [[ -n "$WAYLAND_DISPLAY" ]]; then - "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$FILE" + if [[ "$XDG_CURRENT_DESKTOP" == "KDE" && -n "$WAYLAND_DISPLAY" ]]; then + "${SCREENSHOT_CMD[@]}" "$FILE" + else + "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$FILE" + fi else "${SCREENSHOT_CMD[@]}" "$FILE" fi @@ -272,7 +281,11 @@ if [[ -z "$USER_FILE" ]]; then fi if [[ -n "$WAYLAND_DISPLAY" ]]; then - "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$FILE" + if [[ "$XDG_CURRENT_DESKTOP" == "KDE" && -n "$WAYLAND_DISPLAY" ]]; then + "${SCREENSHOT_CMD[@]}" "$FILE" + else + "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$FILE" + fi else "${SCREENSHOT_CMD[@]}" "$FILE" fi @@ -286,10 +299,6 @@ if [[ -z "$USER_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 @@ -299,21 +308,56 @@ if [[ ! -f "$FILE" ]]; then exit 1 fi +HEADERS=() +if [[ "$SERVICE" == "zipline" ]]; then + declare -A ZIPLINE_HEADERS + ZIPLINE_HEADERS=$(get_starting_with "x-zipline") + for key in "${!ZIPLINE_HEADERS[@]}"; do + header_name="${key#ZIPLINE__}" | tr '[:upper:]' '[:lower:]' | tr '_' '-' + header_value="${ZIPLINE_HEADERS[$key]}" + headers+=("-H" "$header_name: $header_value") + done +fi + +headers+=("-H" "${UPLOAD_HEADERS[$SERVICE]}: $AUTH") + +echo headers "${headers[@]}" + FILE_URL="" +RESPONSE=$(curl -s -w "%{http_code}" -o /tmp/upload_response.json \ + -X POST -F "file=@$FILE" "${headers[@]}" "$DOMAIN") -REPONSE=$(curl -s -X POST \ - -F "file=@$FILE" \ - -H "${UPLOAD_HEADERS[$SERVICE]}: $AUTH" \ - "$DOMAIN") +HTTP_CODE="${RESPONSE:(-3)}" +BODY=$(cat /tmp/upload_response.json) +rm /tmp/upload_response.json -if ! echo "$REPONSE" | jq . &>/dev/null; then - echo -e "${RED}Error: Invalid response from server.${RESET}" +if [[ "$HTTP_CODE" -ne 200 ]]; then + echo -e "${RED}Error: Upload failed with status code $HTTP_CODE.${RESET}" + echo "$BODY" exit 1 fi -FILE_URL=$(echo "$REPONSE" | jq -r ".${UPLOAD_JSON_KEYS[$SERVICE]}") -if [[ "$FILE_URL" == "null" ]]; then +if ! echo "$BODY" | jq . &>/dev/null; then + echo -e "${RED}Error: Invalid JSON response from server.${RESET}" + echo "$BODY" + exit 1 +fi + +FILE_URL=$(echo "$BODY" | jq -r ".${UPLOAD_JSON_KEYS[$SERVICE]}") +if [[ "$FILE_URL" == "null" || -z "$FILE_URL" ]]; then echo -e "${RED}Error: File URL not found in response.${RESET}" + echo "$BODY" + exit 1 +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 @@ -332,13 +376,7 @@ else 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 +# delete if should not save and not a user file +if [[ "$SHOULD_SAVE" != "true" && "$USER_FILE" == "" ]]; then + rm -f "$FILE" fi \ No newline at end of file