-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplitdl.sh
executable file
·184 lines (152 loc) · 5.23 KB
/
splitdl.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
#!/bin/bash
# ----------------------------------------------------------------------------#
# Copyright (c) 2011 Jason Kit #
# #
# Permission is hereby granted, free of charge, to any person obtaining #
# a copy of this software and associated documentation files (the "Software"),#
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# ----------------------------------------------------------------------------#
# -----------------------------------------------------------------------------
# Pring Usage and exit
# -----------------------------------------------------------------------------
function print_usage {
echo "Usgae: splitdl <url> <number of conntection> [output file path]"
exit
}
# -----------------------------------------------------------------------------
# Update console to print the progress bar
# Use following global variable:
# row, part_prefix, chunk_size, last_chunk_size, length, m
# -----------------------------------------------------------------------------
function print_progress {
totalsize=0
# length of progress bar
div=50
i=1
while [ "$i" -le "$m" ]
do
part_size=`stat -f %z $part_prefix$i`
totalsize=`expr $totalsize + $part_size`
trow=`echo "$row - $m - 2 + $i" | bc`
tput cup $trow 0
if [ "$i" -lt "$m" ]; then
progress=`echo "$part_size*100/$chunk_size" | bc`
else
progress=`echo "$part_size*100/$last_chunk_size" | bc`
fi
nbar=`echo "$div*$progress/100" | bc`
if [ "$nbar" -eq "0" ]; then
nbar=1
fi
cnbar=`expr $div - $nbar`
progress_bar=`printf "%${nbar}s" "#" | sed "s/ /#/g"`
progress_bar=`printf "$progress_bar%${cnbar}s" ""`
printf "[%04d] %s [ %3d%% ]\n" $i "$progress_bar" $progress
i=`expr $i + 1`
done
echo "Total: $totalsize/$length [ `echo "$totalsize*100/$length" | bc`%]"
}
#Target URL
url=$1
#Number of Split
m=$2
#Output file path
o=$3
prefix_file=`mktemp /tmp/splitdl.XXXXXX`
part_prefix="$header_file.part."
# Actually we don't need the prefix_file, we just use mktemp to generate unique file name
rm $prefix_file
if [ "$#" -lt 2 ]; then
print_usage
fi
if `echo $m | grep -q [^[:digit:]]`; then
print_usage
fi
if [ "$o" == "" ]; then
o=`basename $url`
fi
# Obtain Remote file size
length=`curl -Is $url |
grep "Content-Length:" |
sed "s/Content-Length: //g" | tr -d "\n\r"`
if [ "$length" == "" ]; then
echo "Cannot resolve remte file size."
exit
fi
# Calculate chunk size
chunk_size=`expr $length / $m`
# Last chunk size may be larger
last_chunk_size=`echo "$length - ($chunk_size*($m-1))" | bc`
# Print the download information
echo "From: $url"
echo "Download To: $o"
echo "Connection: $m"
echo "Chunk Size: $chunk_size"
# Confirm Download
while [ "1" == "1" ]
do
echo "Confirm download? (Y/N)"
read confirm
if [ "$confirm" == "Y" ]; then
break
elif [ "$confirm" == "N" ]; then
exit
fi
done
# For merging part files
part_file_list=""
# spwan curl progress for each part
i=1
while [ "$i" -le "$m" ]
do
touch "$part_prefix$i"
printf "[%04d]\n" $i
head=`echo "($i-1)*$chunk_size" | bc`
if [ "$i" -lt "$m" ]; then
part_file_list="$part_file_list$part_prefix$i "
end=`echo "$i*$chunk_size-1" | bc`
curl --range $head-$end -s -o $part_prefix$i $url &
else
# handle the last chunk
part_file_list="$part_file_list$part_prefix$i"
curl --range $head- -s -o $part_prefix$i $url &
fi
i=`expr $i + 1`
done
# Record the current cursor postion for displaying progress bar
echo -en "\033[6n"
read -sdR CURPOS
row=`echo ${CURPOS#*[} | sed "s/;.*//g"`
# Here if jobslist is clear, all the background curl process is done
# Not using wait is because we need to update progress bar
jobslist=`jobs`
while [ "$jobslist" != "" ]
do
sleep 1
# seems we need to call jobs to flush the jobs list so that jobslist
# can obtain empty string
jobs > /dev/null
jobslist=`jobs`
print_progress
done
# Print progress bar once more to make sure everything show 100%
print_progress
# Merge each part to the result output file
cat $part_file_list > $o
# Clean up
rm $part_prefix*
echo "Finish Download"