commit 7aac3d6b386f7880cb4c6ce0575c0b2b060e8299 Author: creations Date: Sat Mar 29 08:10:40 2025 -0400 first commit diff --git a/show_image.py b/show_image.py new file mode 100644 index 0000000..38b2621 --- /dev/null +++ b/show_image.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +import tkinter as tk +from PIL import Image, ImageTk +import screeninfo, sys, os + +def show_image(image_path, box_width=400, box_height=300, hover_header=None, click_url=None): + root = tk.Tk() + root.overrideredirect(True) + root.withdraw() + root.after(100, root.deiconify) + root.attributes("-topmost", True) + root.attributes("-alpha", 0.85) + + img = Image.open(image_path) + img.thumbnail((box_width, box_height)) + img_tk = ImageTk.PhotoImage(img) + + monitors = screeninfo.get_monitors() + primary_screen = next((m for m in monitors if m.is_primary), monitors[0]) + + screen_x = primary_screen.x + screen_y = primary_screen.y + screen_width = primary_screen.width + screen_height = primary_screen.height + + x_pos = screen_x + screen_width - box_width - 20 + y_pos = screen_y + screen_height - box_height - 60 + + root.geometry(f"{box_width}x{box_height}+{x_pos}+{y_pos}") + + frame = tk.Frame(root, bg="black") + frame.pack(fill="both", expand=True) + + label = tk.Label(frame, image=img_tk, borderwidth=0) + label.image = img_tk + label.place(relx=0.5, rely=0.5, anchor="center") + + header_label = tk.Label(root, text=hover_header, bg="black", fg="white", font=("Arial", 10)) + + def show_header(event): + if hover_header: + header_label.place(relx=0.5, rely=0, anchor="n", relwidth=1) + + def hide_header(event): + if hover_header: + header_label.place_forget() + + def close_window(event=None): + root.destroy() + + root.bind("", close_window if click_url is None else lambda e: os.system(f"xdg-open {click_url}")) + root.bind("", show_header) + root.bind("", hide_header) + + root.after(5000, close_window) + root.mainloop() + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: show_image.py [hover_header] [click_url]") + sys.exit(1) + + image_path = sys.argv[1] + hover_header = sys.argv[2] if len(sys.argv) > 2 else None + click_url = sys.argv[3] if len(sys.argv) > 3 else None + + show_image(image_path, hover_header=hover_header, click_url=click_url) diff --git a/upload.sh b/upload.sh new file mode 100755 index 0000000..e94a688 --- /dev/null +++ b/upload.sh @@ -0,0 +1,154 @@ +#!/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"