-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzzheavier.sh
749 lines (637 loc) · 17.9 KB
/
buzzheavier.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
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
#!/usr/bin/env bash
#
# buzzheavier.sh: An extended CLI script to interact with Buzzheavier.com
#
# Dependencies:
# - curl
# - jq (optional, for pretty JSON; remove/replace if not available)
# - base64 (or openssl for base64 fallback)
# - bash 4+ (for arrays in bulk operations)
#
########################################
# Configuration & Global Variables
########################################
API_BASE="https://buzzheavier.com/api"
FILE_UPLOAD_BASE="https://w.buzzheavier.com"
CONFIG_FILE="${HOME}/.config/buzzheavier-cli/config"
# If a user calls a command that requires token-based auth,
# and they haven't provided a token, we will try to load from $CONFIG_FILE.
ACCOUNT_ID="" # Will be loaded from config, if present.
########################################
# Helper: usage instructions
########################################
function usage() {
cat <<EOF
Usage: $(basename "$0") <command> [arguments...]
Commands:
#--------------------#
# Config Management #
#--------------------#
set-token <token>
Saves <token> to config so you don't have to provide it every time.
#--------------------------#
# Interactive Mode #
#--------------------------#
interactive
Launch an interactive menu-driven mode.
#----------------------------#
# File Upload Endpoints #
#----------------------------#
upload-anon <filePath> <fileName>
Upload <filePath> anonymously as <fileName>.
upload-auth <filePath> <parentId> <fileName> [token]
Upload <filePath> to a user directory (parentId). Token can be given or read from config.
upload-loc <filePath> <fileName> <locationId>
Upload <filePath> to a specific locationId.
upload-note <filePath> <fileName> <noteString>
Upload <filePath> with a text note (up to 500 chars). Script base64-encodes the note.
bulk-upload <parentId> <file1> [file2] [file3] ...
Upload multiple files to user directory <parentId> in one command.
#--------------------#
# Public / Account #
#--------------------#
locations
Get file storage locations (public).
account [token]
Retrieve authenticated account info. Token can be passed or read from config.
#----------------------#
# File Manager #
#----------------------#
get-root [token]
Lists the contents of the root directory.
get-dir <directoryId> [token]
Lists the contents of <directoryId>.
create-dir <name> <parentId> [token]
Create a new directory named <name> under <parentId>.
rename-dir <directoryId> <newName> [token]
Rename directory <directoryId> to <newName>.
move-dir <directoryId> <newParentId> [token]
Move directory <directoryId> under <newParentId>.
rename-file <fileId> <newName> [token]
Rename file <fileId> to <newName>.
move-file <fileId> <newParentId> [token]
Move file <fileId> under <newParentId>.
add-note-file <fileId> <noteString> [token]
Add/change note on <fileId>.
delete-dir <directoryId> [token]
Delete directory <directoryId> (including subdirectories).
bulk-delete <dirId1> [dirId2] [dirId3] ...
Delete multiple directories in one go.
Examples:
$(basename "$0") set-token "YOUR_ACCOUNT_ID"
$(basename "$0") interactive
$(basename "$0") upload-anon ./myvideo.mp4 myvideo.mp4
$(basename "$0") upload-auth ./myvideo.mp4 parent123 myvideo.mp4
$(basename "$0") upload-note ./myvideo.mp4 myvideo.mp4 "Hello from Buzzheavier!"
$(basename "$0") bulk-upload parent123 ./file1.mp4 ./file2.mp4
$(basename "$0") locations
$(basename "$0") account
$(basename "$0") get-root
EOF
}
########################################
# Load token from config if it exists
########################################
function load_token_from_config() {
if [[ -f "$CONFIG_FILE" ]]; then
# shellcheck disable=SC1090
source "$CONFIG_FILE" 2>/dev/null
fi
}
########################################
# Set token command
########################################
function set_token() {
local newToken="$1"
if [[ -z "$newToken" ]]; then
echo "Error: You must provide a token."
exit 1
fi
mkdir -p "$(dirname "$CONFIG_FILE")"
echo "ACCOUNT_ID=\"$newToken\"" > "$CONFIG_FILE"
echo "Token saved to $CONFIG_FILE"
}
########################################
# Helper to get token
########################################
function get_token_argument_or_config() {
local passedToken="$1"
if [[ -n "$passedToken" ]]; then
echo "$passedToken"
else
if [[ -z "$ACCOUNT_ID" ]]; then
echo "Error: No token provided and none found in $CONFIG_FILE. Please run:"
echo " $(basename "$0") set-token <your_token>"
exit 1
fi
echo "$ACCOUNT_ID"
fi
}
########################################
# Enhanced Upload (Progress Bar)
########################################
function enhanced_curl_put() {
# Usage: enhanced_curl_put <filePath> <URL> [<headers...>]
# This wrapper uses a more verbose progress bar for uploading.
local filePath="$1"
local url="$2"
shift 2
local headers=("$@")
# We'll capture the JSON response in a variable to parse it afterwards.
local response
response=$(curl --progress-bar -s -T "$filePath" "${headers[@]}" "$url")
# Echo the raw JSON back to stdout so calling functions can parse it.
echo "$response"
}
########################################
# File Upload Functions
########################################
# 1) Anonymous file upload
function upload_anon() {
local filePath="$1"
local fileName="$2"
if [[ ! -f "$filePath" ]]; then
echo "Error: file does not exist: $filePath"
exit 1
fi
echo "Uploading $filePath anonymously as $fileName ..."
local response
response=$(enhanced_curl_put "$filePath" \
"${FILE_UPLOAD_BASE}/${fileName}")
echo "Server Response:"
echo "$response"
# Extract the 'id' from JSON and display the share link
local id
id=$(echo "$response" | jq -r '.data.id // empty')
if [[ -n "$id" ]]; then
echo "Download link: https://buzzheavier.com/$id"
fi
echo ""
}
# 2) Authenticated file upload
function upload_auth() {
local filePath="$1"
local parentId="$2"
local fileName="$3"
local tokenArg="$4"
if [[ ! -f "$filePath" ]]; then
echo "Error: file does not exist: $filePath"
exit 1
fi
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Uploading $filePath to user directory $parentId as $fileName ..."
local response
response=$(enhanced_curl_put "$filePath" \
"${FILE_UPLOAD_BASE}/${parentId}/${fileName}" \
-H "Authorization: Bearer $token")
echo "Server Response:"
echo "$response"
local id
id=$(echo "$response" | jq -r '.data.id // empty')
if [[ -n "$id" ]]; then
echo "Download link: https://buzzheavier.com/$id"
fi
echo ""
}
# 3) Upload a file to a specific location
function upload_loc() {
local filePath="$1"
local fileName="$2"
local locationId="$3"
if [[ ! -f "$filePath" ]]; then
echo "Error: file does not exist: $filePath"
exit 1
fi
echo "Uploading $filePath to location $locationId as $fileName ..."
local response
response=$(enhanced_curl_put "$filePath" \
"${FILE_UPLOAD_BASE}/${fileName}?locationId=${locationId}")
echo "Server Response:"
echo "$response"
local id
id=$(echo "$response" | jq -r '.data.id // empty')
if [[ -n "$id" ]]; then
echo "Download link: https://buzzheavier.com/$id"
fi
echo ""
}
# 4) Upload a file with note
function upload_note() {
local filePath="$1"
local fileName="$2"
local noteString="$3"
if [[ ! -f "$filePath" ]]; then
echo "Error: file does not exist: $filePath"
exit 1
fi
local encodedNote
if command -v base64 >/dev/null 2>&1; then
encodedNote="$(echo -n "$noteString" | base64)"
else
encodedNote="$(openssl base64 -A <<< "$noteString")"
fi
echo "Uploading $filePath with note ..."
local response
response=$(enhanced_curl_put "$filePath" \
"${FILE_UPLOAD_BASE}/${fileName}?note=${encodedNote}")
echo "Server Response:"
echo "$response"
local id
id=$(echo "$response" | jq -r '.data.id // empty')
if [[ -n "$id" ]]; then
echo "Download link: https://buzzheavier.com/$id"
fi
echo ""
}
# Bulk upload multiple files into a user directory
function bulk_upload() {
local parentId="$1"
shift
if [[ $# -lt 1 ]]; then
echo "Usage: bulk-upload <parentId> <file1> [file2] ..."
exit 1
fi
# We'll attempt to use the token from config. No separate token argument here,
# but you can easily add [token] if needed.
local token
token="$(get_token_argument_or_config "")"
# For each file, we’ll keep the same file name (basename).
for filePath in "$@"; do
if [[ ! -f "$filePath" ]]; then
echo "Warning: $filePath does not exist, skipping..."
continue
fi
local fileName
fileName="$(basename "$filePath")"
echo "Bulk uploading $fileName to directory $parentId..."
local response
response=$(enhanced_curl_put "$filePath" \
"${FILE_UPLOAD_BASE}/${parentId}/${fileName}" \
-H "Authorization: Bearer $token")
echo "Server Response:"
echo "$response"
local id
id=$(echo "$response" | jq -r '.data.id // empty')
if [[ -n "$id" ]]; then
echo "Download link: https://buzzheavier.com/$id"
fi
echo ""
done
}
########################################
# Public / Account Info
########################################
# 5) Get file storage locations
function get_locations() {
echo "Fetching file storage locations..."
curl -s "${API_BASE}/locations" | jq .
}
# 6) Get account information
function get_account() {
local tokenArg="$1"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Fetching account info..."
curl -s \
-H "Authorization: Bearer $token" \
"${API_BASE}/account" \
| jq .
}
########################################
# File Manager
########################################
# 7) Get root directory
function get_root() {
local tokenArg="$1"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Listing root directory contents..."
curl -s \
-H "Authorization: Bearer $token" \
"${API_BASE}/fs" \
| jq .
}
# 8) Get directory
function get_directory() {
local directoryId="$1"
local tokenArg="$2"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Listing directory $directoryId..."
curl -s \
-H "Authorization: Bearer $token" \
"${API_BASE}/fs/${directoryId}" \
| jq .
}
# 9) Create directory
function create_directory() {
local name="$1"
local parentId="$2"
local tokenArg="$3"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Creating directory '$name' under parentId='$parentId'..."
curl -s \
-X POST \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "{\"name\": \"${name}\", \"parentId\": \"${parentId}\"}" \
"${API_BASE}/fs" \
| jq .
}
# 10) Rename directory
function rename_directory() {
local directoryId="$1"
local newName="$2"
local tokenArg="$3"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Renaming directory $directoryId to $newName..."
curl -s \
-X PATCH \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "{\"name\": \"${newName}\"}" \
"${API_BASE}/fs/${directoryId}" \
| jq .
}
# 11) Move directory
function move_directory() {
local directoryId="$1"
local newParentId="$2"
local tokenArg="$3"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Moving directory $directoryId to $newParentId..."
curl -s \
-X PUT \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "{\"parentId\": \"${newParentId}\"}" \
"${API_BASE}/fs/${directoryId}" \
| jq .
}
# 12) Rename file
function rename_file() {
local fileId="$1"
local newName="$2"
local tokenArg="$3"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Renaming file $fileId to $newName..."
curl -s \
-X PATCH \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "{\"name\": \"${newName}\"}" \
"${API_BASE}/fs/${fileId}" \
| jq .
}
# 13) Move file
function move_file() {
local fileId="$1"
local newParentId="$2"
local tokenArg="$3"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Moving file $fileId to $newParentId..."
curl -s \
-X PUT \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "{\"parentId\": \"${newParentId}\"}" \
"${API_BASE}/fs/${fileId}" \
| jq .
}
# 14) Add / change note for a file
function add_note_file() {
local fileId="$1"
local noteString="$2"
local tokenArg="$3"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Updating note for file $fileId..."
curl -s \
-X PUT \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "{\"note\": \"${noteString}\"}" \
"${API_BASE}/fs/${fileId}" \
| jq .
}
# 15) Delete directory
function delete_directory() {
local directoryId="$1"
local tokenArg="$2"
local token
token="$(get_token_argument_or_config "$tokenArg")"
echo "Deleting directory $directoryId..."
curl -s \
-X DELETE \
-H "Authorization: Bearer $token" \
"${API_BASE}/fs/${directoryId}" \
| jq .
}
# Bulk delete multiple directories
function bulk_delete_directories() {
if [[ $# -lt 1 ]]; then
echo "Usage: bulk-delete <dirId1> [dirId2] [dirId3] ..."
exit 1
fi
local token
token="$(get_token_argument_or_config "")"
for dirId in "$@"; do
echo "Deleting directory: $dirId"
curl -s \
-X DELETE \
-H "Authorization: Bearer $token" \
"${API_BASE}/fs/${dirId}" \
| jq .
done
}
########################################
# Interactive Menu
########################################
function interactive_menu() {
while true; do
echo
echo "========== Buzzheavier CLI Interactive Menu =========="
echo " 1) Set Token"
echo " 2) Show Account Info"
echo " 3) Upload File (Anon)"
echo " 4) Upload File (Auth)"
echo " 5) Bulk Upload (Auth)"
echo " 6) List Root Directory"
echo " 7) Create Directory"
echo " 8) Delete Directory"
echo " 9) Bulk Delete Directories"
echo "10) Get Storage Locations"
echo "11) Quit"
echo "======================================================"
read -rp "Choose an option (1-11): " choice
case "$choice" in
1)
echo -n "Enter new token: "
read -r newTok
set_token "$newTok"
;;
2)
get_account
;;
3)
echo -n "Enter file path: "
read -r anonPath
echo -n "Enter file name as stored: "
read -r anonName
upload_anon "$anonPath" "$anonName"
;;
4)
echo -n "Enter file path: "
read -r authPath
echo -n "Enter parentId: "
read -r pId
echo -n "Enter file name as stored: "
read -r fName
upload_auth "$authPath" "$pId" "$fName"
;;
5)
echo -n "Enter parentId for bulk upload: "
read -r bParent
echo -n "Enter paths for files to upload (space-separated): "
read -ra fileArray
bulk_upload "$bParent" "${fileArray[@]}"
;;
6)
get_root
;;
7)
echo -n "Enter new directory name: "
read -r newDirName
echo -n "Enter parentId: "
read -r cParent
create_directory "$newDirName" "$cParent"
;;
8)
echo -n "Enter directoryId to delete: "
read -r dId
delete_directory "$dId"
;;
9)
echo -n "Enter directoryIds to bulk-delete (space-separated): "
read -ra dirArray
bulk_delete_directories "${dirArray[@]}"
;;
10)
get_locations
;;
11)
echo "Exiting Interactive Mode."
break
;;
*)
echo "Invalid choice."
;;
esac
done
}
########################################
# Main
########################################
# 1) Load token from config if available
load_token_from_config
# 2) Handle user command
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
COMMAND="$1"
shift
case "$COMMAND" in
# Config
set-token)
# usage: set-token <token>
set_token "$@"
;;
# Interactive Mode
interactive)
interactive_menu
;;
# File Upload Endpoints
upload-anon)
# usage: upload-anon <filePath> <fileName>
upload_anon "$@"
;;
upload-auth)
# usage: upload-auth <filePath> <parentId> <fileName> [token]
upload_auth "$@"
;;
upload-loc)
# usage: upload-loc <filePath> <fileName> <locationId>
upload_loc "$@"
;;
upload-note)
# usage: upload-note <filePath> <fileName> <noteString>
upload_note "$@"
;;
bulk-upload)
# usage: bulk-upload <parentId> <file1> [file2] ...
bulk_upload "$@"
;;
# Public / Account
locations)
# usage: locations
get_locations
;;
account)
# usage: account [token]
get_account "$@"
;;
# File Manager
get-root)
# usage: get-root [token]
get_root "$@"
;;
get-dir)
# usage: get-dir <directoryId> [token]
get_directory "$@"
;;
create-dir)
# usage: create-dir <name> <parentId> [token]
create_directory "$@"
;;
rename-dir)
# usage: rename-dir <directoryId> <newName> [token]
rename_directory "$@"
;;
move-dir)
# usage: move-dir <directoryId> <newParentId> [token]
move_directory "$@"
;;
rename-file)
# usage: rename-file <fileId> <newName> [token]
rename_file "$@"
;;
move-file)
# usage: move-file <fileId> <newParentId> [token]
move_file "$@"
;;
add-note-file)
# usage: add-note-file <fileId> <noteString> [token]
add_note_file "$@"
;;
delete-dir)
# usage: delete-dir <directoryId> [token]
delete_directory "$@"
;;
bulk-delete)
# usage: bulk-delete <dirId1> [dirId2] [dirId3] ...
bulk_delete_directories "$@"
;;
*)
echo "Unknown command: $COMMAND"
usage
exit 1
;;
esac
exit 0