Posted By

ksaver on 11/18/11


Tagged

curl upload cjb


Versions (?)

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


 / Published in: Bash
 

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

  1. #!/usr/bin/env bash
  2. #
  3. # cjb-upload.sh v1.0
  4. # Upload a file to upload.cjb.net
  5. # using curl.
  6. # BSD-Like licence: Do what (tf*) you want
  7. # with this code, just keep original author.
  8. # ksaver, ksaver.tk 15.11.11
  9. # http://users.cjb.net/ksaver/cjb-upload.sh
  10.  
  11. # Edit this 2 lines to fit your needs:
  12. USERNAME='your_username'
  13. PASSWORD='your_password'
  14.  
  15. # No further edition required.
  16. CODENAME=`basename "$0"`
  17. VERSION="1.0"
  18. AUTHOR="ksaver (ksaver.tk)"
  19. LOCATION='http://upload.cjb.net'
  20.  
  21. function curl_request(){
  22. curl -s -L $LOCATION \
  23. -F "username=$USERNAME" \
  24. -F "password=$PASSWORD" \
  25. "$@"
  26. }
  27.  
  28. function delete_file(){
  29. # Delete a file already stored in cjb server.
  30. # First argument is the remote's file name.
  31. FILE="$1"
  32. echo "Deleting remote file \"$1\"..."
  33. curl_request -F "delete=$FILE" > /dev/null
  34. }
  35.  
  36. function update_file(){
  37. # Update a file already stored in cjb server,
  38. # by deleting it, and then upload a new local copy.
  39. echo "Updating remote file \"$1\" with a local copy..."
  40. FILE="$1"
  41. delete_file "$FILE"
  42. upload_file "$FILE"
  43. }
  44.  
  45. function upload_file(){
  46. # Upload a file to cjb server.
  47. # First argument must be the local file name.
  48. FILE="$1"
  49. # Check if file exists...
  50. if [ -f "$FILE" ]; then
  51. echo "Uploading file \"$FILE\"..."
  52. curl_request -F "file=@$FILE" > /dev/null
  53. echo "File is: http://users.cjb.net/$USERNAME/$FILE"
  54. else
  55. echo "Local file \"$FILE\" does not exist! (does it?)"
  56. return 1
  57. fi
  58. }
  59.  
  60. function show_help(){
  61. show_usage
  62. echo " Options:"
  63. echo " -d Delete a remote file in cjb server. "
  64. echo " Example: $CODENAME -d picture1.png "
  65. echo ""
  66. echo " -u Upload a local file to cjb server. "
  67. echo " Example: $CODENAME -u local_file.pdf "
  68. echo ""
  69. echo " -U Update a file already stored in cjb server, "
  70. echo " by deleting it, then uploading a new copy. "
  71. echo ""
  72. echo " -h Show this help."
  73. echo ""
  74. }
  75.  
  76. function show_usage(){
  77. echo " $CODENAME, v$VERSION, $AUTHOR"
  78. echo " Usage: $CODENAME <OPTIONS> [FILE]"
  79. echo " $CODENAME -h for show more options."
  80. echo ""
  81. }
  82.  
  83. function main(){
  84. ARGV="$1"
  85. FILE="$2"
  86.  
  87. case $ARGV in
  88. "-d")
  89. delete_file "$FILE"
  90. ;;
  91.  
  92. "-h" | "--help")
  93. show_help
  94. ;;
  95.  
  96. "-u")
  97. upload_file "$FILE"
  98. ;;
  99.  
  100. "-U")
  101. update_file "$FILE"
  102. ;;
  103.  
  104. *)
  105. show_usage
  106. ;;
  107. esac
  108. }
  109.  
  110. # Start script...
  111. if [ -z $1 ]; then
  112. show_usage
  113. exit 1
  114. else
  115. main "$1" "$2"
  116. fi

Report this snippet  

You need to login to post a comment.