up 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. ##### This is my (demuredemeanor) personal pastebin upload script.
  3. # Based on https://causal.agency/bin/up.html
  4. #
  5. # Content can be piped in, a file can be specified, and a few flags can be used.
  6. ## TODO: Terminal output option https://github.com/buildkite/terminal-to-html
  7. ## TODO: tmux to html? (tmux2html?)
  8. ## TODO: Consider adding png compression?
  9. set -eu
  10. readonly BaseURL='http://up.demu.red/pb/'
  11. readonly WebDir='/usr/share/nginx/up/pb/'
  12. readonly sshHost='demu.red'
  13. readonly Port='500'
  14. upload() {
  15. local src ext ft ts rand Path
  16. src=$1
  17. ext=${src##*.}
  18. ## Change plain files to txt for better browser viewing
  19. ft=$(file -b --mime-type ${src})
  20. if [ "${ft}" = "text/plain" ]; then
  21. ext="txt"
  22. fi
  23. ts=$(date "+%Y-%m-%d")
  24. rand=$(openssl rand -hex 4)
  25. Path=$(printf '%s_%s.%s' "${ts}" "${rand}" "${ext}")
  26. scp -q -P ${Port} "${src}" "${sshHost}:${WebDir}${Path}"
  27. ssh -p ${Port} ${sshHost} chmod a+r "${WebDir}${Path}"
  28. echo "${BaseURL}${Path}"
  29. }
  30. temp() {
  31. temp=$(mktemp -d)
  32. trap "rm -r '${temp}'" EXIT
  33. }
  34. uploadText() {
  35. temp
  36. cat > "${temp}/input.txt"
  37. upload "${temp}/input.txt"
  38. }
  39. uploadCommand() {
  40. temp
  41. echo "$ $*" > "${temp}/exec.txt"
  42. "$@" >> "${temp}/exec.txt" 2>&1 || true
  43. upload "${temp}/exec.txt"
  44. }
  45. uploadHighlight() {
  46. temp
  47. highlight -i "$@" -o "${temp}/hi.html" -lIas nightshimmer
  48. upload "${temp}/hi.html"
  49. }
  50. uploadScreen() {
  51. temp
  52. scrot -m "${temp}/capture.png"
  53. upload "${temp}/capture.png"
  54. }
  55. #uploadTerminal() {
  56. #temp
  57. #cat > "${temp}/term.html" <<-EOF
  58. #<!DOCTYPE html>
  59. #<title>${1}</title>
  60. #<style>
  61. #$(scheme -s)
  62. #</style>
  63. #EOF
  64. #ptee "$@" | shotty -Bs >> "${temp}/term.html"
  65. #upload "${temp}/term.html"
  66. #}
  67. uploadTermSVG() {
  68. echo "run svg" ## DEBUG
  69. temp
  70. termtosvg -D 5000 ${temp}/term.svg
  71. upload ${temp}/term.svg
  72. }
  73. ## Make sure to add new options to the getopts...
  74. while getopts 'chstv' opt; do
  75. case "${opt}" in
  76. (c) fn=uploadCommand;;
  77. (h) fn=uploadHighlight;;
  78. (s) fn=uploadScreen;;
  79. (v) fn=uploadTermSVG;;
  80. #(t) fn=uploadTerminal;;
  81. (?) exit 1;;
  82. esac
  83. done
  84. shift $((OPTIND - 1))
  85. [ $# -eq 0 ] && : ${fn:=uploadText}
  86. : ${fn:=upload}
  87. url=$($fn "$@")
  88. printf '%s' "${url}" | xclip -selection c 2> /dev/null || true
  89. echo " ${url}"