-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload
More file actions
executable file
·139 lines (113 loc) · 3.68 KB
/
Copy pathupload
File metadata and controls
executable file
·139 lines (113 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bash curl libnotify moreutils wl-clipboard xclip
# shellcheck shell=bash
: "${UPLOAD_HOST:=https://0x0.st}"
: "${UPLOAD_POST_FILE_VARIABLE:=file}"
: "${UPLOAD_POST_URL_VARIABLE:=url}"
set -euo pipefail
usage() {
# shellcheck disable=SC2059
[[ "$#" -eq 0 ]] || printf "$@" >&2
cat >&2 <<EOF
usage: ${0##*/} [-cnv] [-h <host>] [-m (file | url)] [item...]
A simple client for uploading files to services in the style of
sprunge.us (RIP), 0x0.st, and their workalikes. Generally, these
services will have a minimal, plain text HTTP homepage, prescribing
that those wishing to use the service use curl(1) to perform a POST
to upload the file.
options:
-c Copy the destination URL to the clipboard. (X11/Wayland)
-h <host> Set the host to be uploaded to.
-m (file | url)
Operate in <mode> mode; valid modes are "file" and "url".
For "file", attempt to upload <item> to the host. For "url",
attempt to have the host download <item>.
-n Show a desktop notification on success.
-v Be more verbose; this is handled by curl(1).
[<item>...]
Upload <item> to host. If no items are provided, assume standard input
as <item>, block until EOF, and then upload it.
variables:
\$UPLOAD_HOST${UPLOAD_HOST:+ [current: ${UPLOAD_HOST@Q}]}
\$UPLOAD_POST_FILE_VARIABLE${UPLOAD_POST_FILE_VARIABLE:+ [current: ${UPLOAD_POST_FILE_VARIABLE@Q}]}
\$UPLOAD_POST_URL_VARIABLE${UPLOAD_POST_URL_VARIABLE:+ [current: ${UPLOAD_POST_URL_VARIABLE@Q}]}
Kylie McClain <kylie@somas.is>
EOF
[[ "$#" -eq 0 ]] || exit 1
exit 64 # EX_USAGE
}
clip() {
: "${1:?no input given}"
if [[ -v WAYLAND_DISPLAY ]]; then
wl-copy -n <<<"$1"
else
xclip -in -selection clipboard <<<"$1"
fi
}
output() {
local item="$1"
local url="$2"
printf '%s\n' "${url}"
if [[ "${clip}" == 'true' ]]; then
clip "${url}"
fi
if [[ "${notification}" == 'true' ]]; then
action=$(
notify-send \
-a upload \
-i upload-media \
-A 'open=Open' \
-A 'copy=Copy to clipboard' \
'Uploaded successfully' \
"Uploaded ${item@Q} to ${url}.${clip:+ Copied to clipboard.}"
)
case "${action}" in
open)
xdg-open "${url}" &
disown
;;
copy) clip "${url}" ;;
esac
fi
}
curl() {
command curl -A "${user_agent}" "${verbose:--sS}" "$@"
}
upload_stdin() { sponge | curl -F "${UPLOAD_POST_FILE_VARIABLE}=<-" "${UPLOAD_HOST}"; }
upload_file() { curl -F "${UPLOAD_POST_FILE_VARIABLE}=@${1}" "${UPLOAD_HOST}"; }
upload_url() { curl -F "${UPLOAD_POST_URL_VARIABLE}=${1}" "${UPLOAD_HOST}"; }
user_agent='upload <kylie@somas.is>'
mode='upload_file'
clip=
notification=
verbose=
while getopts :cm:nv arg >/dev/null 2>&1; do
case "${arg}" in
c) clip=true ;;
n) notification=true ;;
v) verbose=true ;;
m)
case "${OPTARG}" in
file | url) mode="${OPTARG}" ;;
*) usage 'error: unknown mode -- %s\n' "${OPTARG}" ;;
esac
;;
*) usage 'error: unknown argument -- %s\n' "${OPTARG}" ;;
esac
done
shift $((OPTIND - 1))
[[ "${clip}" == true ]] || clip=
[[ "${verbose}" == true ]] || verbose=
if [[ "$#" -eq 0 ]]; then
item=$(upload_stdin)
output "standard input" "${url}"
exit
fi
for item; do
case "${mode}" in
file) url=$(upload_file "${item}") ;;
url) url=$(upload_url "${item}") ;;
esac
output "${item}" "${url}"
shift
done