transfer.plugin.zsh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # transfer.sh Easy file sharing from the command line
  2. # transfer Plugin
  3. # Usage Example :
  4. # > transfer file.txt
  5. # > transfer directory/
  6. # Author:
  7. # Remco Verhoef <remco@dutchcoders.io>
  8. # https://gist.github.com/nl5887/a511f172d3fb3cd0e42d
  9. # Modified to use tar command instead of zip
  10. #
  11. curl --version 2>&1 > /dev/null
  12. if [ $? -ne 0 ]; then
  13. echo "Could not find curl."
  14. return 1
  15. fi
  16. transfer() {
  17. # check arguments
  18. if [ $# -eq 0 ];
  19. then
  20. echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
  21. return 1
  22. fi
  23. # get temporarily filename, output is written to this file show progress can be showed
  24. tmpfile=$( mktemp -t transferXXX )
  25. # upload stdin or file
  26. file=$1
  27. if tty -s;
  28. then
  29. basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
  30. if [ ! -e $file ];
  31. then
  32. echo "File $file doesn't exists."
  33. return 1
  34. fi
  35. if [ -d $file ];
  36. then
  37. echo $file
  38. # tar directory and transfer
  39. tarfile=$( mktemp -t transferXXX.tar.gz )
  40. cd $(dirname $file) && tar -czf $tarfile $(basename $file)
  41. curl --progress-bar --upload-file "$tarfile" "https://transfer.sh/$basefile.tar.gz" >> $tmpfile
  42. rm -f $tarfile
  43. else
  44. # transfer file
  45. curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
  46. fi
  47. else
  48. # transfer pipe
  49. curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
  50. fi
  51. # cat output link
  52. cat $tmpfile
  53. # cleanup
  54. rm -f $tmpfile
  55. }