-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgofile.sh
More file actions
1309 lines (1124 loc) · 37.7 KB
/
Copy pathgofile.sh
File metadata and controls
1309 lines (1124 loc) · 37.7 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# gofile.sh - A more robust Bash CLI for the Gofile.io API
#
# Requires: bash, curl, jq
#
# Notes:
# - Defaults to the official global upload endpoint:
# POST https://upload.gofile.io/uploadfile
# - Supports official regional upload proxies and a legacy --server option.
#
# License: MIT (same spirit as original)
#
set -euo pipefail
IFS=$'\n\t'
VERSION="2.1.0"
# --------------------------------------------------
# Config (XDG-friendly)
# --------------------------------------------------
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
CONFIG_DIR="${GOFILE_CONFIG_DIR:-$XDG_CONFIG_HOME/gofile-cli}"
CONFIG_FILE="${GOFILE_CONFIG_FILE:-$CONFIG_DIR/config}"
API_BASE="${GOFILE_API_BASE:-https://api.gofile.io}"
API_BASE="${API_BASE%/}"
UPLOAD_URL_DEFAULT="${GOFILE_UPLOAD_URL:-https://upload.gofile.io/uploadfile}"
CURL_CONNECT_TIMEOUT="${GOFILE_CONNECT_TIMEOUT:-15}"
CURL_MAX_TIME="${GOFILE_MAX_TIME:-0}"
# --------------------------------------------------
# Global flags
# --------------------------------------------------
OUTPUT_MODE="pretty" # pretty|compact|raw
VERBOSE=0
CLI_TOKEN="" # optional override via --token
NO_AUTH=0 # if 1, do not send Authorization even if token exists (guest mode)
# --------------------------------------------------
# Small helpers
# --------------------------------------------------
say() { printf '%s\n' "$*"; }
err() { printf 'Error: %s\n' "$*" >&2; }
die() { err "$*"; exit 1; }
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing dependency: $1"
}
debug() {
if [[ "$VERBOSE" -ge 1 ]]; then
printf '[debug] %s\n' "$*" >&2
fi
}
init_config() {
local config_parent="."
if [[ "$CONFIG_FILE" == */* ]]; then
config_parent="${CONFIG_FILE%/*}"
fi
if [[ ! -d "$config_parent" ]]; then
(umask 077; mkdir -p -- "$config_parent")
fi
(umask 077; touch "$CONFIG_FILE")
chmod 600 "$CONFIG_FILE" 2>/dev/null || true
}
require_value() {
local option="$1"
local count="$2"
[[ "$count" -ge 2 ]] || die "$option requires a value"
}
require_exact_args() {
local expected="$1"
local usage_text="$2"
local actual="$3"
[[ "$actual" -eq "$expected" ]] || die "Usage: $0 $usage_text"
}
# Token precedence:
# 1) --token
# 2) GOFILE_TOKEN env
# 3) config file API_TOKEN=
get_token() {
if [[ "$NO_AUTH" -eq 1 ]]; then
printf '\n'
return 0
fi
if [[ -n "${CLI_TOKEN:-}" ]]; then
printf '%s\n' "$CLI_TOKEN"
return 0
fi
if [[ -n "${GOFILE_TOKEN:-}" ]]; then
printf '%s\n' "$GOFILE_TOKEN"
return 0
fi
if [[ -f "$CONFIG_FILE" ]]; then
awk '/^API_TOKEN=/{sub(/^API_TOKEN=/,""); sub(/\r$/, ""); print; exit}' "$CONFIG_FILE"
else
printf '\n'
fi
}
require_token() {
local t
t="$(get_token)"
[[ -n "$t" ]] || die "API token is not set. Use: $0 set-token <TOKEN> (or export GOFILE_TOKEN=...)"
}
set_token() {
local new_token="$1"
[[ -n "$new_token" ]] || die "Usage: $0 set-token <TOKEN>"
[[ "$new_token" != *$'\n'* && "$new_token" != *$'\r'* ]] || die "Token must be a single line"
init_config
local tmp
tmp="$(mktemp "${CONFIG_FILE}.tmp.XXXXXX")" || die "Could not create a temporary config file"
trap '[[ -z "${tmp-}" ]] || rm -f -- "$tmp"' EXIT
if [[ -s "$CONFIG_FILE" ]]; then
grep -v '^API_TOKEN=' "$CONFIG_FILE" >"$tmp" || true
fi
printf 'API_TOKEN=%s\n' "$new_token" >>"$tmp"
if ! mv -f "$tmp" "$CONFIG_FILE"; then
rm -f -- "$tmp"
die "Could not update $CONFIG_FILE"
fi
tmp=""
trap - EXIT
chmod 600 "$CONFIG_FILE" 2>/dev/null || true
say "API token saved to $CONFIG_FILE"
}
unset_token() {
init_config
local tmp
tmp="$(mktemp "${CONFIG_FILE}.tmp.XXXXXX")" || die "Could not create a temporary config file"
trap '[[ -z "${tmp-}" ]] || rm -f -- "$tmp"' EXIT
grep -v '^API_TOKEN=' "$CONFIG_FILE" >"$tmp" || true
if ! mv -f "$tmp" "$CONFIG_FILE"; then
rm -f -- "$tmp"
die "Could not update $CONFIG_FILE"
fi
tmp=""
trap - EXIT
chmod 600 "$CONFIG_FILE" 2>/dev/null || true
say "API token removed from $CONFIG_FILE"
}
mask_token() {
local t="$1"
if [[ ${#t} -le 8 ]]; then
printf '********'
else
printf '%s…%s' "${t:0:4}" "${t: -4}"
fi
}
validate_password_hash() {
local hash="$1"
[[ "$hash" =~ ^[[:xdigit:]]{64}$ ]] || die "Password hash must be exactly 64 hexadecimal characters"
}
validate_http_settings() {
[[ "$CURL_CONNECT_TIMEOUT" =~ ^[0-9]+([.][0-9]+)?$ ]] || die "GOFILE_CONNECT_TIMEOUT must be a non-negative number"
[[ "$CURL_MAX_TIME" =~ ^[0-9]+([.][0-9]+)?$ ]] || die "GOFILE_MAX_TIME must be a non-negative number"
}
is_official_gofile_url() {
[[ "$1" =~ ^https://([[:alnum:]-]+\.)*gofile\.io(/|$) ]]
}
curl_form_escape_path() {
local path="$1"
path="${path//\\/\\\\}"
path="${path//\"/\\\"}"
printf '%s' "$path"
}
print_json() {
case "$OUTPUT_MODE" in
raw) cat ;;
compact) jq -c '.' ;;
pretty|*) jq '.' ;;
esac
}
# SHA-256 helper for password hashing (API expects sha256 hex for password query params)
sha256_hex() {
local s="$1"
if command -v sha256sum >/dev/null 2>&1; then
printf '%s' "$s" | sha256sum | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
printf '%s' "$s" | shasum -a 256 | awk '{print $1}'
elif command -v openssl >/dev/null 2>&1; then
# openssl output differs across versions; -r is usually: "<hash> *stdin"
local out
out="$(printf '%s' "$s" | openssl dgst -sha256 -r 2>/dev/null || true)"
if [[ -n "$out" ]]; then
echo "$out" | awk '{print $1}'
else
# fallback: "(stdin)= <hash>"
printf '%s' "$s" | openssl dgst -sha256 2>/dev/null | awk '{print $NF}'
fi
else
die "To hash passwords you need one of: sha256sum, shasum, openssl"
fi
}
extract_id() {
# Allow passing a gofile share URL; extract the code/ID.
# e.g. https://gofile.io/d/AbCdEf -> AbCdEf
local input="$1"
if [[ "$input" =~ ^https?:// ]]; then
if [[ "$input" =~ /d/([A-Za-z0-9_-]+) ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
if [[ "$input" =~ [\?\&]c=([A-Za-z0-9_-]+) ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
fi
echo "$input"
}
# --------------------------------------------------
# HTTP helpers (status-aware)
# --------------------------------------------------
curl_json() {
# curl_json METHOD URL [JSON_DATA]
local method="$1"
local url="$2"
local data="${3-}"
local token
token="$(get_token)"
local args=(
-sS
--connect-timeout "$CURL_CONNECT_TIMEOUT"
--max-time "$CURL_MAX_TIME"
-X "$method"
-H "Accept: application/json"
-H "User-Agent: gofile-cli/$VERSION"
)
if [[ -n "$token" ]]; then
args+=(-H "Authorization: Bearer $token")
fi
if [[ -n "$data" ]]; then
args+=(-H "Content-Type: application/json" -d "$data")
fi
debug "curl_json $method $url"
local resp http_code body
if ! resp="$(curl "${args[@]}" "$url" -w $'\n%{http_code}')"; then
err "Request failed: $method $url"
return 1
fi
http_code="${resp##*$'\n'}"
body="${resp%$'\n'*}"
if [[ ! "$http_code" =~ ^[0-9]{3}$ ]]; then
err "Invalid HTTP status from $url"
return 1
fi
if (( 10#$http_code < 200 || 10#$http_code >= 300 )); then
err "HTTP $http_code from $url"
printf '%s\n' "$body" >&2
return 1
fi
# Validate JSON + API status
if ! printf '%s\n' "$body" | jq -e '.' >/dev/null 2>&1; then
err "Non-JSON response from $url"
printf '%s\n' "$body" >&2
return 1
fi
local status
status="$(printf '%s\n' "$body" | jq -r '.status // empty')"
if [[ -n "$status" && "$status" != "ok" ]]; then
err "API status=$status"
printf '%s\n' "$body" >&2
return 1
fi
printf '%s\n' "$body"
}
curl_json_get_urlencoded() {
# curl_json_get_urlencoded URL key value [key value ...]
local url="$1"
shift
local token
token="$(get_token)"
local args=(
-sS
--connect-timeout "$CURL_CONNECT_TIMEOUT"
--max-time "$CURL_MAX_TIME"
-G
-H "Accept: application/json"
-H "User-Agent: gofile-cli/$VERSION"
)
if [[ -n "$token" ]]; then
args+=(-H "Authorization: Bearer $token")
fi
while [[ $# -gt 0 ]]; do
[[ $# -ge 2 ]] || die "Internal error: query parameter has no value"
local k="$1"; local v="$2"
args+=(--data-urlencode "$k=$v")
shift 2
done
debug "curl_json_get_urlencoded $url"
local resp http_code body
if ! resp="$(curl "${args[@]}" "$url" -w $'\n%{http_code}')"; then
err "Request failed: GET $url"
return 1
fi
http_code="${resp##*$'\n'}"
body="${resp%$'\n'*}"
if [[ ! "$http_code" =~ ^[0-9]{3}$ ]]; then
err "Invalid HTTP status from $url"
return 1
fi
if (( 10#$http_code < 200 || 10#$http_code >= 300 )); then
err "HTTP $http_code from $url"
printf '%s\n' "$body" >&2
return 1
fi
if ! printf '%s\n' "$body" | jq -e '.' >/dev/null 2>&1; then
err "Non-JSON response from $url"
printf '%s\n' "$body" >&2
return 1
fi
local status
status="$(printf '%s\n' "$body" | jq -r '.status // empty')"
if [[ -n "$status" && "$status" != "ok" ]]; then
err "API status=$status"
printf '%s\n' "$body" >&2
return 1
fi
printf '%s\n' "$body"
}
curl_upload() {
# curl_upload URL FILE [folderId]
local url="$1"
local file="$2"
local folder_id="${3-}"
local progress="${4-0}"
local token="${5-}" # explicit token (may be empty)
[[ -f "$file" ]] || die "File not found: $file"
local args=(
--connect-timeout "$CURL_CONNECT_TIMEOUT"
--max-time "$CURL_MAX_TIME"
-X POST
-H "Accept: application/json"
-H "User-Agent: gofile-cli/$VERSION"
)
if [[ -n "$token" ]]; then
args+=(-H "Authorization: Bearer $token")
fi
if [[ "$progress" -eq 1 ]]; then
args+=(--progress-bar)
else
args+=(-sS)
fi
local escaped_file
escaped_file="$(curl_form_escape_path "$file")"
args+=(-F "file=@\"${escaped_file}\"")
if [[ -n "$folder_id" ]]; then
args+=(-F "folderId=$folder_id")
fi
debug "curl_upload $url file=$file folder=$folder_id auth=$([[ -n "$token" ]] && echo yes || echo no)"
local resp http_code body
if ! resp="$(curl "${args[@]}" "$url" -w $'\n%{http_code}')"; then
err "Upload failed: $file"
return 1
fi
http_code="${resp##*$'\n'}"
body="${resp%$'\n'*}"
if [[ ! "$http_code" =~ ^[0-9]{3}$ ]]; then
err "Invalid HTTP status from $url"
return 1
fi
if (( 10#$http_code < 200 || 10#$http_code >= 300 )); then
err "HTTP $http_code from $url"
printf '%s\n' "$body" >&2
return 1
fi
if ! printf '%s\n' "$body" | jq -e '.' >/dev/null 2>&1; then
err "Non-JSON response from upload endpoint"
printf '%s\n' "$body" >&2
return 1
fi
local status
status="$(printf '%s\n' "$body" | jq -r '.status // empty')"
if [[ -n "$status" && "$status" != "ok" ]]; then
err "API status=$status"
printf '%s\n' "$body" >&2
return 1
fi
printf '%s\n' "$body"
}
# --------------------------------------------------
# Usage
# --------------------------------------------------
usage() {
cat <<EOF
gofile.sh v$VERSION
Usage:
$0 [global options] <command> [args...]
Global options:
--token <TOKEN> Use TOKEN for this invocation (overrides config/env)
--guest Do NOT send Authorization header (guest mode) for this invocation
-v, --verbose Verbose output (repeat for more)
--raw Print raw JSON (no jq formatting)
--compact Print compact JSON (jq -c)
--version Show version
-h, --help Show help
Token management:
set-token <TOKEN> Save token to ${CONFIG_FILE}
show-token Show token (masked)
unset-token Remove token from config
hash-password <PASSWORD> Print sha256(password) hex (for --password-hash usage)
Server info:
get-servers [zone] Show available servers (zone optional: eu|na)
Upload:
upload <file...> [--folder <folderIdOrUrl>] [--region <region>] [--zone eu|na] [--server <storeX>] [--url <uploadUrl>] [--allow-custom-auth] [--progress] [--json]
- Default is official global upload endpoint: ${UPLOAD_URL_DEFAULT}
- Regions: eu-par, na-phx, ap-sgp, ap-hkg, ap-tyo, sa-sao
- --zone eu|na is a compatibility alias for eu-par|na-phx.
- --server uses the legacy https://<server>.gofile.io/contents/uploadfile endpoint.
- --allow-custom-auth explicitly permits sending your token to a non-Gofile --url.
- If uploading multiple files without --folder, it will reuse the folder created/used by the first upload.
Content:
get-content <folderIdOrUrl> [--password <plain>] [--password-hash <sha256hex>]
search-content <folderIdOrUrl> <query> [--password <plain>] [--password-hash <sha256hex>]
ls <folderIdOrUrl> [--password <plain>] [--password-hash <sha256hex>] [--json]
List children in a folder
create-folder <parentFolderIdOrUrl> [name] [--public true|false]
update-content <contentIdOrUrl> <attribute> <value>
attributes: name, description, tags, public, expiry, password
delete-content <contentIdsCsv>
Direct links:
create-direct-link <contentIdOrUrl> [expireTime] [ipsCsv] [domainsAllowedCsv] [authCsv] [domainsBlockedCsv]
update-direct-link <contentIdOrUrl> <directLinkId> [expireTime] [ipsCsv] [domainsAllowedCsv] [authCsv] [domainsBlockedCsv]
delete-direct-link <contentIdOrUrl> <directLinkId>
Moves / copies:
copy-content <contentsIdCsv> <destFolderIdOrUrl> [--password <plain>] [--password-hash <sha256hex>]
move-content <contentsIdCsv> <destFolderIdOrUrl>
import-content <contentsIdCsv> [--password <plain>] [--password-hash <sha256hex>]
Account:
get-account-id
get-account <accountId>
whoami Prints account id + root folder id (if available)
reset-token <accountId>
Examples:
$0 set-token abc123
$0 upload ./video.mp4 --progress
$0 upload ./a.bin ./b.bin --json
$0 get-servers eu
$0 ls https://gofile.io/d/AbCdEf
$0 search-content <folderId> "music files"
$0 create-folder <parentId> "My Folder" --public true
EOF
}
# --------------------------------------------------
# Commands
# --------------------------------------------------
cmd_show_token() {
if [[ "$NO_AUTH" -eq 1 ]]; then
say "Authentication is disabled for this invocation (--guest)."
return 0
fi
local t
t="$(get_token)"
if [[ -z "$t" ]]; then
say "No API token set."
else
say "Token: $(mask_token "$t")"
if [[ -n "$CLI_TOKEN" ]]; then
say "Source: --token"
elif [[ -n "${GOFILE_TOKEN:-}" ]]; then
say "Source: GOFILE_TOKEN"
else
say "Source: $CONFIG_FILE"
fi
fi
}
cmd_get_servers() {
# /servers is public (token not required), but we still include Authorization if available.
[[ $# -le 1 ]] || die "Usage: $0 get-servers [zone]"
local zone="${1-}"
if [[ -n "$zone" ]]; then
[[ "$zone" == "eu" || "$zone" == "na" ]] || die "zone must be eu or na"
curl_json_get_urlencoded "${API_BASE}/servers" zone "$zone" | print_json
else
curl_json "GET" "${API_BASE}/servers" | print_json
fi
}
cmd_upload() {
# upload <file...> [--folder <folderId>] [--region <region>] [--zone eu|na]
# [--server storeX] [--url <uploadUrl>] [--progress] [--json]
local folder_id=""
local folder_was_set=0
local region=""
local zone=""
local server=""
local upload_url="$UPLOAD_URL_DEFAULT"
local endpoint_options=0
local allow_custom_auth=0
local progress=0
local as_json=0
local -a args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--folder|-f)
require_value "$1" "$#"
folder_id="$2"
folder_was_set=1
shift 2
;;
--region)
require_value "$1" "$#"
region="$2"
endpoint_options=$((endpoint_options + 1))
shift 2
;;
--zone)
require_value "$1" "$#"
zone="$2"
endpoint_options=$((endpoint_options + 1))
shift 2
;;
--server)
require_value "$1" "$#"
server="$2"
endpoint_options=$((endpoint_options + 1))
shift 2
;;
--url)
require_value "$1" "$#"
upload_url="$2"
endpoint_options=$((endpoint_options + 1))
shift 2
;;
--allow-custom-auth) allow_custom_auth=1; shift ;;
--progress) progress=1; shift ;;
--json) as_json=1; shift ;;
--) shift; break ;;
-h|--help) usage; exit 0 ;;
-*) die "Unknown upload option: $1" ;;
*) args+=("$1"); shift ;;
esac
done
while [[ $# -gt 0 ]]; do args+=("$1"); shift; done
[[ ${#args[@]} -gt 0 ]] || die "Usage: $0 upload <file...> [--folder <folderIdOrUrl>] [--region <region>] [--zone eu|na]"
(( endpoint_options <= 1 )) || die "Use only one of --region, --zone, --server, or --url"
if [[ "$folder_was_set" -eq 1 ]]; then
[[ -n "$folder_id" ]] || die "--folder requires a non-empty folder ID"
folder_id="$(extract_id "$folder_id")"
fi
if [[ -n "$region" ]]; then
case "$region" in
eu-par|na-phx|ap-sgp|ap-hkg|ap-tyo|sa-sao) ;;
*) die "Unknown region: $region (expected eu-par, na-phx, ap-sgp, ap-hkg, ap-tyo, or sa-sao)" ;;
esac
upload_url="https://upload-${region}.gofile.io/uploadfile"
elif [[ -n "$zone" ]]; then
case "$zone" in
eu) upload_url="https://upload-eu-par.gofile.io/uploadfile" ;;
na) upload_url="https://upload-na-phx.gofile.io/uploadfile" ;;
*) die "zone must be eu or na" ;;
esac
elif [[ -n "$server" ]]; then
[[ "$server" =~ ^[[:alnum:]-]+$ ]] || die "Invalid server name: $server"
upload_url="https://${server}.gofile.io/contents/uploadfile"
fi
[[ "$upload_url" =~ ^https:// ]] || die "Upload URL must use HTTPS"
local f
for f in "${args[@]}"; do
[[ -f "$f" ]] || die "File not found: $f"
[[ -r "$f" ]] || die "File is not readable: $f"
done
local token
token="$(get_token)"
if [[ -n "$token" && "$allow_custom_auth" -eq 0 ]] && ! is_official_gofile_url "$upload_url"; then
die "Refusing to send your token to a non-Gofile URL. Use --guest or explicitly pass --allow-custom-auth."
fi
local session_token="$token"
local used_folder="$folder_id"
local -a responses=()
for f in "${args[@]}"; do
local resp
resp="$(curl_upload "$upload_url" "$f" "$used_folder" "$progress" "$session_token")"
if [[ "$as_json" -eq 1 ]]; then
responses+=("$resp")
else
local file_name download_page file_id
file_name="$(printf '%s\n' "$resp" | jq -r '.data.fileName // .data.name // empty')"
download_page="$(printf '%s\n' "$resp" | jq -r '.data.downloadPage // empty')"
file_id="$(printf '%s\n' "$resp" | jq -r '.data.fileId // empty')"
[[ -n "$file_name" ]] || file_name="$(basename "$f")"
say "✅ Uploaded: $file_name"
[[ -n "$download_page" ]] && say " 🔗 $download_page"
[[ -n "$file_id" ]] && say " id: $file_id"
fi
if [[ -z "$folder_id" ]]; then
local pf
pf="$(printf '%s\n' "$resp" | jq -r '.data.parentFolder // .data.folderId // empty')"
if [[ -n "$pf" ]]; then
used_folder="$pf"
fi
fi
if [[ -z "$token" ]]; then
local gt
gt="$(printf '%s\n' "$resp" | jq -r '.data.guestToken // .data.token // empty')"
if [[ -n "$gt" ]]; then
session_token="$gt"
fi
fi
done
if [[ "$as_json" -eq 1 ]]; then
printf '%s\n' "${responses[@]}" | jq -s '.' | print_json
fi
}
cmd_upload_file_compat() {
# Backwards compatible with your original:
# upload-file <filePath> [folderId] [zoneOrServer]
[[ $# -le 3 ]] || die "Usage: $0 upload-file <filePath> [folderId] [zoneOrServer]"
local filePath="${1-}"
local folderId="${2-}"
local zoneOrServer="${3-}"
[[ -n "$filePath" ]] || die "Usage: $0 upload-file <filePath> [folderId] [zoneOrServer]"
# Smart fix: allow "upload-file file eu" (zone w/out folder)
if [[ -z "$zoneOrServer" && ( "$folderId" == "eu" || "$folderId" == "na" ) ]]; then
zoneOrServer="$folderId"
folderId=""
fi
local -a upload_args=("$filePath")
if [[ -n "$folderId" ]]; then
upload_args+=(--folder "$folderId")
fi
if [[ -n "$zoneOrServer" && ( "$zoneOrServer" == "eu" || "$zoneOrServer" == "na" ) ]]; then
cmd_upload "${upload_args[@]}" --zone "$zoneOrServer" --json
elif [[ -n "$zoneOrServer" ]]; then
cmd_upload "${upload_args[@]}" --server "$zoneOrServer" --json
else
cmd_upload "${upload_args[@]}" --json
fi
}
cmd_create_folder() {
# create-folder <parentFolderId> [folderName] [--public true|false]
local parent="${1-}"
[[ -n "$parent" ]] || die "Usage: $0 create-folder <parentFolderId> [folderName] [--public true|false]"
shift
parent="$(extract_id "$parent")"
local name=""
local public_val=""
while [[ $# -gt 0 ]]; do
case "$1" in
--public)
require_value "$1" "$#"
public_val="$2"
shift 2
;;
--)
shift
while [[ $# -gt 0 ]]; do
[[ -z "$name" ]] || die "Only one folder name may be specified"
name="$1"
shift
done
;;
-*) die "Unknown create-folder option: $1" ;;
*)
[[ -z "$name" ]] || die "Only one folder name may be specified"
name="$1"
shift
;;
esac
done
if [[ -n "$public_val" && "$public_val" != "true" && "$public_val" != "false" ]]; then
die "--public must be true or false"
fi
require_token
local payload
if [[ -n "$name" ]]; then
payload="$(jq -n --arg pid "$parent" --arg fn "$name" '{ parentFolderId: $pid, folderName: $fn }')"
else
payload="$(jq -n --arg pid "$parent" '{ parentFolderId: $pid }')"
fi
if [[ -n "$public_val" ]]; then
payload="$(printf '%s\n' "$payload" | jq --argjson p "$public_val" '. + {public: $p}')"
fi
curl_json "POST" "${API_BASE}/contents/createFolder" "$payload" | print_json
}
cmd_update_content() {
# update-content <contentId> <attribute> <value>
require_exact_args 3 "update-content <contentId> <attribute> <value>" "$#"
local cid="$1"
local attr="$2"
local val="$3"
cid="$(extract_id "$cid")"
[[ -n "$cid" ]] || die "Content ID must not be empty"
case "$attr" in
name|description|tags|public|expiry|password) ;;
*) die "Unsupported attribute: $attr (expected name, description, tags, public, expiry, or password)" ;;
esac
local payload
case "$attr" in
expiry)
# numeric unix timestamp
if [[ "$val" =~ ^[0-9]+$ ]]; then
payload="$(jq -n --arg a "$attr" --argjson v "$val" '{attribute:$a, attributeValue:$v}')"
else
die "expiry must be a unix timestamp integer"
fi
;;
public)
# API expects booleans as string "true"/"false" for update-content in many clients
if [[ "$val" == "true" || "$val" == "false" ]]; then
payload="$(jq -n --arg a "$attr" --arg v "$val" '{attribute:$a, attributeValue:$v}')"
else
die "public must be true or false"
fi
;;
*)
payload="$(jq -n --arg a "$attr" --arg v "$val" '{attribute:$a, attributeValue:$v}')"
;;
esac
require_token
curl_json "PUT" "${API_BASE}/contents/${cid}/update" "$payload" | print_json
}
cmd_delete_content() {
require_exact_args 1 "delete-content <contentIdsCsv>" "$#"
local ids="$1"
[[ -n "$ids" ]] || die "Content IDs must not be empty"
require_token
local payload
payload="$(jq -n --arg cid "$ids" '{ contentsId: $cid }')"
curl_json "DELETE" "${API_BASE}/contents" "$payload" | print_json
}
cmd_get_content() {
# get-content <contentId> [--password <plain>] [--password-hash <hash>]
local cid="${1-}"
[[ -n "$cid" ]] || die "Usage: $0 get-content <contentIdOrUrl> [--password <plain>] [--password-hash <sha256hex>]"
cid="$(extract_id "$cid")"
shift
local pw_plain=""
local pw_hash=""
while [[ $# -gt 0 ]]; do
case "$1" in
--password)
require_value "$1" "$#"
pw_plain="$2"
shift 2
;;
--password-hash)
require_value "$1" "$#"
pw_hash="$2"
shift 2
;;
*) die "Unknown get-content option: $1" ;;
esac
done
[[ -z "$pw_plain" || -z "$pw_hash" ]] || die "Use either --password or --password-hash, not both"
if [[ -n "$pw_plain" && -z "$pw_hash" ]]; then
pw_hash="$(sha256_hex "$pw_plain")"
fi
[[ -z "$pw_hash" ]] || validate_password_hash "$pw_hash"
require_token
if [[ -n "$pw_hash" ]]; then
curl_json_get_urlencoded "${API_BASE}/contents/${cid}" password "$pw_hash" | print_json
else
curl_json "GET" "${API_BASE}/contents/${cid}" | print_json
fi
}
cmd_search_content() {
# search-content <folderId> <query> [--password <plain>] [--password-hash <hash>]
local folder="${1-}"
local q="${2-}"
[[ -n "$folder" && -n "$q" ]] || die "Usage: $0 search-content <folderIdOrUrl> <query> [--password <plain>] [--password-hash <sha256hex>]"
folder="$(extract_id "$folder")"
shift 2
local pw_plain=""
local pw_hash=""
while [[ $# -gt 0 ]]; do
case "$1" in
--password)
require_value "$1" "$#"
pw_plain="$2"
shift 2
;;
--password-hash)
require_value "$1" "$#"
pw_hash="$2"
shift 2
;;
*) die "Unknown search-content option: $1" ;;
esac
done
[[ -z "$pw_plain" || -z "$pw_hash" ]] || die "Use either --password or --password-hash, not both"
if [[ -n "$pw_plain" && -z "$pw_hash" ]]; then
pw_hash="$(sha256_hex "$pw_plain")"
fi
[[ -z "$pw_hash" ]] || validate_password_hash "$pw_hash"
require_token
if [[ -n "$pw_hash" ]]; then
curl_json_get_urlencoded "${API_BASE}/contents/search" contentId "$folder" searchedString "$q" password "$pw_hash" | print_json
else
curl_json_get_urlencoded "${API_BASE}/contents/search" contentId "$folder" searchedString "$q" | print_json
fi
}
cmd_ls() {
# ls <folderIdOrUrl> [--password <plain>] [--password-hash <hash>] [--json]
local folder="${1-}"
[[ -n "$folder" ]] || die "Usage: $0 ls <folderIdOrUrl> [--password <plain>] [--password-hash <sha256hex>] [--json]"
folder="$(extract_id "$folder")"
shift
local as_json=0
local pw_plain=""
local pw_hash=""
while [[ $# -gt 0 ]]; do
case "$1" in
--json) as_json=1; shift ;;
--password)
require_value "$1" "$#"
pw_plain="$2"
shift 2
;;
--password-hash)
require_value "$1" "$#"
pw_hash="$2"
shift 2
;;
*) die "Unknown ls option: $1" ;;
esac
done
[[ -z "$pw_plain" || -z "$pw_hash" ]] || die "Use either --password or --password-hash, not both"
if [[ -n "$pw_plain" ]]; then
pw_hash="$(sha256_hex "$pw_plain")"
fi
[[ -z "$pw_hash" ]] || validate_password_hash "$pw_hash"
local resp
require_token
if [[ -n "$pw_hash" ]]; then
resp="$(curl_json_get_urlencoded "${API_BASE}/contents/${folder}" password "$pw_hash")"
else
resp="$(curl_json "GET" "${API_BASE}/contents/${folder}")"
fi
if [[ "$as_json" -eq 1 ]]; then
printf '%s\n' "$resp" | print_json
return 0
fi
# Human-friendly listing (best-effort: works when API returns .data.children as a map)
printf '%-6s %-28s %s\n' "TYPE" "ID" "NAME"
printf '%-6s %-28s %s\n' "----" "--" "----"
echo "$resp" | jq -r '
(.data.children // {})
| to_entries
| sort_by(.value.type, (.value.name // ""))
| .[]
| .value
| [.type, .id, .name]
| @tsv
' | while IFS=$'\t' read -r t id name; do
printf '%-6s %-28s %s\n' "$t" "$id" "$name"
done
}
# Direct links helpers
csv_to_json_array() {
# prints JSON array from comma-separated string
local csv="$1"
local -a arr=()
IFS=',' read -r -a arr <<< "$csv"
printf '%s\n' "${arr[@]}" | jq -R . | jq -s '.'
}
cmd_create_direct_link() {
# create-direct-link <contentId> [expireTime] [ipsCsv] [domainsAllowedCsv] [authCsv] [domainsBlockedCsv]
[[ $# -le 6 ]] || die "Usage: $0 create-direct-link <contentId> [expireTime] [ipsCsv] [domainsAllowedCsv] [authCsv] [domainsBlockedCsv]"
local cid="${1-}"; cid="$(extract_id "${cid:-}")"
[[ -n "$cid" ]] || die "Usage: $0 create-direct-link <contentId> [expireTime] [ipsCsv] [domainsAllowedCsv] [authCsv] [domainsBlockedCsv]"
local expireTime="${2-}"
local sourceIpsAllowed="${3-}"
local domainsAllowed="${4-}"
local auth="${5-}"
local domainsBlocked="${6-}"
local payload="{}"
if [[ -n "$expireTime" ]]; then
[[ "$expireTime" =~ ^[0-9]+$ ]] || die "expireTime must be a unix timestamp integer"
payload="$(echo "$payload" | jq --arg et "$expireTime" '. + {expireTime: ($et|tonumber)}')"