From 8f0e465b8e6250737407705bceda5fa5da6e7fa8 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Tue, 21 Feb 2023 23:44:41 +0100 Subject: [PATCH] pairdrop-cli: change domain via flag, move bash file to separate folder and add console logs to ui.js --- pairdrop | 123 --------------- pairdrop-cli/pairdrop | 181 ++++++++++++++++++++++ public/scripts/ui.js | 8 +- public_included_ws_fallback/scripts/ui.js | 8 +- 4 files changed, 195 insertions(+), 125 deletions(-) delete mode 100644 pairdrop create mode 100644 pairdrop-cli/pairdrop diff --git a/pairdrop b/pairdrop deleted file mode 100644 index baebfd8..0000000 --- a/pairdrop +++ /dev/null @@ -1,123 +0,0 @@ -#!/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 diff --git a/pairdrop-cli/pairdrop b/pairdrop-cli/pairdrop new file mode 100644 index 0000000..1d577dd --- /dev/null +++ b/pairdrop-cli/pairdrop @@ -0,0 +1,181 @@ +#!/bin/bash +set -e + +############################################################ +# Help # +############################################################ +help() +{ + # Display Help + echo "Send files or text with PairDrop via commandline." + echo "Current domain: ${DOMAIN}" + echo + echo "Usage:" + echo -e "Send files:\t\t$(basename "$0") file/directory" + echo -e "Send text:\t\t$(basename "$0") -t \"text\"" + echo -e "Specify domain:\t$(basename "$0") -d \"https://pairdrop.net/\"" +} + +openPairDrop() +{ + url="$DOMAIN" + if [[ -n $params ]];then + url="${url}?${params}" + fi + if [[ -n $hash ]];then + url="${url}#${hash}" + fi + + echo "PairDrop is opening at $DOMAIN" + if [[ $OS == "Windows" ]];then + start "$url" + elif [[ $OS == "Mac" ]];then + open "$url" + elif [[ $OS == "WSL" || $OS == "WSL2" ]];then + powershell.exe /c "Start-Process ${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 +} + +specifyDomain() +{ + [[ ! $1 = http* ]] || [[ ! $1 = */ ]] && echo "Incorrect format. Specify domain like https://pairdrop.net/" && exit + echo "DOMAIN=${1}" > "${SCRIPTPATH}/.pairdrop-cli-config" + echo -e "Domain is now set to:\n$1" +} + +sendText() +{ + 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 +} + +sendFiles() +{ + params="base64zip=hash" + if [[ $1 == */ ]]; then + path="${1::-1}" + else + path=$1 + fi + zipPath="${path}_pairdrop.zip" + zipPath=${zipPath// /_} + + [[ -a "$zipPath" ]] && echo "Cannot overwrite $zipPath. Please remove first." && exit + + if [[ -d $path ]]; then + zipPathTemp="temp_${zipPath}" + [[ -a "$zipPathTemp" ]] && echo "Cannot overwrite $zipPathTemp. Please remove first." && exit + echo "Processing directory..." + + # Create zip files temporarily to send directory + zip -q -b /tmp/ -r "$zipPath" "$path" + zip -q -b /tmp/ "$zipPathTemp" "$zipPath" + + hash=$(base64 -w 0 "$zipPathTemp") + + # remove temporary temp file + rm "$zipPathTemp" + else + echo "Processing file..." + + # Create zip file temporarily to send file + zip -q -b /tmp/ "$zipPath" "$path" + + hash=$(base64 -w 0 "$zipPath") + fi + + # remove temporary temp file + rm "$zipPath" + + 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 + exit +} + +############################################################ +############################################################ +# Main program # +############################################################ +############################################################ +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +[ ! -f "${SCRIPTPATH}/.pairdrop-cli-config" ] && + specifyDomain "https://pairdrop.net/" && + [ ! -f "${SCRIPTPATH}/.pairdrop-cli-config" ] && + echo "Could not create config file. Add 'DOMAIN=https://pairdrop.net/' to a file called .pairdrop-cli-config in the same file as this pairdrop bash file" + +[ ! -f "${SCRIPTPATH}/.pairdrop-cli-config" ] || export "$(grep -v '^#' "${SCRIPTPATH}/.pairdrop-cli-config" | xargs)" + +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 "d:ht:*" option; do + case $option in + d) # specify domain + specifyDomain "$2" + exit;; + t) # Send text + sendText + 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 +sendFiles "$1" +exit diff --git a/public/scripts/ui.js b/public/scripts/ui.js index 1d38868..f829f32 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -1102,6 +1102,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Text(base64Hash) .catch(_ => { Events.fire('notify-user', 'Text content is incorrect.'); + console.log("Text content incorrect.") }).finally(_ => { this.hide(); }); @@ -1111,6 +1112,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Text(base64Text) .catch(_ => { Events.fire('notify-user', 'Text content is incorrect.'); + console.log("Text content incorrect.") }).finally(_ => { this.hide(); }); @@ -1123,6 +1125,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Zip(base64Hash) .catch(_ => { Events.fire('notify-user', 'File content is incorrect.'); + console.log("File content incorrect.") }).finally(_ => { this.hide(); }); @@ -1141,7 +1144,8 @@ class Base64ZipDialog extends Dialog { async processClipboard(type) { if (!navigator.clipboard.readText) { - Events.fire('notify-user', 'This feature is not available on your device.'); + Events.fire('notify-user', 'This feature is not available on your browser.'); + console.log("navigator.clipboard.readText() is not available on your browser.") this.hide(); return; } @@ -1156,6 +1160,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Text(base64) .catch(_ => { Events.fire('notify-user', 'Clipboard content is incorrect.'); + console.log("Clipboard content is incorrect.") }).finally(_ => { this.hide(); }); @@ -1163,6 +1168,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Zip(base64) .catch(_ => { Events.fire('notify-user', 'Clipboard content is incorrect.'); + console.log("Clipboard content is incorrect.") }).finally(_ => { this.hide(); }); diff --git a/public_included_ws_fallback/scripts/ui.js b/public_included_ws_fallback/scripts/ui.js index e7dd79d..796230f 100644 --- a/public_included_ws_fallback/scripts/ui.js +++ b/public_included_ws_fallback/scripts/ui.js @@ -1103,6 +1103,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Text(base64Hash) .catch(_ => { Events.fire('notify-user', 'Text content is incorrect.'); + console.log("Text content incorrect.") }).finally(_ => { this.hide(); }); @@ -1112,6 +1113,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Text(base64Text) .catch(_ => { Events.fire('notify-user', 'Text content is incorrect.'); + console.log("Text content incorrect.") }).finally(_ => { this.hide(); }); @@ -1124,6 +1126,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Zip(base64Hash) .catch(_ => { Events.fire('notify-user', 'File content is incorrect.'); + console.log("File content incorrect.") }).finally(_ => { this.hide(); }); @@ -1142,7 +1145,8 @@ class Base64ZipDialog extends Dialog { async processClipboard(type) { if (!navigator.clipboard.readText) { - Events.fire('notify-user', 'This feature is not available on your device.'); + Events.fire('notify-user', 'This feature is not available on your browser.'); + console.log("navigator.clipboard.readText() is not available on your browser.") this.hide(); return; } @@ -1157,6 +1161,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Text(base64) .catch(_ => { Events.fire('notify-user', 'Clipboard content is incorrect.'); + console.log("Clipboard content is incorrect.") }).finally(_ => { this.hide(); }); @@ -1164,6 +1169,7 @@ class Base64ZipDialog extends Dialog { this.processBase64Zip(base64) .catch(_ => { Events.fire('notify-user', 'Clipboard content is incorrect.'); + console.log("Clipboard content is incorrect.") }).finally(_ => { this.hide(); });