-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathupload-file
executable file
·137 lines (118 loc) · 3.86 KB
/
upload-file
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
#!/bin/bash
readonly program="$(basename "${0}")"
readonly hosts=('0x0.st' 'litterbox.catbox.moe' 'transfer.sh' 'transfer.archivete.am' 'filebin.net' 'oshi.at')
readonly default_host="${hosts[0]}"
# Create and cleanup temporary directories
readonly tmp_dir="$(mktemp -d)"
readonly tmp_zip_dir="$(mktemp -d)/archive"
mkdir "${tmp_zip_dir}"
trap 'rm -rf "${tmp_dir}" "${tmp_zip_dir}"' EXIT
function ascii_basename {
basename "${1}" | sed -e 's/[^a-zA-Z0-9._-]/-/g'
}
function is_string_in_array {
local -r string="${1}"
for value in "${@:2}"; do
[[ "${string}" == "${value}" ]] && return 0
done
return 1
}
function usage {
echo "
Upload files and directories to a file hosting service.
If multiple files or a directory are given, they will be zipped beforehand.
To set a host as the default, export UPLOAD_FILE_TO in your shell.
Valid hosts:
$(printf '\n %s' "${hosts[@]}")
Usage:
${program} [options] <path...>
Options:
-u, --upload-host <host> File host to upload to. Defaults to ${default_host}.
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Options
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-u | --upload-host)
UPLOAD_FILE_TO="${2}"
shift
;;
--)
shift
args+=("${@}")
break
;;
-*)
echo "Unrecognised option: ${1}"
exit 1
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
if [[ "${#}" -lt 1 ]]; then
usage
exit 1
fi
# Abort if any of the paths is invalid
for path in "${@}"; do
if [[ ! -e "${path}" ]]; then
echo "${path} does not exist." >&2
exit 1
fi
done
# If acting on multiple files or a software bundle, first copy them to a directory
if [[ "${#}" -gt 1 || -d "${1}/Contents" ]]; then
cp -r "${@}" "${tmp_zip_dir}"
readonly given_file="${tmp_zip_dir}"
else
readonly given_file="${1}"
fi
# Make zip if acting on a directory
if [[ -d "${given_file}" ]]; then
readonly dir_name="$(ascii_basename "${given_file}")"
readonly zip_file="${tmp_dir}/${dir_name}.zip"
DITTONORSRC=1 ditto -ck "${given_file}" "${zip_file}"
readonly file_path="${zip_file}"
else
readonly file_path="${given_file}"
fi
# Escape quotes, so we can quote curl's "--form"
# to allow for filenames with commas and semicolons
readonly escaped_file_path="${file_path//\"/\\\"}"
# Upload
if [[ -n "${UPLOAD_FILE_TO}" ]]; then
if is_string_in_array "${UPLOAD_FILE_TO}" "${hosts[@]}"; then
readonly upload_host="${UPLOAD_FILE_TO}"
else
echo "Invalid upload host: ${UPLOAD_FILE_TO}" >&2
exit 1
fi
else
readonly upload_host="${default_host}"
fi
if [[ "${upload_host}" == '0x0.st' ]]; then
readonly url="$(curl --globoff --progress-bar --form "file=@\"${escaped_file_path}\"" "https://${upload_host}")"
elif [[ "${upload_host}" == 'litterbox.catbox.moe' ]]; then
readonly url="$(curl --globoff --progress-bar --form 'reqtype=fileupload' --form 'time=72h' --form "fileToUpload=@\"${escaped_file_path}\"" "https://${upload_host}/resources/internals/api.php")"
elif [[ "${upload_host}" == 'transfer.sh' ]]; then
readonly url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}")"
elif [[ "${upload_host}" == 'transfer.archivete.am' ]]; then
readonly url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}")"
elif [[ "${upload_host}" == 'filebin.net' ]]; then
readonly url="https://${upload_host}/$(uuidgen)/$(uuidgen)"
curl --globoff --progress-bar --request 'POST' "${url}" --data-binary "@${file_path}" --output /dev/null
elif [[ "${upload_host}" == 'oshi.at' ]]; then
readonly url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}" | grep ' \[Download\]' | sed 's/ \[Download\].*//')"
fi
echo -n "${url}" | pbcopy
echo "Copied to clipboard: ${url}"