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


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  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. function delete_file(){
  28. # Delete a file already stored in cjb server.
  29. # First argument is the remote's file name.
  30. FILE="$1"
  31. echo "Deleting remote file \"$1\"..."
  32. curl_request -F "delete=$FILE" > /dev/null
  33. }
  34.  
  35. function update_file(){
  36. # Update a file already stored in cjb server,
  37. # by deleting it, and then upload a new local copy.
  38. echo "Updating remote file \"$1\" with a local copy..."
  39. FILE="$1"
  40. delete_file "$FILE"
  41. upload_file "$FILE"
  42. }
  43.  
  44. function upload_file(){
  45. # Upload a file to cjb server.
  46. # First argument must be the local file name.
  47. FILE="$1"
  48. # Check if file exists...
  49. if [ -f "$FILE" ]; then
  50. echo "Uploading file \"$FILE\"..."
  51. curl_request -F "[email protected]$FILE" > /dev/null
  52. echo "File is: http://users.cjb.net/$USERNAME/$FILE"
  53. else
  54. echo "Local file \"$FILE\" does not exist! (does it?)"
  55. return 1
  56. fi
  57. }
  58.  
  59. function show_help(){
  60. show_usage
  61. echo " Options:"
  62. echo " -d Delete a remote file in cjb server. "
  63. echo " Example: $CODENAME -d picture1.png "
  64. echo ""
  65. echo " -u Upload a local file to cjb server. "
  66. echo " Example: $CODENAME -u local_file.pdf "
  67. echo ""
  68. echo " -U Update a file already stored in cjb server, "
  69. echo " by deleting it, then uploading a new copy. "
  70. echo ""
  71. echo " -h Show this help."
  72. echo ""
  73. }
  74.  
  75. function show_usage(){
  76. echo " $CODENAME, v$VERSION, $AUTHOR"
  77. echo " Usage: $CODENAME <OPTIONS> [FILE]"
  78. echo " $CODENAME -h for show more options."
  79. echo ""
  80. }
  81.  
  82. function main(){
  83. ARGV="$1"
  84. FILE="$2"
  85.  
  86. case $ARGV in
  87. "-d")
  88. delete_file "$FILE"
  89. ;;
  90.  
  91. "-h" | "--help")
  92. show_help
  93. ;;
  94.  
  95. "-u")
  96. upload_file "$FILE"
  97. ;;
  98.  
  99. "-U")
  100. update_file "$FILE"
  101. ;;
  102.  
  103. *)
  104. show_usage
  105. ;;
  106. esac
  107. }
  108.  
  109. # Start script...
  110. if [ -z $1 ]; then
  111. show_usage
  112. exit 1
  113. else
  114. main "$1" "$2"
  115. fi

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.