-
Notifications
You must be signed in to change notification settings - Fork 0
/
callable.sh
168 lines (155 loc) · 4.98 KB
/
callable.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# fileio(1)
#
# NAME
# fileio - Upload file(s) to https://file.io
#
# SYNOPSIS
# fileio file upload single file & copy link to clipboard
# fileio file1 file2 upload multiple files
# fileio:history output history
#
# FLAGS
# -e, --expires 'time' (default: 2 weeks) positive integer followed by
# w (week), m (month) or y (year)
#
# EXAMPLES
# ash fileio --expires 1w help.txt license.md
# ash fileio help.txt license.md -e 1w
# ash fileio --expires 1w help.txt license.md -e 1y readme.md
#
# NOTES
# The expires/e flag is able to be added after or before the files.
# Which ever you pattern you choose you must be consitant with.
FileIO__callable_main () {
Logger__set_prefix "file.io"
if [[ $@ == "" ]]; then
Logger__alert "No file passed"
return
fi
# hacky way to create an array of all params, including short flags
local str="$(echo "$@" | awk '{ for (i=1; i <= NF; i++) { print $i } }')"
local arr
local counter=0
for i in $str; do
arr[$counter]="$i"
counter=$(($counter+1))
done
local pos=$(( ${#arr[*]} - 1 ))
local last=${arr[$pos]}
local flagFound="false" # identifies the start or end of the list
local firstPass="true"
local filesAfter="false"
local files=""
local fileSets=()
local fSetIndex=""
local setIndex=""
local prevTime=""
for param in "${arr[@]}"; do
if [[ "${param}" == "-e" ]] || [[ "${param}" == "--expires" ]]; then
if [[ "$firstPass" == "true" ]]; then
filesAfter="true"
fi
flagFound="true"
continue
elif [[ -f "$param" ]]; then
if [[ $files == "" ]]; then
files+="$param" # start of set
else
files+=":$param"
fi
elif [[ ! "$param" =~ \d*([wmy]) ]]; then
Logger__error "Unknown parameter $param"
return
fi
if [[ "$flagFound" == "true" ]]; then
if [[ "$filesAfter" != "true" ]]; then
# add to set
time="$param"
# push current fileSet to fileSetArray
fSetIndex="${#fileSets[@]}"
setIndex=$(($fSetIndex + 1))
fileSets[$setIndex]="$time|$files"
# reset variables
time=""
files=""
elif [[ "$firstPass" != "true" ]]; then
# push current fileSet to fileSetArray
fSetIndex="${#fileSets[@]}"
setIndex=$(($fSetIndex + 1))
fileSets[$setIndex]="$prevTime|$files"
# reset variables
time=""
files=""
fi
prevTime="$param"
flagFound="false"
continue
elif [[ "$last" == "$param" ]]; then
# push current fileSet to fileSetArray
fSetIndex="${#fileSets[@]}"
setIndex=$(($fSetIndex + 1))
fileSets[$setIndex]="$prevTime|$files"
fi
firstPass="false"
done
pos=$(( ${#fileSets[*]} - 1 ))
last=${fileSets[$pos]}
for fileSet in ${fileSets[@]}; do
if [[ "$fileSet" =~ (.*)\|(.*) ]]; then
local expiry="${BASH_REMATCH[1]}"
local expiryQuery=""
local files="${BASH_REMATCH[2]}"
files=(${files//:/ })
if [[ "$expiry" != "" ]]; then
expiryQuery="/?expires=$expiry"
fi
local posFiles=$(( ${#files[*]} - 1 ))
local lastFile=${files[$posFiles]}
for file in $files; do
local url="https://file.io"
local output=$(curl --silent -F "file=@$file" "$url$expiryQuery")
if [[ $output =~ (\"success\"\:)(true|false) ]]; then
if [[ ${BASH_REMATCH[2]} == true ]]; then
if [[ $output =~ (\"link\"\:\")(.*)\", ]]; then
url="${BASH_REMATCH[2]}"
if [[ "$expiry" == "" ]]; then
expiry="2w"
fi
echo "$(date) ($expiry) [$url] $file" >> "$Ash__ACTIVE_MODULE_DIRECTORY/extras/history.txt"
Logger__success "Uploaded: [$url] $file"
if [[ "${#fileSets[@]}" == 1 ]] && [[ "$file" == "$lastFile" ]]; then
# Copy to clipboard
echo $url | pbcopy
Logger__success "Copied to clipboard."
fi
else
Logger__alert "Unable to decode response: $output"
return
fi
else
Logger__alert "Failed to upload"
if [[ $output =~ (\"error\"\:)(.*), ]]; then # error code
local errorMsg="Error: (${BASH_REMATCH[2]})"
if [[ $output =~ (\"message\":)(\")(.*)\" ]]; then
errorMsg+=" ${BASH_REMATCH[3]}"
fi
Logger__alert "$errorMsg"
elif [[ $output =~ (\"message\"\:)(\")(.*)\" ]]; then
Logger__alert "Message: ${BASH_REMATCH[3]}"
else
Logger__alert "Unknown Issue: $output"
fi
return
fi
fi
done
fi
done
}
FileIO__callable_help () {
more "$Ash__ACTIVE_MODULE_DIRECTORY/help.txt"
}
FileIO__callable_history () {
cat "$Ash__ACTIVE_MODULE_DIRECTORY/extras/history.txt"
}