-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileio
executable file
·97 lines (77 loc) · 1.69 KB
/
fileio
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
#!/bin/zsh
# Upload one or more files to https://file.io. Also yanks the url(s) to
# clipboard if you have `xclip, or pbcopy` installed.
#
# ---------------------------------
# Author:
# ---------------------------------
# Prajjwal Singh
# https://prajjwal.com/about.html
#
# Dependencies:
# - jq
#
# Optional Dependencies:
# - xclip | pbcopy (MacOS)
source "${0%/*}/include/util.sh"
ensure_presence_of jq
TTL="1d" # Files live for 1 day by default
BASE_URL="https://file.io"
usage() {
cat <<EOF
Usage: fileio [OPTIONS] [FILES]
Options:
-t Specify TTL of file in [d]ays, [w]eeks, [m]onths, or [y]ears.
Examples:
fileio -t 2d file.txt file-2.txt
fileio -t 1y file.txt
EOF
exit 1
}
while getopts ":t:h" opt; do
case $opt in
t)
TTL=$OPTARG
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument" >&2
usage
;;
esac
done
# Get all files after options
files=(${@:$OPTIND})
file_count=${#files[@]}
# Print usage & quit if no files were specified
[ $file_count -eq 0 ] && usage
links=""
# Upload a file to file.io, and append resulting url to $urls
function upload() {
response=$(curl -s -F "file=@$1" "$BASE_URL/?expires=$TTL")
link=$(echo $response | jq '.["link"]' | cut -d '"' -f2)
error=$(echo $response | jq '.["error"]')
if [[ "$link" == "null" ]]; then
echo $error
exit 1
else
if [ -z $links ]; then
links=$link
else
links="$links\n$link"
fi
fi
}
# Upload each input file
for file in $files; do
upload $file
done
# Copy URLS to clipboard
copy_to_clipboard $links
echo $links