PairDrop/pairdrop
2023-02-20 17:42:02 +01:00

123 lines
3.5 KiB
Bash

#!/bin/bash
set -e
############################################################
# Help #
############################################################
help()
{
# Display Help
echo "Send files or text via PairDrop via commandline."
echo
echo -e "To send files:\t$(basename "$0") file"
echo -e "To send text:\t$(basename "$0") -t \"text\""
}
openPairDrop()
{
# openPairDrop
url=$domain
if [[ -n $params ]];then
url=$url"?"$params
fi
if [[ -n $hash ]];then
url=$url"#"$hash
fi
echo "PairDrop is opening in a browser."
if [[ $OS == "Windows" ]];then
start "$url"
elif [[ $OS == "Mac" ]];then
open "$url"
else
xdg-open "$url"
fi
exit
}
setOs()
{
unameOut=$(uname -a)
case "${unameOut}" in
*Microsoft*) OS="WSL";; #must be first since Windows subsystem for linux will have Linux in the name too
*microsoft*) OS="WSL2";; #WARNING: My v2 uses ubuntu 20.4 at the moment slightly different name may not always work
Linux*) OS="Linux";;
Darwin*) OS="Mac";;
CYGWIN*) OS="Cygwin";;
MINGW*) OS="Windows";;
*Msys) OS="Windows";;
*) OS="UNKNOWN:${unameOut}"
esac
}
############################################################
############################################################
# Main program #
############################################################
############################################################
domain="https://pairdrop.net/"
domain="https://192.168.2.90:8443/"
setOs
############################################################
# Process the input options. Add options as needed. #
############################################################
# Get the options
# open PairDrop if no options are given
[[ $# -eq 0 ]] && openPairDrop && exit
# display help and exit if first argument is "--help" or more than 2 arguments are given
[ "$1" = "--help" ] | [[ $# -gt 2 ]] && help && exit
while getopts "ht:*" option; do
case $option in
t) # Send text
params="base64text=hash"
hash=$(echo -n "${OPTARG}" | base64)
if [[ $(echo -n "$hash" | wc -m) -gt 32600 ]];then
params="base64text=paste"
if [[ $OS == "Windows" || $OS == "WSL" || $OS == "WSL2" ]];then
echo -n $hash | clip.exe
elif [[ $OS == "Mac" ]];then
echo -n $hash | pbcopy
else
(echo -n $hash | xclip) || echo "You need to install xclip for sending bigger files from cli"
fi
hash=
fi
openPairDrop
exit;;
h | ?) # display help and exit
help
exit;;
esac
done
# Send file(s)
# display help and exit if 2 arguments are given or if file does not exist
[[ $# -eq 2 ]] || [[ ! -a $1 ]] && help && exit
params="base64zip=hash"
if [[ -d $1 ]]; then
zipPath="${1::-1}_pairdrop_temp.zip"
[[ -a "$zipPath" ]] && echo "Cannot overwrite $zipPath. Please remove first." && exit
zip -r -q -b /tmp/ "$zipPath" "$1"
hash=$(zip -q -b /tmp/ - "$zipPath" | base64 -w 0)
rm "$zipPath"
else
hash=$(zip -q -b /tmp/ - "$1" | base64 -w 0)
fi
if [[ $(echo -n "$hash" | wc -m) -gt 32600 ]];then
params="base64zip=paste"
if [[ $OS == "Windows" || $OS == "WSL" || $OS == "WSL2" ]];then
echo -n $hash | clip.exe
elif [[ $OS == "Mac" ]];then
echo -n $hash | pbcopy
else
(echo -n $hash | xclip) || echo "You need to install xclip for sending bigger files from cli"
fi
hash=
fi
openPairDrop