-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_gofile_multi_stable.sh
executable file
·297 lines (259 loc) · 8.88 KB
/
cl_gofile_multi_stable.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash
# Function to create or update the config file
update_config() {
local token="$1"
echo "auth_token=\"$token\"" > "$config_file"
chmod 600 "$config_file"
echo "Auth token updated in $config_file"
}
# Config file location
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
config_file="$USERPROFILE/.gofile_config"
else
config_file="$HOME/.gofile_config"
fi
# Initialize auth_token
auth_token=""
# Check if config file exists, if so, source it
if [ -f "$config_file" ]; then
source "$config_file"
fi
# Set other default values
default_directory="./"
default_folderid=""
recursive=false
os_type=""
min_size=""
delete_after_upload=false
move_after_upload=false
# Parse command-line arguments
while getopts ":d:a:f:ro:u:s:Dm" opt; do
case $opt in
d) directory="$OPTARG"
;;
a) auth_token="$OPTARG"
;;
f) folderid="$OPTARG"
;;
r) recursive=true
;;
o) os_type="$OPTARG"
;;
u) echo "Please enter your new GoFile auth token:"
read -r new_token
update_config "$new_token"
exit 0
;;
s) min_size="$OPTARG"
;;
D) delete_after_upload=true
;;
m) move_after_upload=true
;;
\?) echo "Invalid option -$OPTARG" >&2
echo "Usage: $0 [-d directory] [-a auth_token] [-f folderid] [-r] [-o os_type] [-u] [-s min_size] [-D] [-m]"
exit 1
;;
esac
done
# Use defaults if not provided
directory="${directory:-$default_directory}"
folderid="${folderid:-$default_folderid}"
# Check if auth_token is set, if not, exit with an error
if [ -z "$auth_token" ]; then
echo "Error: No auth token provided. Please set it in the config file or use the -a flag."
exit 1
fi
# Confirmation for delete option
if [ "$delete_after_upload" = true ]; then
echo "WARNING: The delete option (-D) will permanently remove files after successful upload."
echo "To confirm, please type exactly: 'I understand and accept the risk of deleting files'"
read -r confirmation
if [ "$confirmation" != "I understand and accept the risk of deleting files" ]; then
echo "Confirmation failed. Exiting without deleting files."
exit 1
fi
fi
# Confirmation for move option
if [ "$move_after_upload" = true ]; then
echo "The move option (-m) will move files to a 'completed' folder after successful upload."
echo "To confirm, please type exactly: 'I confirm the file movement'"
read -r confirmation
if [ "$confirmation" != "I confirm the file movement" ]; then
echo "Confirmation failed. Exiting without moving files."
exit 1
fi
# Create 'completed' folder if it doesn't exist
mkdir -p "${directory}/completed"
fi
# Detect OS if not specified
if [ -z "$os_type" ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
os_type="macos"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
os_type="linux"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
os_type="windows"
else
echo "Unsupported OS. Please specify using -o option (macos, linux, or windows)."
exit 1
fi
fi
# Function to get available 'na' server
get_na_server() {
local max_attempts=16
local attempt=1
local server_info
local na_server
while [ $attempt -le $max_attempts ]; do
server_info=$(curl -s -X GET 'https://api.gofile.io/servers')
if [ $? -ne 0 ]; then
if [ $attempt -le 5 ]; then
echo "Failed to fetch server info. Retrying in 60 seconds (attempt $attempt of $max_attempts)..." >&2
sleep_time=60
else
echo "Failed to fetch server info. Retrying in 300 seconds (attempt $attempt of $max_attempts)..." >&2
sleep_time=300
fi
echo "You can force quit the script with Ctrl+C if needed." >&2
sleep $sleep_time
((attempt++))
continue
fi
na_server=$(echo "$server_info" | jq -r '.data.servers[] | select(.zone == "na") | .name' | head -n 1)
if [ -n "$na_server" ]; then
echo "$na_server"
return 0
fi
if [ $attempt -le 5 ]; then
echo "No 'na' server available. Retrying in 60 seconds (attempt $attempt of $max_attempts)..." >&2
sleep_time=60
else
echo "No 'na' server available. Retrying in 300 seconds (attempt $attempt of $max_attempts)..." >&2
sleep_time=300
fi
echo "You can force quit the script with Ctrl+C if needed." >&2
sleep $sleep_time
((attempt++))
done
echo "Failed to get an 'na' server after $max_attempts attempts. Exiting." >&2
exit 1
}
# Function to format time
format_time() {
local seconds=$1
printf "%02d:%02d:%02d" $((seconds/3600)) $((seconds%3600/60)) $((seconds%60))
}
# Function to format file size
format_size() {
local size=$1
if [ $size -ge 1073741824 ]; then
printf "%.2f GB" $(awk "BEGIN {printf \"%.2f\", $size/1073741824}")
elif [ $size -ge 1048576 ]; then
printf "%.2f MB" $(awk "BEGIN {printf \"%.2f\", $size/1048576}")
else
printf "%d bytes" $size
fi
}
# Function to convert size to bytes
convert_to_bytes() {
local size="$1"
local unit="${size: -2}"
local value="${size%??}"
case "$unit" in
KB|kb) echo $((value * 1024)) ;;
MB|mb) echo $((value * 1024 * 1024)) ;;
*) echo "$size" ;;
esac
}
# Set find options based on recursive flag, OS, and min_size
if [ -n "$min_size" ]; then
min_size_bytes=$(convert_to_bytes "$min_size")
size_option="-size +${min_size_bytes}c"
else
size_option=""
fi
if [ "$recursive" = true ]; then
find_opts="-type f $size_option ! -name '.*' ! -name 'SYNOINDEX_*'"
else
find_opts="-maxdepth 1 -type f $size_option ! -name '.*' ! -name 'SYNOINDEX_*'"
fi
# Get total number of files and size
if [ "$os_type" = "macos" ]; then
total_files=$(eval find "$directory" $find_opts | wc -l)
total_size=$(eval find "$directory" $find_opts -print0 | xargs -0 stat -f%z | awk '{sum+=$1} END {print sum}')
else
total_files=$(eval find "$directory" $find_opts | wc -l)
total_size=$(eval find "$directory" $find_opts -print0 | xargs -0 stat -c%s | awk '{sum+=$1} END {print sum}')
fi
# Initialize counters
current_file=0
uploaded_size=0
# Create a temporary file to store sorted file list
temp_file=$(mktemp)
trap 'rm -f "$temp_file"' EXIT
# Sort files by size and save to temporary file
if [ "$os_type" = "macos" ]; then
eval find "$directory" $find_opts -print0 | while IFS= read -r -d '' file; do
file_size=$(stat -f%z "$file")
echo "$file_size $file"
done | sort -n | cut -d' ' -f2- > "$temp_file"
else
eval find "$directory" $find_opts -print0 | while IFS= read -r -d '' file; do
file_size=$(stat -c%s "$file")
echo "$file_size $file"
done | sort -n | cut -d' ' -f2- > "$temp_file"
fi
# Read from the temporary file and process each file
while read -r file; do
if [ -f "$file" ]; then
((current_file++))
file_name=$(basename "$file")
if [ "$os_type" = "macos" ]; then
file_size=$(stat -f%z "$file")
else
file_size=$(stat -c%s "$file")
fi
formatted_size=$(format_size $file_size)
echo "=========================================="
echo "Uploading file $current_file of $total_files: $file_name ($formatted_size)"
echo "Overall progress: $((uploaded_size * 100 / total_size))% completed"
# Get the 'na' server for this upload
storenum=$(get_na_server)
echo "Using server: $storenum"
# GoFile API endpoint
url="https://$storenum.gofile.io/contents/uploadfile"
start_time=$(date +%s)
curl_output=$(curl -X POST "$url" \
-H "Authorization: Bearer $auth_token" \
-F "file=@$file" \
-F "folderId=$folderid" \
--progress-bar)
end_time=$(date +%s)
duration=$((end_time - start_time))
uploaded_size=$((uploaded_size + file_size))
upload_status=$(echo "$curl_output" | jq -r '.status')
download_page=$(echo "$curl_output" | jq -r '.data.downloadPage')
echo
if [ "$upload_status" == "ok" ]; then
echo "Uploaded $file_name successfully!"
echo "Time taken: $(format_time $duration)"
echo "Download Page: $download_page"
if [ "$delete_after_upload" = true ]; then
rm "$file"
echo "File deleted: $file_name"
elif [ "$move_after_upload" = true ]; then
mv "$file" "${directory}/completed/"
echo "File moved to completed folder: $file_name"
fi
else
echo "Failed to upload $file_name"
echo "Error: $curl_output"
fi
echo "Overall progress: $((uploaded_size * 100 / total_size))% completed"
echo "=========================================="
echo
fi
done < "$temp_file"
total_formatted_size=$(format_size $total_size)
echo "All files uploaded. Total size: $total_formatted_size. Total time: $(format_time $SECONDS)"