From 6e2cf940910a852042fc60554ff35859722c3244 Mon Sep 17 00:00:00 2001 From: psychhim Date: Sat, 25 Oct 2025 00:26:37 +0530 Subject: [PATCH] First commit --- upload.sh | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 upload.sh diff --git a/upload.sh b/upload.sh new file mode 100755 index 0000000..1cfafc9 --- /dev/null +++ b/upload.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash +# upload.sh - upload a file with POST and print the returned https:// link + +set -euo pipefail + +# Change the UPLOAD_URL to a desired one +UPLOAD_URL="https://upload.freedoms4.top" + +print_usage() { + cat < + +Options: + -f, --file PATH File to upload (required) + -u, --user USER:PASS HTTP Basic auth (pass to curl --user) + -H, --header "K: V" Additional header (can be repeated) + -F, --field name=value Additional form field (can be repeated) + -U, --url URL Override upload URL (default: $UPLOAD_URL) + -h, --help Show this help +USAGE +} + +# parse args +USER_AUTH="" +declare -a HEADERS +declare -a FIELDS +FILE="" + +while [[ $# -gt 0 ]]; do + case "$1" in + -f|--file) FILE="$2"; shift 2 ;; + -u|--user) USER_AUTH="$2"; shift 2 ;; + -H|--header) HEADERS+=("$2"); shift 2 ;; + -F|--field) FIELDS+=("$2"); shift 2 ;; + -U|--url) UPLOAD_URL="$2"; shift 2 ;; + -h|--help) print_usage; exit 0 ;; + --) shift; break ;; + -*) + echo "Unknown option: $1" >&2 + print_usage + exit 2 + ;; + *) + if [[ -z "$FILE" ]]; then + FILE="$1" + shift + else + echo "Unexpected argument: $1" >&2 + print_usage + exit 2 + fi + ;; + esac +done + +if [[ -z "$FILE" ]]; then + echo "Error: file is required." >&2 + print_usage + exit 2 +fi + +if [[ ! -f "$FILE" ]]; then + echo "Error: file not found: $FILE" >&2 + exit 3 +fi + +# Build curl args +declare -a CURL_OPTS +CURL_OPTS+=( -s ) +if [[ -n "$USER_AUTH" ]]; then + CURL_OPTS+=( --user "$USER_AUTH" ) +fi + +for h in "${HEADERS[@]}"; do + CURL_OPTS+=( --header "$h" ) +done + +for f in "${FIELDS[@]}"; do + CURL_OPTS+=( --form "$f" ) +done + +CURL_OPTS+=( --form "file=@${FILE}" ) + +# Perform request and capture response +response="$(curl "${CURL_OPTS[@]}" "$UPLOAD_URL" 2>/dev/null || true)" + +# Extract the first URL (allow spaces) and URL-encode spaces +link="$(printf '%s\n' "$response" \ + | perl -nle 'if (m{https?://.*}) { $url=$&; $url=~s/ /%20/g; print $url; exit }')" + +if [[ -n "$link" ]]; then + >&2 echo "Upload successful!" + echo "$link" + exit 0 +else + >&2 echo "Upload failed or no URL found in server response." + >&2 echo "---- server response ----" + >&2 printf '%s\n' "$response" + >&2 echo "-------------------------" + exit 4 +fi