Skip to content

Instantly share code, notes, and snippets.

@tavinus
Last active September 12, 2024 12:57
Show Gist options
  • Save tavinus/93bdbc051728748787dc22a58dfe58d8 to your computer and use it in GitHub Desktop.
Save tavinus/93bdbc051728748787dc22a58dfe58d8 to your computer and use it in GitHub Desktop.
Send files to Nextcloud/Owncloud shared folder using curl
#!/usr/bin/env bash
############################################################
# MIGRATED TO REPOSITORY
# https://github.com/tavinus/cloudsend.sh
#
# This gist will NOT be updated anymore
############################################################
############################################################
#
# cloudsend.sh
#
# Uses curl to send files to a shared
# Nextcloud/Owncloud folder
#
# Usage: ./cloudsend.sh <file> <folderLink>
# Help: ./cloudsend.sh -h
#
# Gustavo Arnosti Neves
# https://github.com/tavinus
#
# Contributors:
# @MG2R @gessel
#
# Get this script to current folder with:
# curl -O 'https://raw.githubusercontent.com/tavinus/cloudsend.sh/master/cloudsend.sh' && chmod +x cloudsend.sh
#
############################################################
CS_VERSION="0.1.6"
CLOUDURL=""
FOLDERTOKEN=""
PUBSUFFIX="public.php/webdav"
HEADER='X-Requested-With: XMLHttpRequest'
INSECURE=''
# https://cloud.mydomain.net/s/fLDzToZF4MLvG28
# curl -k -T myFile.ext -u "fLDzToZF4MLvG28:" -H 'X-Requested-With: XMLHttpRequest' https://cloud.mydomain.net/public.php/webdav/myFile.ext
log() {
[ "$VERBOSE" == " -s" ] || printf "%s\n" "$1"
}
printVersion() {
printf "%s\n" "CloudSender v$CS_VERSION"
}
initError() {
printVersion >&2
printf "%s\n" "Init Error! $1" >&2
printf "%s\n" "Try: $0 --help" >&2
exit 1
}
usage() {
printVersion
printf "\n%s%s\n" "Parameters:" "
-h | --help Print this help and exits
-q | --quiet Be quiet
-V | --version Prints version and exits
-k | --insecure Uses curl with -k option (https insecure)
-p | --password Uses env var \$CLOUDSEND_PASSWORD as share password
You can 'export CLOUDSEND_PASSWORD' at your system, or set it at the call.
Please remeber to also call -p to use the password set."
printf "\n%s\n%s\n%s\n" "Use:" " $0 <filepath> <folderLink>" " CLOUDSEND_PASSWORD='MySecretPass' $0 -p <filepath> <folderLink>"
printf "\n%s\n%s\n%s\n" "Example:" " $0 './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'" " CLOUDSEND_PASSWORD='MySecretPass' $0 -p './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'"
}
##########################
# Process parameters
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
fi
if [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
printVersion
exit 0
fi
if [ "$1" = "-q" ] || [ "$1" = "--quiet" ]; then
VERBOSE=" -s"
shift
fi
if [ "$1" = "-k" ] || [ "$1" = "--insecure" ]; then
INSECURE=' -k'
log " > Insecure mode ON"
shift
fi
if [ "$1" = "-p" ] || [ "$1" = "--password" ]; then
PASSWORD=${CLOUDSEND_PASSWORD}
log " > Using password from env"
shift
fi
##########################
# Validate input
FILENAME="$1"
CLOUDURL=''
# if we have index.php in the URL, process accordingly
if [[ $2 == *"index.php"* ]]; then
CLOUDURL="${2%/index.php/s/*}"
else
CLOUDURL="${2%/s/*}"
fi
FOLDERTOKEN="${2##*/s/}"
if [ ! -f "$FILENAME" ]; then
initError "Invalid input file: $FILENAME"
fi
if [ -z "$CLOUDURL" ]; then
initError "Empty URL! Nowhere to send..."
fi
if [ -z "$FOLDERTOKEN" ]; then
initError "Empty Folder Token! 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
##########################
# Extract base filename
BFILENAME=$(/usr/bin/basename $FILENAME)
##########################
# Send file
#echo "$CURLBIN"$INSECURE$VERBOSE -T "$FILENAME" -u "$FOLDERTOKEN":"$PASSWORD" -H "$HEADER" "$CLOUDURL/$PUBSUFFIX/$BFILENAME"
"$CURLBIN"$INSECURE$VERBOSE -T "$FILENAME" -u "$FOLDERTOKEN":"$PASSWORD" -H "$HEADER" "$CLOUDURL/$PUBSUFFIX/$BFILENAME"
@C0rn3j
Copy link

C0rn3j commented Apr 13, 2020

I'm not sure if we're on the same page - copy <source> <destination> is still available with my changes, my point was to not break existing usage, it's just that you can ALSO use parameters for easy use in scripts.

There's no real 'complexity' added, I simply put the Process parameters stage into a case statement (which is cleaner) and added a condition if it encounters an unknown(positional) parameter.

I'd prefer to have my changes merged rather than have to maintain my own fork.

I've updated the URL to a file with the usage() changes already present.

@nerrixde
Copy link

@tavinus You may want to move everything here into a github repo, it's maybe getting too large for a single gist ^^

@tavinus
Copy link
Author

tavinus commented Apr 15, 2020

Migrated to repository

https://github.com/tavinus/cloudsend.sh

This gist will not be updated anymore.

@tavinus
Copy link
Author

tavinus commented May 15, 2020

Rebuild

I have just rebuilt most of the script and added many improvements.
But it is only available at the new repository.
This gist will be kept as is for all eternity.

The only 'downside' is that the old -p option is now -e, because -p now receives the password as a parameter, as in -p <myPass>.

From here to there, this is the current log (Aug 2020)

  • v0.1.7 - Rename
    • Adds option -r / --rename to change the destination (uploaded) file name.
  • v2.0.0 - Rebuild
    • Script rebuild; many internal changes
    • Changes how options and parameters are parsed
    • Added many validation and checking functions
    • Options can now come in any order and after the filename and URL Link
    • Updated and added verbose messages
    • Updated quiet mode
    • Updated --help and readme.md
    • Password parsing has changed and is NOT compatible with prior versions
      • -p now asks for a password as a parameter, as in -p <pass>
      • new option -e uses an envrionment variable password (substitutes the old -p option)
  • v2.1.0 - Pipes and Globs
    • Adds stdin as input option (piped content)
      • uses either - or . as input file name
      • requires -r <filename>
    • Allows to use globbing at the input
      • can send multiple files by using globs
      • requires -g option
      • not compatible with -r <name> option (rename)
    • Adds new checks and parsing
    • Cleaned up unused code
    • Updated readme and --help
  • v2.1.1 - Pipes and Globs (fix)
    • Small fix to file name parsing VS globbing
    • Updated readme globbing link
  • v2.1.2 - Pipes and Globs (readme-help)
    • Better help and readme text and examples.
  • v2.1.3 - Pipes and Globs (fix2)
    • Disables curl globbing when not globbing.

More info on globbing:


CLICK HERE to see how to send an entire folder (and subfolders?) with the new script and find

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