Skip to content

Instantly share code, notes, and snippets.

@maxme
Created November 30, 2018 10:46
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 maxme/16a40c96e9618fbb392bbdc83c460b1a to your computer and use it in GitHub Desktop.
Save maxme/16a40c96e9618fbb392bbdc83c460b1a to your computer and use it in GitHub Desktop.
Shell script that upload any kind of file to a web server and expect it to webserve the file at https://example.com/uploads/ - The script is made for OS X, as it copy the url to the pasteboard for later use. Main usage of this script is to quickly upload and share a file (image/video/json/etc.).
#!/bin/sh
# Replace the following by your server configuration
UPLOAD_DIR_BASE=/var/www/example.com/uploads/
URLBASE=https://example.com/uploads/
SSH_SERVER=example.com
RAND=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1`
UPDIR=UPLOAD_DIR_BASE/$RAND
SCP="rsync --chmod=u+rw,g+r,o+r"
function mkremotedir() {
ssh $SSH_SERVER mkdir $UPDIR
if [ $? -eq 1 ]; then
echo "No luck: directory already exists on the server"
exit 1
fi
}
function upload_onefile() {
filepath="$1"
basename="`basename "$filepath"`"
mkremotedir
newname=`echo $basename|sed "s/ /_/g"`
$SCP "$filepath" $SSH_SERVER:$UPDIR/"$newname"
/bin/echo -n $URLBASE/$RAND/$newname | pbcopy -Prefer txt
echo $URLBASE/$RAND/"$newname"
}
function upload_multi() {
filepath=$1
mkremotedir
$SCP $@ $SSH_SERVER:$UPDIR/
/bin/echo -n $URLBASE/$RAND/ | pbcopy -Prefer txt
echo $URLBASE/$RAND/
}
if [ x == $2x ]; then
upload_onefile "$1"
else
upload_multi $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment