Add src/simple-send.sh

This commit is contained in:
colin 2024-10-01 10:37:06 -04:00
parent 10b7652319
commit ef5bc53093
1 changed files with 70 additions and 0 deletions

70
src/simple-send.sh Normal file
View File

@ -0,0 +1,70 @@
# Constants for ffsend binary and server with environment variable overrides
FFSEND_URL="${FFSEND_URL:-https://github.com/timvisee/ffsend/releases/download/v0.2.76/ffsend-v0.2.76-linux-x64}"
FFSEND_DIR="${FFSEND_DIR:-$HOME/.local/bin}"
FFSEND_PATH="${FFSEND_PATH:-$FFSEND_DIR/ffsend}"
DEFAULT_FFSEND_HOST="${DEFAULT_FFSEND_HOST:-https://send.aenow.fun}"
# Private: Ensure ffsend is installed only when needed
_install_ffsend() {
# Check if ffsend is already installed
if [ -f "$FFSEND_PATH" ]; then
return
fi
echo "ffsend not found, checking if the download URL is valid..."
# Check if the URL is valid by making a HEAD request
if curl -Isf "$FFSEND_URL" > /dev/null; then
echo "URL is valid, downloading ffsend..."
mkdir -p "$FFSEND_DIR"
curl -L "$FFSEND_URL" -o "$FFSEND_PATH"
chmod +x "$FFSEND_PATH"
else
echo "Error: The ffsend download URL is not valid. Please check the URL: $FFSEND_URL"
exit 1
fi
}
# Private: Generate random password, not accessible from the CLI
_generate_password() {
openssl rand -base64 12
}
# Private: Upload file or piped data function
_ffsend_upload() {
local file=$1
local password=$(_generate_password)
# Use the custom host if FFSEND_CUSTOM_HOST is set, otherwise use the default host
local ffsend_host="${FFSEND_CUSTOM_HOST:-$DEFAULT_FFSEND_HOST}"
# Upload the file or data with 7-day expiry and the generated password
$FFSEND_PATH upload --host "$ffsend_host" --expiry-time 7d --password "$password" "$file"
# Print the password and the download URL
echo "Password: $password"
}
# Public: Main send function to wrap the upload, accepts files and piped data
send() {
# Ensure ffsend is installed before proceeding
_install_ffsend
if [ -p /dev/stdin ]; then
# Handle piped input
local temp_file=$(mktemp)
cat > "$temp_file"
_ffsend_upload "$temp_file"
rm "$temp_file"
elif [ -f "$1" ]; then
# Handle file input as an argument
_ffsend_upload "$1"
else
# Print usage if no input or argument is provided
echo "Usage: send <file> or echo 'data' | send"
return 1
fi
}
# To override any of the defaults, set the corresponding environment variables:
# FFSEND_URL, FFSEND_DIR, FFSEND_PATH, DEFAULT_FFSEND_HOST