Files
f0ckm-uploader/uploader.sh
2026-06-13 16:14:37 +02:00

75 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# ==========================================
# Notification Wrapper
# ==========================================
show_notification() {
local title="$1"
local message="$2"
local icon="${3:-cloud-upload-symbolic}"
if command -v kdialog &> /dev/null; then
kdialog --passivepopup "$message" 5 --title "$title" --icon "$icon"
else
echo "$title: $message"
fi
}
# ==========================================
# Configuration
# ==========================================
API_URL="http://<f0ckm>/api/v2/upload"
API_KEY=""
# ==========================================
FILE="$1"
if [ -z "$FILE" ]; then
show_notification "Upload Failed" "No file provided to uploader." "dialog-error"
exit 1
fi
# Determine clipboard command (Wayland or X11)
if command -v wl-copy &> /dev/null; then
COPY_CMD="wl-copy"
elif command -v xclip &> /dev/null; then
COPY_CMD="xclip -selection clipboard"
else
show_notification "Upload Error" "Please install wl-clipboard (Wayland) or xclip (X11)." "dialog-error"
exit 1
fi
show_notification "Uploading..." "Uploading $(basename "$FILE")" "cloud-upload-symbolic"
# Detect the real MIME type so curl doesn't fall back to application/octet-stream
MIME_TYPE=$(file --mime-type -b "$FILE")
# Perform the upload
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API_URL" \
-H "X-Api-Key: $API_KEY" \
-F "file=@$FILE;type=$MIME_TYPE" \
-F "rating=" \
-F "tags=" \
-F "is_oc=0")
# Extract the HTTP status code (last line) and the body
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then
# Parse the direct file URL from the JSON response.
# Expects a response containing a "file_url" field.
URL=$(echo "$BODY" | grep -oP '"file_url"\s*:\s*"\K[^"]+')
# Fallback: if no file_url field is found, just copy the whole body if it's a raw string
if [ -z "$URL" ]; then
URL="$BODY"
fi
echo -n "$URL" | $COPY_CMD
show_notification "Upload Successful" "Link copied to clipboard!<br><br><a href=\"$URL\">$URL</a>" "cloud-upload-symbolic"
else
show_notification "Upload Failed" "HTTP Error $HTTP_CODE\n$BODY" "dialog-error"
exit 1
fi