Skip to content

Instantly share code, notes, and snippets.

@kousu
Last active August 3, 2020 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kousu/cff70a82898da410afb305b6251b9641 to your computer and use it in GitHub Desktop.
Save kousu/cff70a82898da410afb305b6251b9641 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# upload a file to neuropoly's wiki's filestore at https://www.neuro.polymtl.ca/lib/exe/mediamanager.php
# public domain
# author: nguenthe@uwaterloo.ca
#
# TODO: be fully scp compatible: we should be able to specify the target filename, disambiguating between DESTs ending in / or not
set -eo pipefail # crash on sub-errors; pipefail is a bashism, but sooo useful
set -u # crash on missing vars
usage() {
echo "NEUROPOLY_USERNAME=user NEUROPOLY_PASSWORD=pass $0 file [file2 file3 ...] dest"
echo
echo "dest is a folder path like datasets/mri"
}
if [ -z "${NEUROPOLY_USERNAME+x}" -o -z "${NEUROPOLY_PASSWORD+x}" ]; then
echo "error: NEUROPOLY_USERNAME and NEUROPOLY_PASSWORD must be defined. These should be credentials for the wiki at https://www.neuro.polymtl.ca/." >&2
exit 1
fi
if [ "$#" -lt 2 ]; then
echo "error: not enough arguments" >&2
usage
exit 1
fi
DEST="${!#}"
set -- "${@:1:$(($#-1))}" # shift the path off https://stackoverflow.com/a/26163980
# at this point: "$@" is @FILES which has at least one element and DEST is the destination
namespace=$(echo "$DEST" | sed 's|/|:|g' | sed 's|:|%3A|g') # translate to namespace form, and urlencode it; TODO: use a more complete urlencoder.
# throw away credentials when we're done with them
trap 'rm -f cookies.txt' EXIT
# log in
# if the password is wrong, -f will make the script quit
curl -f -b cookies.txt -j -c cookies.txt -X POST 'https://www.neuro.polymtl.ca/home?do=login' --data-raw 'u='"${NEUROPOLY_USERNAME}"'&p='"${NEUROPOLY_PASSWORD}"
extract_sectokens() {
# using [pup](https://github.com/ericchiang/pup): `go get https://github.com/ericchiang/pup; export PATH=~/go/bin:$PATH`
#pup 'input[type=hidden][name=sectok] attr{value}'
# or, this is almost equally good and avoids the dependency:
egrep -o '<input type="hidden" name="sectok" value=".*" />' | awk -F " " '{print $4}' | awk -F "=" '{print $2}' | sed 's|"||g'
}
# get the CSRF token
sectok=$(curl -s -b cookies.txt https://www.neuro.polymtl.ca/lib/exe/mediamanager.php | extract_sectokens | head -n 1)
# upload the files
for file in "$@"; do
curl -f -b cookies.txt -X POST 'https://www.neuro.polymtl.ca/lib/exe/ajax.php?call=mediaupload&ow=false''&'"sectok="${sectok}""'&'"ns=${namespace}"'&'"qqfile="$(basename "$file")"" --data-binary @"$file"
# TODO: this returns json, either:
# {"success":true,"link":...,"id":...,"ns":"downloads:sct"}
# {"error": msg, "ns":"downloads:sct"}
# integrate `jq` here to detect these cases and print something reasonable to stdout
echo
done
# Alternately instead of Wiki credentials + `curl`, use {VPN,SSH,sudo} credentials + `scp`:
#echo -n "$VPN_PASS" | sudo openconnect -b -u "$VPN_USER" --authgroup "$VPN_GROUP" --passwd-on-stdin ssl.vpn.polymtl.ca
#scp "$@" neuro@websv21.polymtl.ca:live/data/media/"${DEST}"
#<somehow auto-kill openconnect here>
# Maybe harder to get set up the first time but scp is probably reliable than some sketchy curl commands.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment