mirror of
https://github.com/hyzendust/command_upload.git
synced 2026-02-15 00:21:17 +01:00
Add: -c to automatically copy URL to clipboard.
This commit is contained in:
26
upload.sh
26
upload.sh
@@ -1,11 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
# upload.sh - upload a file with POST and print the returned https:// link
|
||||
# upload.sh - upload a file with POST, print the returned https:// link
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Change the UPLOAD_URL to a desired one
|
||||
UPLOAD_URL="https://upload.freedoms4.top"
|
||||
|
||||
COPY_TO_CLIPBOARD=false
|
||||
|
||||
print_usage() {
|
||||
cat <<USAGE
|
||||
Usage: $0 [options] -f <file>
|
||||
@@ -16,6 +18,7 @@ Options:
|
||||
-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)
|
||||
-c, --clipboard Copy returned URL to clipboard
|
||||
-h, --help Show this help
|
||||
USAGE
|
||||
}
|
||||
@@ -25,7 +28,6 @@ USER_AUTH=""
|
||||
declare -a HEADERS
|
||||
declare -a FIELDS
|
||||
FILE=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-f|--file) FILE="$2"; shift 2 ;;
|
||||
@@ -33,6 +35,7 @@ while [[ $# -gt 0 ]]; do
|
||||
-H|--header) HEADERS+=("$2"); shift 2 ;;
|
||||
-F|--field) FIELDS+=("$2"); shift 2 ;;
|
||||
-U|--url) UPLOAD_URL="$2"; shift 2 ;;
|
||||
-c|--clipboard) COPY_TO_CLIPBOARD=true; shift ;;
|
||||
-h|--help) print_usage; exit 0 ;;
|
||||
--) shift; break ;;
|
||||
-*)
|
||||
@@ -52,13 +55,11 @@ while [[ $# -gt 0 ]]; do
|
||||
;;
|
||||
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
|
||||
@@ -70,27 +71,34 @@ 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
|
||||
# Extract the URL
|
||||
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"
|
||||
if $COPY_TO_CLIPBOARD; then
|
||||
if command -v xclip >/dev/null 2>&1; then
|
||||
echo -n "$link" | xclip -selection clipboard
|
||||
>&2 echo "Copied to clipboard."
|
||||
elif command -v pbcopy >/dev/null 2>&1; then
|
||||
echo -n "$link" | pbcopy
|
||||
>&2 echo "Copied to clipboard."
|
||||
else
|
||||
>&2 echo "Clipboard copy requested, but no clipboard utility found (install xclip or pbcopy)."
|
||||
fi
|
||||
fi
|
||||
exit 0
|
||||
else
|
||||
>&2 echo "Upload failed or no URL found in server response."
|
||||
|
||||
Reference in New Issue
Block a user