Advertisement
v1ral_ITS

easy shareable upload link to any file

Aug 5th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.50 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Author: v1ral
  3. configuredDownloadClient=""
  4. configuredUploadClient=""
  5. configuredClient=""
  6. currentVersion="1.22.0"
  7. down="false"
  8.  
  9. ## This function determines which http get tool the system has installed and returns an error if there isnt one
  10. getConfiguredDownloadClient()
  11. {
  12.   if  command -v curl &>/dev/null; then
  13.     configuredDownloadClient="curl"
  14.   elif command -v wget &>/dev/null; then
  15.     configuredDownloadClient="wget"
  16.   elif command -v fetch &>/dev/null; then
  17.     configuredDownloadClient="fetch"
  18.   else
  19.     echo "Error: Downloading with this tool reqires either curl, wget, or fetch to be installed." >&2
  20.     return 1
  21.   fi
  22. }
  23.  
  24. ## Allows to call the users configured client without if statements everywhere
  25. httpGet()
  26. {
  27.   case "$configuredClient" in
  28.     curl)  curl -A curl -s "$@" ;;
  29.     wget)  wget -qO- "$@" ;;
  30.     httpie) http -b GET "$@" ;;
  31.     fetch) fetch -q "$@" ;;
  32.   esac
  33. }
  34.  
  35. ## This function determines which http get tool the system has installed and returns an error if there isnt one
  36. getConfiguredClient()
  37. {
  38.   if  command -v curl &>/dev/null; then
  39.     configuredClient="curl"
  40.   elif command -v wget &>/dev/null; then
  41.     configuredClient="wget"
  42.   elif command -v http &>/dev/null; then
  43.     configuredClient="httpie"
  44.   elif command -v fetch &>/dev/null; then
  45.     configuredClient="fetch"
  46.   else
  47.     echo "Error: This tool reqires either curl, wget, httpie or fetch to be installed." >&2
  48.     return 1
  49.   fi
  50. }
  51.  
  52. ## This function determines which http get tool the system has installed and returns an error if there isnt one
  53. getconfiguredUploadClient()
  54. {
  55.   if  command -v curl &>/dev/null; then
  56.     configuredUploadClient="curl"
  57.   elif command -v wget &>/dev/null; then
  58.     configuredUploadClient="wget"
  59.   else
  60.     echo "Error: Uploading with this tool reqires either curl or wget to be installed." >&2
  61.     return 1
  62.   fi
  63. }
  64. ## Allows to call the users configured client without if statements everywhere
  65. httpDownload()
  66. {
  67.   case "$configuredDownloadClient" in
  68.     curl)  curl -A curl --progress -o "$tempOutputPath/$3" "https://transfer.sh/$2/$3" || { echo "Failure!"; return 1;};;
  69.     wget)  wget --progress=dot -O "$tempOutputPath/$3" "https://transfer.sh/$2/$3" || { echo "Failure!"; return 1;} ;;
  70.     fetch) fetch -q -o "$tempOutputPath/$3" "https://transfer.sh/$2/$3" || { echo "Failure!"; return 1;};;
  71.   esac
  72. }
  73.  
  74. update()
  75. {
  76.   # Author: Alexander Epstein https://github.com/alexanderepstein
  77.   # Update utility version 2.2.0
  78.   # To test the tool enter in the defualt values that are in the examples for each variable
  79.   repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite
  80.   githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein
  81.   nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one
  82.   latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option
  83.  
  84.   if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then
  85.     echo "Error: update utility has not been configured correctly." >&2
  86.     exit 1
  87.   elif [[ $latestVersion == "" ]]; then
  88.     echo "Error: no active internet connection" >&2
  89.     exit 1
  90.   else
  91.     if [[ "$latestVersion" != "$currentVersion" ]]; then
  92.       echo "Version $latestVersion available"
  93.       echo -n "Do you wish to update $repositoryName [Y/n]: "
  94.       read -r answer
  95.       if [[ "$answer" == [Yy] ]]; then
  96.         cd ~ || { echo 'Update Failed'; exit 1; }
  97.         if [[ -d  ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi
  98.         echo -n "Downloading latest version of: $repositoryName."
  99.         git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } &
  100.         while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done
  101.         rm -f .BSnippetsHiddenFile
  102.         echo "Success!"
  103.         cd $repositoryName || { echo 'Update Failed'; exit 1; }
  104.         git checkout "v$latestVersion" 2> /dev/null || git checkout "$latestVersion" 2> /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit."
  105.         chmod a+x install.sh #this might be necessary in your case but wasnt in mine.
  106.         ./$nameOfInstallFile "update" || exit 1
  107.         cd ..
  108.         rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; }
  109.       else
  110.         exit 1
  111.       fi
  112.     else
  113.       echo "$repositoryName is already the latest version"
  114.     fi
  115.   fi
  116. }
  117.  
  118. checkInternet()
  119. {
  120.   httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request
  121. }
  122.  
  123. singleDownload()
  124. {
  125.   if [[ ! -d $1 ]];then { echo "Directory doesn't exist, creating it now..."; mkdir -p $1;};fi
  126.   tempOutputPath=$1
  127.   if [ -f "$tempOutputPath/$3" ];then
  128.     echo -n "File aleady exists at $tempOutputPath/$3, do you want to delete it? [Y/n] "
  129.     read -r answer
  130.     if [[ "$answer" == [Yy] ]] ;then
  131.       rm -f $tempOutputPath/$3
  132.     else
  133.       echo "Stopping download"
  134.       return 1
  135.     fi
  136.   fi
  137.   echo "Downloading $3"
  138.   httpDownload "$tempOutputPath" "$2" "$3"
  139.   echo "Success!"
  140. }
  141.  
  142. httpSingleUpload()
  143. {
  144.   case "$configuredUploadClient" in
  145.     curl) response=$(curl -A curl --progress --upload-file "$1" "https://transfer.sh/$2") || { echo "Failure!"; return 1;};;
  146.     wget) response=$(wget --progress=dot --method PUT --body-file="$1" "https://transfer.sh/$2") || { echo "Failure!"; return 1;} ;;
  147.   esac
  148.   echo  "Success!"
  149. }
  150.  
  151. printUploadResponse()
  152. {
  153. fileID=$(echo $response | cut -d "/" -f 4)
  154.   cat <<EOF
  155. Transfer Download Command: transfer -d desiredOutputDirectory $fileID $tempFileName
  156. Transfer File URL: $response
  157. EOF
  158. }
  159.  
  160. printOntimeUpload()
  161. {
  162.   cat <<EOF
  163.   Download link: $downlink
  164. EOF
  165. }
  166.  
  167. singleUpload()
  168. {
  169.   filePath=$(echo $1 | sed s:"~":$HOME:g)
  170.   if [ ! -f $filePath ];then { echo "Error: invalid file path"; return 1;}; fi
  171.   tempFileName=$(echo $1 | sed "s/.*\///")
  172.   echo "Uploading $tempFileName"
  173.   httpSingleUpload "$filePath" "$tempFileName"
  174. }
  175.  
  176. onetimeUpload()
  177. {
  178.   response=$(curl -A curl -s -F "file=@$1" http://ki.tc/file/u/)
  179.   downlink=$(echo $response | python -c "import sys, json; print json.load(sys.stdin)['file']['download_page']")
  180. }
  181.  
  182. usage()
  183. {
  184.   cat <<EOF
  185. Transfer
  186. Description: Quickly transfer files from the command line.
  187. Usage: transfer [flags] or transfer [flag] [args] or transfer [filePathToUpload]
  188.   -d  Download a single file
  189.       First arg: Output file directory
  190.       Second arg: File url id
  191.       Third arg: File name
  192.   -o  Onetime file upload
  193.   -u  Update Bash-Snippet Tools
  194.   -h  Show the help
  195.   -v  Get the tool version
  196. Examples:
  197.   transfer ~/fileToTransfer.txt
  198.   transfer ~/firstFileToTransfer.txt ~/secondFileToTransfer.txt ~/thirdFileToTransfer.txt
  199.   transfer -d ~/outputDirectory fileID fileName
  200.   transfer -o ~/fileToTransfer.txt
  201. EOF
  202. }
  203.  
  204. while getopts "o:d:uvh" opt; do
  205.   case "$opt" in
  206.     \?) echo "Invalid option: -$OPTARG" >&2
  207.       exit 1
  208.     ;;
  209.     h)  usage
  210.       exit 0
  211.     ;;
  212.     v)  echo "Version $currentVersion"
  213.       exit 0
  214.     ;;
  215.     u)
  216.       getConfiguredClient || exit 1
  217.       checkInternet || exit 1
  218.       update || exit 1
  219.       exit 0
  220.     ;;
  221.     o)
  222.       onetime="true"
  223.     ;;
  224.     d)
  225.       down="true"
  226.       if [ $# -lt 4 ];then { echo "Error: not enough arguments for downloading a file, see the usage"; return 1;};fi
  227.       if [ $# -gt 4 ];then { echo "Error: to many enough arguments for downloading a file, see the usage"; return 1;};fi
  228.       inputFilePath=$(echo "$*" | sed s/-d//g | sed s/-o//g | cut -d " " -f 2)
  229.       inputID=$(echo "$*" | sed s/-d//g | sed s/-o//g | cut -d " " -f 3)
  230.       inputFileName=$(echo "$*" | sed s/-d//g | sed s/-o//g | cut -d " " -f 4)
  231.     ;;
  232.     :)  echo "Option -$OPTARG requires an argument." >&2
  233.       exit 1
  234.     ;;
  235.   esac
  236. done
  237.  
  238. if [[ $# == "0" ]]; then
  239.   usage
  240.   exit 0
  241. elif [[ $# == "1" ]];then
  242.   if [[ $1 == "help" ]]; then
  243.     usage
  244.     exit 0
  245.   elif [[ $1 == "update" ]]; then
  246.     getConfiguredClient || exit 1
  247.     checkInternet || exit 1
  248.     update || exit 1
  249.     exit 0
  250.   elif [ -f $1 ];then
  251.     getConfiguredClient || exit 1
  252.     checkInternet || exit 1
  253.     getconfiguredUploadClient || exit 1
  254.     singleUpload "$1" || exit 1
  255.     printUploadResponse
  256.     exit 0
  257.   else
  258.     echo "Error: invalid filepath"
  259.     exit 1
  260.   fi
  261. else
  262.   if $down && ! $onetime ;then
  263.     getConfiguredClient || exit 1
  264.     checkInternet || exit 1
  265.     getConfiguredDownloadClient || exit 1
  266.     singleDownload "$inputFilePath" "$inputID" "$inputFileName" || exit 1
  267.     exit 0
  268.   elif ! $down && ! $onetime; then
  269.     getConfiguredClient || exit 1
  270.     checkInternet || exit 1
  271.     getconfiguredUploadClient || exit 1
  272.     for path in "$@";do
  273.       singleUpload "$path" || exit 1
  274.       printUploadResponse
  275.       echo
  276.     done
  277.     exit 0
  278.   elif ! $down && $onetime; then
  279.     getConfiguredClient || exit 1
  280.     if [[ $configuredClient -ne "curl" ]];then
  281.       echo "Error: curl must be installed to use one time file upload"
  282.       exit 1
  283.     fi
  284.     inputFileName=$(echo "$*" | sed s/-o//g | cut -d " " -f 2 )
  285.     onetimeUpload "$inputFileName"
  286.     printOntimeUpload
  287.   fi
  288. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement