From f9883a8a76f4bf1b662d81f0467b97452be57b71 Mon Sep 17 00:00:00 2001
From: James Tomasino <james@tomasino.org>
Date: Tue, 27 Oct 2020 01:48:46 +0000
Subject: [PATCH] adds filetype override for standard input binary files

---
 README.md |  9 ++++++++-
 pb        | 53 ++++++++++++++++++++++++++++++++++++-----------------
 pb.1      |  9 ++++++++-
 3 files changed, 52 insertions(+), 19 deletions(-)

diff --git a/README.md b/README.md
index 533eac3..51c4649 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-pb ![calver](https://img.shields.io/badge/calver-2020.09.05-22bfda.svg?style=flat-square) [![Build Status](https://drone.tildegit.org/api/badges/tomasino/pb/status.svg)](https://drone.tildegit.org/tomasino/pb) ![license](https://img.shields.io/badge/license-GPL3-blue.svg?style=flat-square)
+pb ![calver](https://img.shields.io/badge/calver-2020.10.27-22bfda.svg?style=flat-square) [![Build Status](https://drone.tildegit.org/api/badges/tomasino/pb/status.svg)](https://drone.tildegit.org/tomasino/pb) ![license](https://img.shields.io/badge/license-GPL3-blue.svg?style=flat-square)
 ------
 
 **pb** is a helper utility for using 0x0 pastebin services
@@ -39,6 +39,12 @@ Shorten a URL
 pb -u https://google.com
 ```
 
+Re-upload an image from the web
+
+```bash
+curl -s https://tildegit.org/_/static/img/gitea-lg.png | pb -e "png"
+```
+
 ### Options
 
 ```bash
@@ -48,6 +54,7 @@ pb -u https://google.com
   -c                        Pretty color output
   -u                        Shorten URL
   -s server_address         Use alternative pastebin server address
+  -e bin_extension          Specify a binary file extension used in the upload
 ```
 
 ### Install
diff --git a/pb b/pb
index af225f6..e968331 100755
--- a/pb
+++ b/pb
@@ -1,15 +1,17 @@
 #!/bin/sh
 
 # init variables
-version="v2020.07.05"
+version="v2020.10.27"
 ENDPOINT="https://ttm.sh"
-flag_options=":hvcufs::"
+flag_options=":hvcufe:s::"
 flag_version=0
 flag_help=0
 flag_file=0
 flag_url=0
 flag_colors=0
+flag_ext=0
 data=""
+EXT=""
 
 # help message available via func
 show_help() {
@@ -27,6 +29,7 @@ OPTIONAL FLAGS:
   -c                        Pretty color output
   -u                        Shorten URL
   -s server_address         Use alternative pastebin server address
+  -e bin_extension          Specify a binary file extension used in the upload
 END
 }
 
@@ -55,13 +58,6 @@ die () {
   exit "${code}"
 }
 
-# is not interactive shell, use stdin
-if [ -t 0 ]; then
-  flag_file=1
-else
-  data="$(cat < /dev/stdin )"
-fi
-
 # attempt to parse options or die
 if ! parsed=$(getopt ${flag_options} "$@"); then
   printf "pb: unknown option\\n"
@@ -85,6 +81,11 @@ while true; do
     -f)
       flag_file=1
       ;;
+    -e)
+      shift
+      flag_ext=1
+      EXT="$1"
+      ;;
     -s)
       shift
       ENDPOINT="$1"
@@ -103,11 +104,6 @@ while true; do
   shift
 done
 
-# if data variable is empty (not a pipe) use params as fallback
-if [ -z "$data" ]; then
-  data="$*"
-fi
-
 # display current version
 if [ ${flag_version} -gt 0 ]; then
   printf "%s\\n" "${version}"
@@ -120,6 +116,24 @@ if [ ${flag_help} -gt 0 ]; then
   die "" 0
 fi
 
+# is not interactive shell, use stdin
+if [ -t 0 ]; then
+  flag_file=1
+else
+  if [ ${flag_ext} -gt 0 ]; then
+    # short-circuit stdin access to ensure binary data is transferred to curl
+    curl -sF"file=@-;filename=null.${EXT}" "${ENDPOINT}" < /dev/stdin
+    exit 0
+  else
+    data="$(cat < /dev/stdin )"
+  fi
+fi
+
+# if data variable is empty (not a pipe) use params as fallback
+if [ -z "$data" ]; then
+  data="$*"
+fi
+
 # Colors
 if [ ${flag_colors} -gt 0 ]; then
   SUCCESS=$(tput setaf 190)
@@ -167,8 +181,13 @@ if [ ${flag_file} -gt 0 ]; then
       fi
       # check if file exists
       if [ -f "${f}" ]; then
-        # send file to endpoint
-        result=$(curl -sF"file=@${f}" "${ENDPOINT}")
+        if [ ${flag_ext} -gt 0 ]; then
+          # send file to endpoint masked with new extension
+          result=$(curl -sF"file=@${f};filename=null.${EXT}" "${ENDPOINT}")
+        else
+          # send file to endpoint
+          result=$(curl -sF"file=@${f}" "${ENDPOINT}")
+        fi
         printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
       else
         # print error message
@@ -189,7 +208,7 @@ else
     printf "%sNo data found for upload. Please try again.%s\\n" "$ERROR" "$RESET"
   else
     # data available
-    # send data to endpoint, print short url
+    # send data to endpoint
     result=$(printf "%s" "${data}" | curl -sF"file=@-;filename=null.txt" "${ENDPOINT}")
     printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
   fi
diff --git a/pb.1 b/pb.1
index be499b3..256e2a9 100644
--- a/pb.1
+++ b/pb.1
@@ -1,4 +1,4 @@
-.TH PB 1 "05 September 2020" "v2020.09.05"
+.TH PB 1 "27 October 2020" "v2020.10.27"
 
 .SH NAME
 pb \- a helper utility for using 0x0 pastebin services
@@ -28,6 +28,13 @@ Explicitly interpret stdin as filename or names.
 .BI -s " server_address"
 Use alternative pastebin server address.
 .TP
+.BI -e " bin_extension"
+Specifes the file extension used in the upload of binary content passed to
+.B pb
+via standard input.
+.I Note:
+this attribute will not work with file uploads or URL shortening.
+.TP
 .B -u
 Shorten a URL.
 .TP