all repos — dotfiles @ db03b022017a6eed1b465a1bd13f0b2e1588236d

my *nix dotfiles

scripts/oxo.sh (view raw)

 1#!/bin/bash
 2# 0x0 - client for 0x0.st
 3
 4me=${0##*/}
 5host=https://0x0.st
 6
 7if [[ ! -t 1 ]];then
 8    quiet=true
 9fi
10
11help() {
12    cat >&2 <<EOF
13Usage: ${me} [-f <file>] [-s <url>] [-u <url>] [file]
14Client for interacting with 0x0.st.
15If no file is given, upload stdin.
16    -f <file>       - upload <file>
17    -s <url>        - shorten <url>
18    -u <url>        - upload content of <url>
19EOF
20}
21
22clip() {
23    if command -v xsel >/dev/null 2>&1;then
24        printf '%s' "$@" | xsel -b
25    fi
26}
27
28file_upload() {
29    local curl_opts="-s" file="$1" type
30    [[ "${quiet}" ]] || curl_opts="-#"
31    [[ "${quiet}" ]] || echo "uploading \"${file}\"..." >&2
32    [[ "$#" -gt 1 ]] && echo -n "${file} ... "
33    url=$(curl ${curl_opts} -F "file=@${file}" "${host}")
34    echo "${url}"
35    clip "${url}"
36}
37
38shorten_url() {
39    local curl_opts="-s" url="$1" shortened
40    [[ "${quiet}" ]] || curl_opts="-#"
41    [[ "${quiet}" ]] || echo "shortening \"${url}\"..." >&2
42    [[ "$#" -gt 1 ]] && echo -n "${url} ... "
43    shortened=$(curl ${curl_opts} -F "shorten=${url}" "${host}")
44    echo "${shortened}"
45    clip "${shortened}"
46}
47
48upload_url() {
49    local curl_opts="-s" url="$1" uploaded
50    [[ "${quiet}" ]] || curl_opts="-#"
51    [[ "${quiet}" ]] || echo "uploading \"${url}\"..." >&2
52    [[ "$#" -gt 1 ]] && echo -n "${url} ... "
53    uploaded=$(curl ${curl_opts} -F "url=${url}" "${host}")
54    echo "${uploaded}"
55    clip "${uploaded}"
56}
57
58# 1KiB = 1024 bytes
59# 1MiB = 1024 KiB
60# max size - 256MiB
61
62max_size=$(( (1024*1024) * 256 ))
63
64if [[ -f "$1" || "$#" -lt 1 ]];then
65    mode="default"
66else
67    mode="$1"
68    shift
69fi
70
71case "$mode" in
72    default)
73        if [[ "$#" -gt 0 ]];then
74            file_upload "${@}"
75        else
76            quiet=true file_upload "-"
77        fi
78    ;;
79    -f)
80        file_upload "${@}"
81    ;;
82    -u)
83        upload_url "${@}"
84    ;;
85    -s)
86        shorten_url "${@}"
87    ;;
88    -h|--help)
89        help
90    ;;
91esac
92