add progress

This commit is contained in:
2026-06-13 16:38:02 +02:00
parent 7f4d6af775
commit 9ad90982cc

View File

@@ -17,9 +17,8 @@ show_notification() {
# ==========================================
# Configuration
# ==========================================
API_URL="http://<f0ckm>/api/v2/upload"
API_URL="http://localhost:1337/api/v2/upload"
API_KEY=""
# ==========================================
FILE="$1"
@@ -39,35 +38,122 @@ else
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")
BASENAME=$(basename "$FILE")
# Perform the upload
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API_URL" \
# ==========================================
# Progress bar setup (kdialog + qdbus)
# ==========================================
# Prefer qdbus6 on KDE6, fall back to qdbus on KDE5
QDBUS_CMD=""
if command -v qdbus6 &>/dev/null; then
QDBUS_CMD="qdbus6"
elif command -v qdbus &>/dev/null; then
QDBUS_CMD="qdbus"
fi
PROGRESS_SERVICE=""
PROGRESS_OBJECT=""
if command -v kdialog &>/dev/null && [ -n "$QDBUS_CMD" ]; then
DBUS_REF=$(kdialog --title "f0ckm Uploader" \
--progressbar "Uploading $BASENAME..." 100 2>/dev/null)
PROGRESS_SERVICE=$(echo "$DBUS_REF" | awk '{print $1}')
PROGRESS_OBJECT=$(echo "$DBUS_REF" | awk '{print $2}')
fi
# If no progress dialog is available, show a passive "uploading" popup instead
if [ -z "$PROGRESS_SERVICE" ]; then
show_notification "Uploading..." "Uploading $BASENAME" "cloud-upload-symbolic"
fi
# ==========================================
# Upload via curl with live progress
# ==========================================
# FIFO routes curl's stderr (progress bar) to the reader loop
FIFO=$(mktemp -u /tmp/f0ckm_XXXXXX)
mkfifo "$FIFO"
RESPONSE_TMP=$(mktemp)
# --progress-meter forces curl to output progress table even when redirected to a FIFO
curl --progress-meter -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")
-F "is_oc=0" \
>"$RESPONSE_TMP" 2>"$FIFO" &
CURL_PID=$!
# Extract the HTTP status code (last line) and the body
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
# Parse the curl progress meter output in real-time
if [ -n "$PROGRESS_SERVICE" ] && [ -n "$PROGRESS_OBJECT" ]; then
while IFS= read -r -d $'\r' line; do
# Clean line and split into fields
line=$(echo "$line" | tr -d '\n\r')
read -ra fields <<< "$line"
# Check if valid progress line: starts with percentage digit and has >= 11 fields
if [[ ${#fields[@]} -ge 11 ]] && [[ "${fields[0]}" =~ ^[0-9]+$ ]]; then
PCT="${fields[4]}" # % Xferd
if [[ ! "$PCT" =~ ^[0-9]+$ ]]; then
PCT="${fields[0]}"
fi
TOTAL_SIZE="${fields[1]}"
XFERD="${fields[5]}"
SPEED="${fields[11]}"
TIME_LEFT="${fields[10]}"
# Format speed (append /s if it looks like a size unit)
if [[ "$SPEED" =~ [0-9]+[bBkKmMgGtT]?$ ]]; then
SPEED="${SPEED}/s"
fi
LABEL="Uploading $BASENAME
($XFERD / $TOTAL_SIZE) @ $SPEED$TIME_LEFT remaining"
$QDBUS_CMD "$PROGRESS_SERVICE" "$PROGRESS_OBJECT" Set "" value "$PCT" 2>/dev/null || true
$QDBUS_CMD "$PROGRESS_SERVICE" "$PROGRESS_OBJECT" setLabelText "$LABEL" 2>/dev/null || true
fi
done < "$FIFO"
else
# No progress dialog — drain the FIFO in the background so curl isn't blocked
cat "$FIFO" >/dev/null &
fi
wait "$CURL_PID"
# Close the progress dialog
if [ -n "$PROGRESS_SERVICE" ] && [ -n "$PROGRESS_OBJECT" ]; then
$QDBUS_CMD "$PROGRESS_SERVICE" "$PROGRESS_OBJECT" close 2>/dev/null || true
fi
rm -f "$FIFO"
# ==========================================
# Parse response
# ==========================================
HTTP_CODE=$(tail -n1 "$RESPONSE_TMP")
BODY=$(sed '$d' "$RESPONSE_TMP")
rm -f "$RESPONSE_TMP"
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
# Fallback: if no file_url field is found, copy the whole body
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"
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