transfer 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/sh
  2. ## Adapted from https://gist.github.com/nl5887/a511f172d3fb3cd0e42d.
  3. ## Original author:
  4. ## Remco Verhoef <remco@dutchcoders.io>
  5. curl --version 2>&1 > /dev/null
  6. if [ $? -ne 0 ]; then
  7. echo "Could not find curl."
  8. exit 1
  9. fi
  10. if [ $# -eq 0 ]; then
  11. cat<<EOF
  12. No arguments specified.
  13. Usage: transfer FILES
  14. cat FILE | transfer FOO
  15. EOF
  16. exit 1
  17. fi
  18. # Get temporarily filename, output is written to this file so progress can be showed.
  19. tmpfile=$( mktemp -t transferXXX )
  20. # upload stdin or file
  21. if tty -s; then
  22. for file; do
  23. basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
  24. if [ ! -e "$file" ]; then
  25. echo "File $file doesn't exists."
  26. exit 1
  27. fi
  28. if [ -d "$file" ]; then
  29. # zip directory and transfer
  30. zipfile=$( mktemp -t transferXXX.zip )
  31. cd $(dirname $file) && zip -r -q - $(basename $file) > $zipfile
  32. curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" > $tmpfile
  33. rm -f $zipfile
  34. else
  35. # transfer file
  36. curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" > $tmpfile
  37. # cat output link
  38. out="$out
  39. $(cat $tmpfile)"
  40. fi
  41. done
  42. echo "$out"
  43. else
  44. # transfer pipe
  45. curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
  46. # cat output link
  47. cat $tmpfile
  48. echo
  49. fi
  50. # cleanup
  51. rm -f $tmpfile