-
-
Save chrisonntag/b36b1f76aec9043725bc4c002f85da16 to your computer and use it in GitHub Desktop.
Nextcloud Transfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
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
transfer
Download the file into your user binary directory and make sure that its executable with
chmod +x transfer
.Open the file and update
with your Nextcloud base URL and your own credentials.