Skip to content

Instantly share code, notes, and snippets.

@chrisonntag
Forked from tavinus/cloudsend.sh
Last active February 20, 2024 11:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisonntag/b36b1f76aec9043725bc4c002f85da16 to your computer and use it in GitHub Desktop.
Save chrisonntag/b36b1f76aec9043725bc4c002f85da16 to your computer and use it in GitHub Desktop.
Nextcloud Transfer
#!/bin/bash
# transfer
#
# Uses curl to send files to a Nextcloud folder
# and creates a share link in the command line.
#
# Usage: transfer <file>
# Help: transfer -h
#
# Fork from:
# Gustavo Arnosti Neves
# https://gist.github.com/tavinus/93bdbc051728748787dc22a58dfe58d8
CLOUDURL="<cloud.example.com>"
USER="<admin>"
PASS="<mypassword>"
FOLDER="Share"
PUBSUFFIX="remote.php/webdav"
SHARES_ENDPOINT="ocs/v2.php/apps/files_sharing/api/v1/shares"
HEADER='OCS-APIRequest: true'
INSECURE=''
initError() {
printf "%s\n" "Init Error! $1" >&2
printf "%s\n" "Try: $0 --help" >&2
exit 1
}
usage() {
printf "\n%s%s\n" "Parameters:" "
-h | --help Print this help and exits
-k | --insecure Uses curl with -k option (https insecure)"
printf "%s\n" "Script Location: $0"
printf "\n%s\n%s\n\n" "Use:" " transfer <filepath>"
exit 0
}
# Process parameters
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
fi
if [ "$1" = "-k" ] || [ "$1" = "--insecure" ]; then
INSECURE=' -k'
printf "%s\n" " > Insecure mode ON"
shift
fi
# Validate input
FILENAME="$1"
if [ ! -f "$FILENAME" ]; then
initError "Invalid input file: $FILENAME"
fi
if [ -z "$CLOUDURL" ]; then
initError "Empty URL! Nowhere to send..."
fi
# Check for curl
CURLBIN='/usr/bin/curl'
if [ ! -x "$CURLBIN" ]; then
CURLBIN="$(which curl 2>/dev/null)"
if [ ! -x "$CURLBIN" ]; then
initError "No curl found on system!"
fi
fi
# Send file
# Write output to tmpfile in order to make the progress-bar work
TMPFILE=$( mktemp -t transferXXX )
"$CURLBIN"$INSECURE -X PUT --progress-bar -u $USER:$PASS "https://$CLOUDURL/$PUBSUFFIX/$FOLDER/$FILENAME" -T "$FILENAME" >> "$TMPFILE"
rm -f $TMPFILE
# Share the uploaded file and print the created link
xml=$("$CURLBIN" -sS -X POST -H "$HEADER" --data "path=/$FOLDER/$FILENAME&shareType=3" -u $USER:$PASS https://$CLOUDURL/$SHARES_ENDPOINT)
url=$(echo "$xml" | grep -e 'url' | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
echo "$url"
@chrisonntag
Copy link
Author

transfer

Download the file into your user binary directory and make sure that its executable with chmod +x transfer.
Open the file and update

CLOUDURL="<cloud.example.com>"
USER="<admin>"
PASS="<mypassword>"

with your Nextcloud base URL and your own credentials.

@chrisonntag
Copy link
Author

Remember that you may have to create an app token in your Nextcloud security settings!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment