# ccu: upload files to https://dosyaup.com.tr from bash, zsh (or any POSIX sh). # # Install: curl -fsSo ~/.ccu.sh https://dosyaup.com.tr/_s/ccu.sh && echo '. ~/.ccu.sh' >> ~/.bashrc # or ~/.zshrc # Try it: . <(curl -fsS https://dosyaup.com.tr/_s/ccu.sh) # Update: run the curl line again. # # Based on the transfer.sh helper by # Remco Verhoef # https://gist.github.com/nl5887/a511f172d3fb3cd0e42d # Modified to use tar instead of zip; adapted for ccu (expiry, delete link, stdin, cleanup). # # Usage: ccu [options] [name] # ccu [options] - [name] read stdin # | ccu [options] [name] # Options: # -d N keep for N days (server default 3, max 7) # -c encrypt with gpg (symmetric, ASCII armor), adds .gpg (-ca also works) # -q no progress bar # -h help # The link goes to stdout, the delete command to stderr. Directories are sent as name.tar.gz. # Names: Turkish letters are transliterated, anything outside A-Z a-z 0-9 . _ - becomes "-". # Set CCU_URL to use another server. ccu() ( __ccu_usage() { cat <<'EOF' Usage: ccu [options] [name] ccu [options] - [name] (read stdin) | ccu [options] [name] Examples: ccu report.pdf ccu photos/ -d 7 directory -> photos.tar.gz, kept 7 days ccu notes.md readme.md upload under another name cat app.log | ccu app.log ccu secret.txt -c gpg symmetric encryption -> secret.txt.gpg Options: -d N keep for N days (server default 3, max 7) -c encrypt with gpg (symmetric, ASCII armor); -ca also works -q no progress bar -h this help EOF } __ccu_clean() { printf '%s' "$1" | LC_ALL=C command sed 's/ç/c/g;s/Ç/C/g;s/ğ/g/g;s/Ğ/G/g;s/ı/i/g;s/İ/I/g;s/ö/o/g;s/Ö/O/g;s/ş/s/g;s/Ş/S/g;s/ü/u/g;s/Ü/U/g' | LC_ALL=C command tr -c 'A-Za-z0-9._-' '-' | LC_ALL=C command tr -s '-' | LC_ALL=C command sed 's/-\././g' } set -o pipefail 2>/dev/null base=${CCU_URL:-https://dosyaup.com.tr} base=${base%/} days='' crypt=0 quiet=0 np=0 p1='' p2='' guessed=0 while [ $# -gt 0 ]; do case $1 in -h|--help) __ccu_usage; exit 0 ;; -c|-ca|--encrypt) crypt=1 ;; -q|--quiet) quiet=1 ;; -d|--days) if [ $# -lt 2 ]; then echo "ccu: $1 needs a number of days" >&2; exit 2; fi days=$2; shift ;; -d[0-9]*) days=${1#-d} ;; --) shift; while [ $# -gt 0 ]; do np=$((np + 1)); [ $np -eq 1 ] && p1=$1 || p2=$1; shift; done; break ;; -?*) echo "ccu: unknown option: $1" >&2; __ccu_usage >&2; exit 2 ;; *) np=$((np + 1)); [ $np -eq 1 ] && p1=$1 || p2=$1 ;; esac shift done if [ $np -gt 2 ]; then echo "ccu: too many arguments (one file per call)" >&2; exit 2; fi case $days in '') ;; *[!0-9]*) echo "ccu: -d needs a whole number of days" >&2; exit 2 ;; esac if ! command -v curl >/dev/null 2>&1; then echo "ccu: curl is not installed" >&2; exit 1; fi if [ $crypt -eq 1 ] && ! command -v gpg >/dev/null 2>&1; then echo "ccu: gpg is not installed" >&2; exit 1; fi # What to send. An existing path always wins; stdin is read only for "-", for no argument, # or when the only argument is not an existing path (then it is the name: cat x | ccu x.txt). if [ $np -eq 0 ]; then if [ -t 0 ]; then __ccu_usage >&2; exit 1; fi src=- name=stdin.txt elif [ "$p1" = - ]; then src=- name=${p2:-stdin.txt} elif [ -e "$p1" ]; then src=$p1 name=${p2:-$(command basename -- "$p1")} if [ -d "$p1" ]; then case $name in *.tar.gz|*.tgz) ;; *) name=$name.tar.gz ;; esac elif [ ! -r "$p1" ]; then echo "ccu: $p1: not readable" >&2; exit 1 fi elif [ $np -eq 1 ] && [ ! -t 0 ]; then src=- name=$p1 guessed=1 else echo "ccu: $p1: no such file or directory" >&2; exit 1 fi if [ $crypt -eq 1 ]; then case $name in *.gpg|*.asc) ;; *) name=$name.gpg ;; esac fi name=$(__ccu_clean "$name") name=${name#-}; name=${name%-} case $name in ''|.|..) name=file.bin ;; esac tmp=$(command mktemp -d "${TMPDIR:-/tmp}/ccu.XXXXXX") || exit 1 trap 'command rm -rf "$tmp"' EXIT trap 'exit 130' HUP INT TERM if [ "$src" != - ]; then if [ -d "$src" ]; then dir=$(command dirname -- "$src") b=$(command basename -- "$src") command tar -czf "$tmp/up.tar.gz" -C "$dir" "$b" || { echo "ccu: tar failed" >&2; exit 1; } src=$tmp/up.tar.gz fi if [ $crypt -eq 1 ]; then command gpg -c --armor -o "$tmp/up.gpg" -- "$src" || { echo "ccu: gpg failed" >&2; exit 1; } src=$tmp/up.gpg fi fi url=$base/$name [ -n "$days" ] && url="$url?days=$days" if [ $quiet -eq 1 ] || [ ! -t 2 ]; then prog=-sS; else prog=--progress-bar; fi if [ "$src" = - ] && [ $crypt -eq 1 ]; then command gpg -c --armor -o - | command curl -g "$prog" -o "$tmp/body" -D "$tmp/hdr" -w '%{http_code}' -T - "$url" >"$tmp/code" else command curl -g "$prog" -o "$tmp/body" -D "$tmp/hdr" -w '%{http_code}' -T "$src" "$url" >"$tmp/code" fi || { echo "ccu: upload failed" >&2; exit 1; } code=$(command cat "$tmp/code") if [ "$code" != 200 ]; then { command cat "$tmp/body"; echo; } >&2 [ $guessed -eq 1 ] && echo "ccu: (there is no file named $p1, so stdin was sent under that name)" >&2 exit 1 fi link=$(command cat "$tmp/body") printf '%s\n' "$link" del=$(command grep -i '^x-url-delete:' "$tmp/hdr" | command tail -n 1 | command sed 's/^[^:]*:[[:space:]]*//' | command tr -d '\r') [ -n "$del" ] && printf 'delete: curl -X DELETE "%s"\n' "$del" >&2 [ $crypt -eq 1 ] && printf 'decrypt: curl -s "%s" | gpg -d\n' "$link" >&2 exit 0 )