-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathespUploadFile
More file actions
executable file
·117 lines (104 loc) · 3.8 KB
/
espUploadFile
File metadata and controls
executable file
·117 lines (104 loc) · 3.8 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
#!/bin/bash
if [ $# -le 3 ]; then
if [ $# -lt 2 ]; then
echo "$0: usage: $0 <file> <device ip address> [--verbose]"
exit 1
fi
if [ "$3" = "--verbose" ]; then
set -x
fi
else
echo "$0: usage: $0 <file> <device ip address> [--verbose]"
exit 1
fi
device_ip=$2
filename=$1
delay=1
extension="${filename##*.}"
if [ "$extension" = "html" ]; then
# file size is compressed removing:
# + blank spaces in front of the lines
# + html comments
# + new line characters
cat $filename | sed 's/^\ *//' | sed 's/<!--.*-->//' | tr -d '\n' > working_file
# state the file version using git and add it to the beginning of the file
version=`git --no-pager describe --tags --always --dirty`
echo "<!--$version-->" | cat - working_file > temp && mv temp working_file
elif [ "$extension" = "js" ]; then
# compress file size:
# + removing blank spaces in front of the lines
# + removing js comments
# + replacing new line characters with blank spaces
cat $filename | sed 's/^\ *//' | sed 's/^\/\/.*//' | tr '\n' ' ' > working_file
# state the file version using git and add it to the beginning of the file
version=`git --no-pager describe --tags --always --dirty`
echo "//$version" | cat - working_file > temp && mv temp working_file
else
# don't change the file!
cp $filename working_file
fi
wc -c working_file
# split the file
split -b1024 -d working_file
# The nullglob option causes the array to be empty if there are no matches.
# shopt -s nullglob
filelist=( x* )
#echo ${#filelist[@]} # will echo number of elements in array
#echo "${filelist[@]}" # will dump all elements of the array
# upload the file pieces to the device
# curl -i --location --request DELETE "http://$device_ip/api/file/$filename"
first_time=true
for file in "${filelist[@]}"; do
if [ "$first_time" = true ] ; then
# FIRST FILE PIECE
# first delete any occurrence of filename
# and create it using the first file piece
first_time=false
if [ "$3" = "--verbose" ]; then
curl -i --location --request DELETE "http://$device_ip/api/file/$filename"
else
echo "deleting old $1 ..."
status_code=$(curl --write-out "%{http_code}\n" --silent --output /dev/null --request DELETE "http://$device_ip/api/file/$filename")
if [ $status_code -ne 200 ] ; then
echo "there was no old $1"
else
echo "done!"
fi
fi
echo "awaiting..."
# sleep $delay
if [ "$3" = "--verbose" ]; then
curl -i --location --request POST "http://$device_ip/api/file/$filename" --header "Content-Type: text/html" --data-binary "@$file"
else
echo "sending file piece ..."
status_code=$(curl --write-out "%{http_code}\n" --silent --output /dev/null --request POST "http://$device_ip/api/file/$filename" --header "Content-Type: text/html" --data-binary "@$file")
if [ $status_code -ne 201 ] ; then
echo "error while sending file, received $status_code"
exit 1
else
echo "done!"
fi
fi
rm $file
else
# FOLLOWING FILE PIECES
# append following file pieces to filename
echo "awaiting..."
# sleep $delay
if [ "$3" = "--verbose" ]; then
curl -i --location --request PUT "http://$device_ip/api/file/$filename" --header "Content-Type: text/html" --data-binary "@$file"
else
echo "sending file piece ..."
status_code=$(curl --write-out "%{http_code}\n" --silent --output /dev/null --request PUT "http://$device_ip/api/file/$filename" --header "Content-Type: text/html" --data-binary "@$file")
if [ $status_code -ne 200 ] ; then
echo "error while sending file, received $status_code"
exit 1
else
echo "done!"
fi
fi
rm $file
fi
done
echo "file sent!"
rm working_file