transfer.plugin.zsh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Author:
  2. # Remco Verhoef <remco@dutchcoders.io>
  3. # https://gist.github.com/nl5887/a511f172d3fb3cd0e42d
  4. # Modified to use tar command instead of zip
  5. #
  6. transfer() {
  7. # check arguments
  8. if [[ $# -eq 0 ]]; then
  9. cat <<EOF
  10. Error: no arguments specified.
  11. Usage: transfer [file/folder] [options]
  12. Examples:
  13. transfer /tmp/test.md
  14. transfer /tmp/test.md -ca
  15. cat /tmp/test.md | transfer test.md
  16. cat /tmp/test.md | transfer test.md -ca
  17. Options:
  18. -ca Encrypt file with symmetric cipher and create ASCII armored output
  19. EOF
  20. return 1
  21. fi
  22. if (( ! $+commands[curl] )); then
  23. echo "Error: curl is not installed"
  24. return 1
  25. fi
  26. local tmpfile tarfile item basename
  27. # get temporarily filename, output is written to this file show progress can be showed
  28. tmpfile=$(mktemp -t transferXXX)
  29. # upload stdin or file
  30. item="$1"
  31. # crypt file with symmetric cipher and create ASCII armored output
  32. local crypt=0
  33. if [[ "$2" = -ca ]]; then
  34. crypt=1
  35. if (( ! $+commands[gpg] )); then
  36. echo "Error: gpg is not installed"
  37. return 1
  38. fi
  39. fi
  40. if ! tty -s; then
  41. # transfer from pipe
  42. if (( crypt )); then
  43. gpg -aco - | curl -X PUT --progress-bar -T - "https://transfer.sh/$item" >> $tmpfile
  44. else
  45. curl --progress-bar --upload-file - "https://transfer.sh/$item" >> $tmpfile
  46. fi
  47. else
  48. basename=$(basename "$item" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
  49. if [[ ! -e $item ]]; then
  50. echo "File $item doesn't exist."
  51. return 1
  52. fi
  53. if [[ -d $item ]]; then
  54. # tar directory and transfer
  55. tarfile=$(mktemp -t transferXXX.tar.gz)
  56. cd $(dirname $item) || {
  57. echo "Error: Could not change to directory $(dirname $item)"
  58. return 1
  59. }
  60. tar -czf $tarfile $(basename $item)
  61. if (( crypt )); then
  62. gpg -cao - "$tarfile" | curl --progress-bar -T "-" "https://transfer.sh/$basename.tar.gz.gpg" >> $tmpfile
  63. else
  64. curl --progress-bar --upload-file "$tarfile" "https://transfer.sh/$basename.tar.gz" >> $tmpfile
  65. fi
  66. rm -f $tarfile
  67. else
  68. # transfer file
  69. if (( crypt )); then
  70. gpg -cao - "$item" | curl --progress-bar -T "-" "https://transfer.sh/$basename.gpg" >> $tmpfile
  71. else
  72. curl --progress-bar --upload-file "$item" "https://transfer.sh/$basename" >> $tmpfile
  73. fi
  74. fi
  75. fi
  76. # cat output link
  77. cat $tmpfile
  78. # add newline
  79. echo
  80. # cleanup
  81. rm -f $tmpfile
  82. }