Return to Snippet

Revision: 53283
at November 18, 2011 00:59 by ksaver


Initial Code
#!/usr/bin/env bash
#
#  cjb-upload.sh v1.0
#  Upload a file to upload.cjb.net
#  using curl.
#  BSD-Like licence: Do what (tf*) you want
#  with this code, just keep original author.
#  ksaver, ksaver.tk 15.11.11
#  http://users.cjb.net/ksaver/cjb-upload.sh

# Edit this 2 lines to fit your needs:
USERNAME='your_username'
PASSWORD='your_password'

# No further edition required.
CODENAME=`basename "$0"`
VERSION="1.0"
AUTHOR="ksaver (ksaver.tk)"
LOCATION='http://upload.cjb.net'

function curl_request(){
  curl -s -L $LOCATION \
       -F "username=$USERNAME" \
       -F "password=$PASSWORD" \
       "$@"
}

function delete_file(){
  # Delete a file already stored in cjb server.
  # First argument is the remote's file name. 
  FILE="$1"
  echo "Deleting remote file \"$1\"..."
  curl_request -F "delete=$FILE" > /dev/null
}

function update_file(){
  # Update a file already stored in cjb server,
  # by deleting it, and then upload a new local copy.
  echo "Updating remote file \"$1\" with a local copy..."
  FILE="$1"
  delete_file "$FILE"
  upload_file "$FILE"
}

function upload_file(){
  # Upload a file to cjb server.
  # First argument must be the local file name.
  FILE="$1"
  # Check if file exists...
  if [ -f "$FILE" ]; then
    echo "Uploading file \"$FILE\"..."
    curl_request -F "file=@$FILE" > /dev/null
    echo "File is: http://users.cjb.net/$USERNAME/$FILE"
  else
    echo "Local file \"$FILE\" does not exist! (does it?)"
    return 1
  fi
}

function show_help(){
  show_usage
  echo "  Options:"
  echo "  -d  Delete a remote file in cjb server.         "
  echo "      Example:  $CODENAME -d picture1.png         "
  echo ""
  echo "  -u  Upload a local file to cjb server.          "
  echo "      Example:  $CODENAME -u local_file.pdf       "
  echo ""
  echo "  -U  Update a file already stored in cjb server, "
  echo "      by deleting it, then uploading a new copy.  "
  echo ""
  echo "  -h  Show this help."
  echo "" 
}

function show_usage(){
  echo "  $CODENAME, v$VERSION, $AUTHOR"
  echo "  Usage: $CODENAME <OPTIONS> [FILE]"
  echo "         $CODENAME -h  for show more options."
  echo ""
}

function main(){
  ARGV="$1"
  FILE="$2"

  case $ARGV in
   "-d")
        delete_file "$FILE"
        ;;

    "-h" | "--help")
        show_help
        ;;

    "-u")
        upload_file "$FILE"
        ;;

    "-U")
        update_file "$FILE"
        ;;

       *)
        show_usage
        ;;
  esac
}

# Start script...
if [ -z $1 ]; then
  show_usage
  exit 1
else
  main "$1" "$2"
fi

Initial URL
http://users.cjb.net/ksaver/cjb-upload.sh

Initial Description

                                

Initial Title
cjb-upload.sh: Upload a file to upload.cjb.net using curl

Initial Tags
curl

Initial Language
Bash