Advertisement
Guest User

fileOMUploader.sh

a guest
Jul 4th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.71 KB | None | 0 0
  1. #!/bin/bash
  2. # Bash Uploader For FileOM
  3. # fileOMUploader.sh
  4. version="2.014.07.01";
  5. # Dependency: cURL | Ubuntu 12.04LTS or 14.04LTS: apt-get install libcurl3 -y;
  6. # Scope: Uploads all files in a directory to an account on FileOM.com
  7. ####################################################################################################
  8. #                                      CONFIGURATION SETTINGS                                      #
  9. ####################################################################################################
  10. # Configure the following with your credentials between the " " marks.
  11. user="";
  12. pass="";
  13. # Insert the following line (without the # symbol) into the alias section of your ~/.bashrc file:
  14. # alias fileom='/path/to/your/directory/with/fileOMUploader.sh'
  15. ####################################################################################################
  16. #                                              SCRIPT                                              #
  17. ####################################################################################################
  18. yellow=$(tput setaf 3); green=$(tput setaf 2); red=$(tput setaf 1); cyan=$(tput setaf 6); textreset=$(tput sgr0); STARTTIME=$(date +%s);
  19. echo $cyan"############################################"${textreset};
  20. echo $cyan"# Bash File Upload For FileOM v"$version" #"${textreset};
  21. echo $cyan"############################################"${textreset};
  22. ####################################################################################################
  23. # Check if script is already running in directory or failed to exit gracefully on last run
  24. if [[ -f .fileOMInfo.swp ]]; then
  25.   echo $red"FAILURE! "${textreset}$cyan"Script is either currently running or did not exit gracefully on last run."${textreset};
  26.   read -r -p $cyan"Would you like to clear the FileOMUploader.sh temp files and try again? (y/n):"${textreset} continue;
  27.   continue=${continue,,}
  28.   if [[ $continue =~ ^(yes|y| ) ]]; then
  29.     clear;
  30.     echo $cyan"Removing .fileOMSessionURL.swp & .fileOMInfo.swp"${textreset};
  31.     rm .fileOMSessionURL.swp .fileOMInfo.swp 2>/dev/null;
  32.     echo $green"SUCCESS! "${textreset}$cyan"All FileOMUploader.sh temp files have been purged, resuming script now..."${textreset};
  33.     echo $green"**************************************************************************************************************"${textreset};
  34.   else
  35.     exit 0;
  36.   fi
  37. fi
  38. if [[ -f FileOMLinks.txt ]]; then
  39.   echo $yellow"WARNING! "${textreset}$cyan"The file FileOMLinks.txt was found in this directory which indicates that this script has already"${textreset};
  40.   echo $cyan"been run in this directory before."${textreset};
  41.   echo $cyan"Would you like to:"${testreset};
  42.   echo $cyan" i - Ignore this warning (Script will append existing FileOMLinks.txt with any new links it creates."${textreset};
  43.   echo $cyan" d - Delete the existing FileOMLinks.txt file and continue to run this script."${textreset};
  44.   echo $cyan" q - Quit this script altogether."${textreset};
  45.   read -r -p $yellow"Select (i/d/q):"${textreset} userSelection;
  46.   userSelection=${userSelection,,}
  47.   if [[ $userSelection =~ ^(i| ) ]]; then
  48.     echo $cyan"**************************************************************************************************************"${textreset};
  49.   fi
  50.   if [[ $userSelection =~ ^(d) ]]; then
  51.     echo $cyan"Removing FileOMLinks.txt"${textreset};
  52.     rm FileOMLinks.txt 2>/dev/null;
  53.     echo $cyan"**************************************************************************************************************"${textreset};
  54.   fi
  55.   if [[ $userSelection =~ ^(q) ]]; then
  56.     exit 0;
  57.   fi
  58. fi
  59.  
  60. # Generate the session and upload URL
  61. rm .fileOMSessionURL.swp 2>/dev/null;
  62. curl --connect-timeout 3 --silent --data "login=$user&password=$pass&op=login" "http://fileom.com/cgi-bin/cAPI.cgi" > .fileOMSessionURL.swp
  63. uploadSessionCheck=`cat .fileOMSessionURL.swp` 2>/dev/null;
  64. uploadSessionCheckLength="${#uploadSessionCheck}";
  65. if [[ $uploadSessionCheckLength -eq "121" ]]; then
  66.   echo $green"SUCCESS! "${textreset}$cyan"Upload session has been created!"${textreset};
  67. fi
  68. if [[ $uploadSessionCheckLength -ne "121" ]]; then
  69.   while [[ $uploadSessionCheckLength -ne "121" ]]; do
  70.     rm .fileOMSessionURL.swp 2>/dev/null;
  71.     curl --silent --data "login=$user&password=$pass&op=login" "http://fileom.com/cgi-bin/cAPI.cgi" > .fileOMSessionURL.swp
  72.     uploadSessionCheck=`cat .fileOMSessionURL.swp` 2>/dev/null; uploadSessionCheckLength=`${#uploadSessionCheck};`;
  73.     if [[ $uploadSessionCheckLength -eq "121" ]]; then
  74.       echo $green"SUCCESS! "${textreset}$cyan"Upload session has been created! Uploading files now."${textreset};
  75.       echo $cyan"***************************************************************************************************************"${textreset};
  76.     fi
  77.     if [[ $uploadSessionCheckLength -ne "121" ]]; then
  78.       echo $red"FAILURE! "${textreset}$cyan"Upload session failed to be created, re-attempting now."${textreset};
  79.       echo $cyan"***************************************************************************************************************"${textreset};
  80.       sleep 10s;
  81.     fi
  82.   done;
  83. fi
  84.  
  85. #Uploads the files
  86. echo $cyan"Upload process for all files in current directory is starting now..."${textreset};
  87. echo $green"**************************************************************************************************************"${textreset};
  88. for f in ./*; do
  89.   file=`echo $f | sed 's/^\.\///;'`;
  90.   fileSize=`ls -lh $file | awk {'print $5'}`;
  91.  
  92.   # Upload the file
  93.   uploadSuccessCheck=0;
  94.   while [[ $uploadSuccessCheck -ne 1 ]]; do
  95.     fileExtension=`echo ${file: -4} | sed 's/.*[\.]//;'`;
  96.     if [[ $fileExtension == "txt" ]] || [[ $fileExtension == "png" ]] || [[ $fileExtension == "jpeg" ]] || [[ $fileExtension == "jpg" ]] || [[ $fileExtension == "sh" ]]; then
  97.       echo $yellow"SKIPPED! "${textreset}$cyan"The following file has been skipped as the file's extension is either .txt, .png, .jpg, .jpeg, or .sh:"${textreset};
  98.       echo $cyan"$file"${textreset};
  99.       echo $cyan"***************************************************************************************************************"${textreset};
  100.       uploadSuccessCheck=1;
  101.     fi
  102.     if [[ $fileExtension != "txt" ]] && [[ $fileExtension != "png" ]] && [[ $fileExtension != "jpeg" ]] && [[ $fileExtension != "jpg" ]] && [[ $fileExtension != "sh" ]]; then
  103.       sessionID=`cat .fileOMSessionURL.swp | sed 's/{\"sess_id\"\:\"//;s/\".*//'`;
  104.       url=`cat .fileOMSessionURL.swp | sed 's/.*\"upload_to_url\":\"//;s/\".*//;'`;
  105.       rm .fileOMInfo.swp 2>/dev/null;
  106.       while [[ $uploadSuccessCheck != "1" ]] && [[ ! -f .fileOMInfo.swp ]]; do
  107.         curl -s --form file=@$file --form utype=prem --form sess_id=$sessionID $url > .fileOMInfo.swp;
  108.         sessionSanityCheck=`cat .fileOMInfo.swp | wc -l`;
  109.         uploadSuccessCheck=1;
  110.         if grep -Fxq "[B]FileOM: [/B][URL=]$file - $fileSizeB[/URL]" .fileOMInfo.swp; then
  111.           echo $red"FAILURE! "${textreset}$cyan$file" has failed to upload, re-attempting..."${textreset};
  112.           rm .fileOMInfo.swp 2>/dev/null;
  113.           uploadSuccessCheck=0;
  114.         fi
  115.         if grep "\&st\=filesize too big$" .fileOMInfo.swp; then
  116.           echo $red"FAILURE! "${textreset}$yellow$file${textreset}$cyan" failed to upload due to it's size..."${textreset};
  117.           rm .fileOMInfo.swp 2>/dev/null;
  118.           uploadSuccessCheck=0;
  119.         fi
  120.         if grep "<h1>Forbidden</h1> <p>" .fileOMInfo.swp; then
  121.           echo $red"FAILURE! "${textreset}$yellow$file${textreset}$cyan" FileOM API Server is currently not functional, pausing for 1 min before re-attempting."${textreset};
  122.           rm .fileOMInfo.swp 2>/dev/null;
  123.           uploadSuccessCheck=0;
  124.           sleep 1m;
  125.         fi
  126.         if grep "504 Gateway Time-out" .fileOMInfo.swp; then
  127.           echo $red"FAILURE! "${textreset}$yellow$file${textreset}$cyan" FileOM API Server is currently not functional, pausing for 1 min before re-attempting."${textreset};
  128.           rm .fileOMInfo.swp 2>/dev/null;
  129.           uploadSuccessCheck=0;
  130.           sleep 1m;
  131.         fi
  132.         if grep "please send mail to the webmaster" .fileOMInfo.swp; then
  133.           echo $red"FAILURE! "${textreset}$yellow$file${textreset}$cyan" FileOM API Server is currently not functional, pausing for 1 min before re-attempting."${textreset};
  134.           rm .fileOMInfo.swp 2>/dev/null;
  135.           uploadSuccessCheck=0;
  136.           sleep 1m;
  137.         fi
  138.         if grep "Software error" .fileOMInfo.swp; then
  139.           echo $red"FAILURE! "${textreset}$yellow$file${textreset}$cyan" FileOM API Server is currently not functional, pausing for 1 min before re-attempting."${textreset};
  140.           rm .fileOMInfo.swp 2>/dev/null;
  141.           uploadSuccessCheck=0;
  142.           sleep 1m;
  143.         fi
  144.         if grep "No file found" .fileOMInfo.swp; then
  145.           echo $red"FAILURE! "${textreset}$yellow$file${textreset}$cyan" FileOM API Server is currently not functional, pausing for 1 min before re-attempting."${textreset};
  146.           rm .fileOMInfo.swp 2>/dev/null;
  147.           uploadSuccessCheck=0;
  148.           sleep 1m;
  149.         fi
  150.         downloadURL=`cat .fileOMInfo.swp | xargs -n1 -i curl -s "{}" | sed 's/.*download_link\":\"//;s/\"\}//;'`;
  151.         downloadURLLength="${#downloadURL}";
  152.         if [[ $downloadURLLength == "0" ]]; then
  153.           uploadSuccessCheck=0;
  154.         fi
  155.       done;
  156.       if [[ $downloadURLength != "0" ]] && [[ $uploadSuccessCheck != "0" ]]; then
  157.         uploadedFileSize=`ls -lh $file | awk {'print $5'}`;
  158.         echo $green"SUCCESS! "${textreset}$cyan$file" has been successfully uploaded:"${textreset};
  159.         echo $cyan$downloadURL ${textreset};
  160.         echo $cyan"***************************************************************************************************************"${textreset};
  161.         echo "[B]FileOM: [/B][URL="$downloadURL"]"$file" - "$fileSize"B[/URL]" >> FileOMLinks.txt;
  162.         uploadSuccessCheck=1;
  163.       fi
  164.       fi
  165.   done;
  166. done;
  167. rm .fileOMInfo.swp .fileOMSessionURL.swp 2>/dev/null;
  168. ####################################################################################################
  169. # Displays the [BBCode] links in the terminal shell
  170. if [[ -f FileOMLinks.txt ]];
  171.   then cat FileOMLinks.txt;
  172. fi
  173. #Counts the number of links generated and display with number of files in current directory.
  174. numFiles=`ls ./* | grep -v \.txt$ | grep -v \.png$ | grep -v \.jpg$ | grep -v \.jpeg$ | grep -v \.sh$ | wc -l`;
  175. if [[ -f FileOMLinks.txt ]]; then
  176.   numLinks=`cat ./FileOMLinks.txt | wc -l`; ENDTIME=$(date +%s);
  177. fi
  178. if [[ ! -f FileOMLinks.txt ]]; then
  179.   numLinks=0;
  180. fi;
  181. echo "Number of files in this directory: "$numFiles;
  182. echo "Total # FileOM.com URLs generated: "$numLinks;
  183. echo "Number of seconds this script ran for: "$(($ENDTIME - $STARTTIME));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement