mirror of
https://github.com/hyzendust/command_upload.git
synced 2026-02-15 03:41:16 +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
|
#!/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
|
set -euo pipefail
|
||||||
|
|
||||||
# Change the UPLOAD_URL to a desired one
|
# Change the UPLOAD_URL to a desired one
|
||||||
UPLOAD_URL="https://upload.freedoms4.top"
|
UPLOAD_URL="https://upload.freedoms4.top"
|
||||||
|
|
||||||
|
COPY_TO_CLIPBOARD=false
|
||||||
|
|
||||||
print_usage() {
|
print_usage() {
|
||||||
cat <<USAGE
|
cat <<USAGE
|
||||||
Usage: $0 [options] -f <file>
|
Usage: $0 [options] -f <file>
|
||||||
@@ -16,6 +18,7 @@ Options:
|
|||||||
-H, --header "K: V" Additional header (can be repeated)
|
-H, --header "K: V" Additional header (can be repeated)
|
||||||
-F, --field name=value Additional form field (can be repeated)
|
-F, --field name=value Additional form field (can be repeated)
|
||||||
-U, --url URL Override upload URL (default: $UPLOAD_URL)
|
-U, --url URL Override upload URL (default: $UPLOAD_URL)
|
||||||
|
-c, --clipboard Copy returned URL to clipboard
|
||||||
-h, --help Show this help
|
-h, --help Show this help
|
||||||
USAGE
|
USAGE
|
||||||
}
|
}
|
||||||
@@ -25,7 +28,6 @@ USER_AUTH=""
|
|||||||
declare -a HEADERS
|
declare -a HEADERS
|
||||||
declare -a FIELDS
|
declare -a FIELDS
|
||||||
FILE=""
|
FILE=""
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-f|--file) FILE="$2"; shift 2 ;;
|
-f|--file) FILE="$2"; shift 2 ;;
|
||||||
@@ -33,6 +35,7 @@ while [[ $# -gt 0 ]]; do
|
|||||||
-H|--header) HEADERS+=("$2"); shift 2 ;;
|
-H|--header) HEADERS+=("$2"); shift 2 ;;
|
||||||
-F|--field) FIELDS+=("$2"); shift 2 ;;
|
-F|--field) FIELDS+=("$2"); shift 2 ;;
|
||||||
-U|--url) UPLOAD_URL="$2"; shift 2 ;;
|
-U|--url) UPLOAD_URL="$2"; shift 2 ;;
|
||||||
|
-c|--clipboard) COPY_TO_CLIPBOARD=true; shift ;;
|
||||||
-h|--help) print_usage; exit 0 ;;
|
-h|--help) print_usage; exit 0 ;;
|
||||||
--) shift; break ;;
|
--) shift; break ;;
|
||||||
-*)
|
-*)
|
||||||
@@ -52,13 +55,11 @@ while [[ $# -gt 0 ]]; do
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ -z "$FILE" ]]; then
|
if [[ -z "$FILE" ]]; then
|
||||||
echo "Error: file is required." >&2
|
echo "Error: file is required." >&2
|
||||||
print_usage
|
print_usage
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -f "$FILE" ]]; then
|
if [[ ! -f "$FILE" ]]; then
|
||||||
echo "Error: file not found: $FILE" >&2
|
echo "Error: file not found: $FILE" >&2
|
||||||
exit 3
|
exit 3
|
||||||
@@ -70,27 +71,34 @@ CURL_OPTS+=( -s )
|
|||||||
if [[ -n "$USER_AUTH" ]]; then
|
if [[ -n "$USER_AUTH" ]]; then
|
||||||
CURL_OPTS+=( --user "$USER_AUTH" )
|
CURL_OPTS+=( --user "$USER_AUTH" )
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for h in "${HEADERS[@]}"; do
|
for h in "${HEADERS[@]}"; do
|
||||||
CURL_OPTS+=( --header "$h" )
|
CURL_OPTS+=( --header "$h" )
|
||||||
done
|
done
|
||||||
|
|
||||||
for f in "${FIELDS[@]}"; do
|
for f in "${FIELDS[@]}"; do
|
||||||
CURL_OPTS+=( --form "$f" )
|
CURL_OPTS+=( --form "$f" )
|
||||||
done
|
done
|
||||||
|
|
||||||
CURL_OPTS+=( --form "file=@${FILE}" )
|
CURL_OPTS+=( --form "file=@${FILE}" )
|
||||||
|
|
||||||
# Perform request and capture response
|
# Perform request and capture response
|
||||||
response="$(curl "${CURL_OPTS[@]}" "$UPLOAD_URL" 2>/dev/null || true)"
|
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" \
|
link="$(printf '%s\n' "$response" \
|
||||||
| perl -nle 'if (m{https?://.*}) { $url=$&; $url=~s/ /%20/g; print $url; exit }')"
|
| perl -nle 'if (m{https?://.*}) { $url=$&; $url=~s/ /%20/g; print $url; exit }')"
|
||||||
|
|
||||||
if [[ -n "$link" ]]; then
|
if [[ -n "$link" ]]; then
|
||||||
>&2 echo "Upload successful!"
|
>&2 echo "Upload successful!"
|
||||||
echo "$link"
|
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
|
exit 0
|
||||||
else
|
else
|
||||||
>&2 echo "Upload failed or no URL found in server response."
|
>&2 echo "Upload failed or no URL found in server response."
|
||||||
|
|||||||
Reference in New Issue
Block a user