upload (6210B)
1 #!/bin/sh 2 set -e 3 4 version=1.0 5 6 # Upload target 7 host="adamsgaard.dk" 8 user="ad" 9 pubdir="tmp" 10 nonpubdir="npub" 11 remote="/var/www/domains/adamsgaard.dk" 12 dir="${nonpubdir}" 13 remotedir="${remote}/${dir}" 14 15 show_help() { 16 echo "usage: ${0##*/} [OPTIONS] FILE1 [FILE2...[FILE N]]" 17 echo "will upload each FILE to $user@$host:$remotedir" 18 echo "If no FILE is specified, this program will expect stdin, and FILE1" 19 echo "will be used as the filename during upload." 20 echo "The program prints the upload URL for each file to standard output" 21 echo 22 echo "OPTIONS are one or more of the following:" 23 echo " -b, --border if an image, add a 10% white border to FILES" 24 echo " -c, --clip send upload url to clipboard" 25 echo " -d, --delete delete previously uploaded FILES instead of uploading" 26 echo " --get and --delete can be used together" 27 echo " -g, --get get previously uploaded FILES instead of uploading" 28 echo " -h, --help show this message" 29 echo " -l, --list [QUERY] list previously uploaded files with optional QUERY" 30 echo " search string as filter" 31 echo " -m, --remove-metadata remove all exif metadata" 32 echo " -n, --notify also show diagnostic output with notify" 33 echo " -o, --gopher return gopher URI instead of http" 34 echo " -p, --public operate on public file directory (/${pubdir})" 35 echo " -r, --resize if an image, resize FILES to fit 800x800 pixels" 36 echo " without changing its width-to-height ratio" 37 echo " -s, --sharpen if an image, sharpen all FILES" 38 echo " -v, --verbose show verbose information (e.g. upload progress)" 39 echo " -V, --version show version and license information" 40 echo " -- do not consider any following arguments as options" 41 echo "the optional operations are performed in the order listed above." 42 echo "${0##*/} requires imagemagick and rsync to be installed on the system." 43 } 44 45 show_version() { 46 echo "${0##*/} version $version" 47 echo "Licensed under the ISC License" 48 echo "written by Anders Damsgaard, anders@adamsgaard.dk" 49 echo "https://src.adamsgaard.dk/dotfiles" 50 } 51 52 check_if_image() { 53 if [ -f "$1" ]; then 54 identify "$1" >/dev/null 2>&1 || die "error: $1 is not an image" 55 fi 56 } 57 58 add_sharpening() { 59 check_if_image "$2" 60 convert "$1" \ 61 -unsharp 2x0.5+0.7+0 \ 62 -quality 95 \ 63 "$1" 64 } 65 66 add_border() { 67 check_if_image "$2" 68 convert "$1" \ 69 -bordercolor White -border 10%x10% \ 70 -unsharp 2x0.5+0.7+0 \ 71 -quality 95 \ 72 "$1" 73 } 74 75 add_resize() { 76 check_if_image "$2" 77 convert "$1" \ 78 -resize 800x800\> \ 79 -quality 95 \ 80 "$1" 81 } 82 83 preprocess_file() { 84 [ "$removemetadata" = 1 ] && exiftool -all= "$1" >/dev/null 85 [ "$border" = 1 ] && add_border "$1" "$2" 86 [ "$resize" = 1 ] && add_resize "$1" "$2" 87 [ "$sharpen" = 1 ] && add_sharpening "$1" "$2" 88 printf "" 89 } 90 91 upload_file() { 92 infile="$1" 93 basename="$2" 94 newbasename="$(echo "$basename" | sed 's/ /_/g')" 95 progress="" 96 if [ "$verbose" = 1 ]; then 97 progress="-v --progress" 98 echo "infile: $infile" 99 echo "newbasename: $newbasename" 100 fi 101 rsync -rz $progress --chmod=Fu=rw,Fog=r -e "ssh" "$infile" \ 102 "${user}@${host}:${remotedir}/'$newbasename'" 103 if [ "$gopher" = 1 ]; then 104 if [ "${newbasename##*.}" = "gph" ]; then 105 type=1 106 else 107 mime_type="$(file -ib "$infile")" 108 case "$mime_type" in 109 image/*) 110 type=I;; 111 text/*) 112 type=0;; 113 *shellscript*) 114 type=0;; 115 *html*) 116 type=0;; 117 *) 118 type=9;; 119 esac 120 fi 121 url="gophers://${host}/${type}/${dir}/${newbasename}" 122 else 123 url="https://${host}/${dir}/${newbasename}" 124 fi 125 if [ "$verbose" = 1 ]; then 126 echo 127 msg="Uploaded to $url (copied to clipboard)" 128 printf '%s\n' "$msg" 129 else 130 printf '%s\n' "$url" 131 fi 132 [ "$notify" = 1 ] && notify "$msg" || : 133 [ "$clip" = 1 ] && printf '%s' "$url" | xclip -i || : 134 } 135 136 download_file() { 137 url="https://${host}/${dir}/${1}" 138 if [ -e "$1" ]; then 139 die "error: file '$1' already exists, aboring" 140 fi 141 if command -v hurl >/dev/null 2>&1; then 142 hurl "$url" > ./"$1" 143 elif command -v curl >/dev/null 2>&1; then 144 curl --silent --fail -OL "$url" 145 elif command -v ftp >/dev/null 2>&1; then 146 ftp -M -V -o - "$url" > ./"$1" 147 elif command -v wget >/dev/null 2>&1; then 148 wget "$url" 149 else 150 die 'error: no download program found' 151 fi 152 153 } 154 155 delete_file() { 156 ssh "$user"@"$host" "rm '${remotedir}/$1'" 157 } 158 159 list_uploads() { 160 if [ -z "$1" ]; then 161 ssh "$user"@"$host" "ls -lah '${remotedir}/'" 162 else 163 ssh "$user"@"$host" "ls -lah '${remotedir}/'" | grep "$1" 164 fi 165 } 166 167 die() { 168 printf '%s\n' "$1" >&2 169 exit 1 170 } 171 172 clip=0 173 notify=0 174 border=0 175 resize=0 176 sharpen=0 177 public=0 178 get=0 179 delete=0 180 verbose=0 181 gopher=0 182 removemetadata=0 183 while :; do 184 case "$1" in 185 -h|-\?|--help) 186 show_help 187 exit 0 188 ;; 189 -V|--version) 190 show_version 191 exit 0 192 ;; 193 -v|--verbose) 194 verbose=1 195 ;; 196 -m|--remove-metadata) 197 removemetadata=1 198 ;; 199 -n|--notify) 200 notify=1 201 ;; 202 -c|--clip) 203 clip=1 204 ;; 205 -b|--border) 206 border=1 207 ;; 208 -r|--resize) 209 resize=1 210 ;; 211 -s|--sharpen) 212 sharpen=1 213 ;; 214 -p|--public) 215 public=1 216 dir="${pubdir}" 217 remotedir="${remote}/${dir}" 218 ;; 219 -o|--gopher) 220 gopher=1 221 ;; 222 -g|--get) 223 get=1 224 ;; 225 -d|--delete) 226 delete=1 227 ;; 228 -l|--list) 229 list_uploads "$2" 230 exit 0 231 ;; 232 --) # end all options 233 shift 234 break 235 ;; 236 -?*) 237 die 'error: Unknown option specified' 238 ;; 239 *) # No more options 240 break 241 esac 242 shift 243 done 244 245 if [ $# -lt 1 ]; then 246 show_help 247 exit 1 248 fi 249 250 if [ "$get" = 1 ] || [ "$delete" = 1 ]; then 251 if [ "$get" = 1 ]; then 252 for f in "$@"; do 253 download_file "$(basename "$f")" 254 done 255 fi 256 if [ "$delete" = 1 ]; then 257 for f in "$@"; do 258 delete_file "$(basename "$f")" 259 done 260 fi 261 else 262 if [ ! -f "$1" ]; then 263 if [ $verbose = 1 ]; then 264 echo "reading from stdin..." 265 fi 266 msg="$(cat)" 267 tempfile="$(mktemp)" 268 printf "%s" "$msg" > "$tempfile" 269 270 preprocess_file "$tempfile" "$1" 271 upload_file "$tempfile" "$(basename "$1")" 272 else 273 for f in "$@"; do 274 [ ! -f "$f" ] && die "error: $f is not a file" 275 tempfile="$(mktemp)" 276 cp "$f" "$tempfile" 277 preprocess_file "$tempfile" "$1" 278 upload_file "$tempfile" "$(basename "$f")" 279 done 280 fi 281 fi