-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupload.zsh
executable file
·48 lines (40 loc) · 1.35 KB
/
upload.zsh
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
#!/bin/zsh
PATH=$PATH:/bin/:/usr/bin:/usr/local/bin
file=${1}
filename=$(echo $file | sed -E "s/.+\///")
fileextension=$(echo $file | sed -E "s/.+\.//")
name=${2}
IFS=' ' read -r tags <<< ${3}
description=${4}
mime=${5:-image/$fileextension}
canister=${6:-charlie}
network=${7:-local}
threshold=${8:-250000}
byteSize=${#$(od -An -v -tuC $file)[@]}
b="\e[1A\e[K"
log_line="Uploading $name $filename ($(( $byteSize / 1024 ))kb ($mime))"
echo "$log_line"
echo "$log_line ...Emptying buffer"
dfx canister --network $network call $canister uploadClear >> ./zsh/upload_log.txt
echo "$log_line ...Uploading"
i=0
while [ $i -le $byteSize ]; do
echo "$log_line ...Uploading #$(($i/$threshold+1))/$(($byteSize/$threshold+1))"
dfx canister --network $network call $canister upload "( vec {\
vec { $(for byte in ${(j:;:)$(od -An -v -tuC $file)[@]:$i:$threshold}; echo "$byte;") };\
})" >> ./zsh/upload_log.txt
i=$(($i+$threshold))
done
echo "$log_line ...Finalizing"
tags=$(echo $tags | sed -e 's/^"//' -e 's/"$//')
tags=(${(@s/ /)tags})
dfx canister --network $network call $canister uploadFinalize "(\
\"\",\
record {\
\"name\" = $name;\
\"filename\" = \"$filename\";\
\"tags\" = vec { $(for tag in $tags; echo "\"$tag\";") };\
\"description\" = $description;\
}\
)" >> ./zsh/upload_log.txt
echo "$log_line ...OK"