-
Notifications
You must be signed in to change notification settings - Fork 53
/
upload-file
executable file
·160 lines (137 loc) · 4.75 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
readonly program="$(basename "${0}")"
readonly hosts=('0x0.st' 'litterbox.catbox.moe' 'transfer.sh' 'transfer.archivete.am' 'pixeldrain.com' 'free.keep.sh' 'oshi.at' 'bayfiles.com' 'anonfile.com' 'rapidshare.nu' 'forumfiles.com')
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 kopimi_parse {
local -r json="$(< /dev/stdin)"
osascript -l JavaScript -e 'function run(argv) {
const data = JSON.parse(argv[0])
if (!data["status"]) { return "There was an error uploading" }
return data["data"]["file"]["url"]["full"]
}' "${json}"
}
function pixeldrain_parse {
local -r json="$(< /dev/stdin)"
osascript -l JavaScript -e 'function run(argv) {
const data = JSON.parse(argv[0])
if (data["success"] === false) { return data["message"] }
return "https://pixeldrain.com/u/" + data["id"]
}' "${json}"
}
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}" == 'pixeldrain.com' ]]; then
readonly url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}/api/file/" | pixeldrain_parse)"
elif [[ "${upload_host}" == 'free.keep.sh' ]]; then
readonly url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}")"
elif [[ "${upload_host}" == 'oshi.at' ]]; then
readonly url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}" | grep ' \[Download\]' | sed 's/ \[Download\].*//')"
else
readonly url="$(curl --globoff --progress-bar --form "file=@\"${escaped_file_path}\"" "https://api.${upload_host}/upload" | kopimi_parse)"
fi
echo -n "${url}" | pbcopy
echo "Copied to clipboard: ${url}"