forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-file
More file actions
executable file
·120 lines (102 loc) · 2.93 KB
/
Copy pathupload-file
File metadata and controls
executable file
·120 lines (102 loc) · 2.93 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
#!/bin/bash
readonly program="$(basename "${0}")"
readonly hosts=('transfer.sh' 'alpha.transfer.sh' 'bayfiles.com' 'anonfile.com' 'megaupload.is' 'forumfiles.com')
readonly default_host="${hosts[0]}"
function ascii_basename {
basename "${1}" | sed -e 's/[^a-zA-Z0-9._-]/-/g'
}
function string_in_array {
local string="${1}"
for value in "${@:2}"; do
[[ "${string}" == "${value}" ]] && return 0
done
return 1
}
function syntax_error {
echo -e "${program}: ${1}\nTry \`${program} --help\` for more information." >&2
exit 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}//'
}
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-u | --upload-host)
UPLOAD_FILE_TO="${2}"
shift
;;
--)
shift
args+=("${@}")
break
;;
-*)
syntax_error "unrecognized option: ${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, first copy them to a directory
if [[ "${#}" -gt 1 ]]; then
readonly tmp_zip_dir="$(mktemp -d)/archive"
mkdir "${tmp_zip_dir}"
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 tmp_dir="$(mktemp -d)"
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
# Upload
readonly file_name=$(ascii_basename "${file_path}")
if [[ -n "${UPLOAD_FILE_TO}" ]] && string_in_array "${UPLOAD_FILE_TO}" "${hosts[@]}"; then
upload_host="${UPLOAD_FILE_TO}"
else
upload_host="${default_host}"
fi
if [[ "${upload_host}" =~ ^(alpha\.)?transfer\.sh$ ]]; then
url="$(curl --globoff --progress-bar --upload-file "${file_path}" "https://${upload_host}/${file_name}")"
else
url="$(curl --globoff --progress-bar --form "file=@${file_path}" "https://api.${upload_host}/upload" | ruby -e 'require "json"; data = JSON.parse(gets); abort "There was an error uploading" if data["status"] == false; puts data["data"]["file"]["url"]["full"]')"
fi
echo -n "${url}" | pbcopy
echo "Copied to clipboard: ${url}"