-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
290 lines (279 loc) · 12.6 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--<meta name="viewport" content="width=device-width,initial-scale=1">-->
<!--<link rel="shortcut icon" href="/favicon.ico">-->
<title>文件下载代理服务</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.main-container {
padding: 20px;
/*margin-top: 50px;*/
}
table {
table-layout: fixed;
}
td.complete_rate {
padding: 0;
}
td, th {
text-align: center;
word-wrap: break-word;
}
td.content_length, td.start_timestamp, td.speed {
display: none;
}
.progress-container {
overflow: hidden;
}
.progress-full {
width: 60px;
height: 60px;
border: 5px solid #5cb85c;
border-radius: 50%;
/*clip: rect(0px, 30px, 60px, 0);*/
position: absolute;
}
.progress-right {
width: 60px;
height: 60px;
border: 5px solid #5cb85c;
border-radius: 50%;
clip: rect(0px, 30px, 60px, 0);
transform: rotate(180deg);
position: absolute;
}
.progress-wrapper-left {
width: 60px;
height: 60px;
border: 5px solid #ccc;
clip: rect(0px, 30px, 60px, 0);
border-radius: 50%;
position: absolute;
}
.progress-wrapper-right {
width: 60px;
height: 60px;
border: 5px solid #ccc;
clip: rect(0px, 30px, 60px, 0);
border-radius: 50%;
/*transform: rotate(180deg);*/
position: absolute;
}
.progress-number {
width: 60px;
height: 60px;
line-height: 60px;
text-align: center;
}
</style>
</head>
<body>
<nav class="navbar navbar-default bg-info">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#"><span class="glyphicon glyphicon-cloud-download"> </span>File Download Proxy</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/hanjm/file_download_proxy" target="_blank">
<span class="glyphicon glyphicon-heart"></span> View Source (Golang net/http.Client + WebSocket)</a></li>
</ul>
</div>
</nav>
<div class="main-container">
<div class="alert">
</div>
<div>
<form class="form form-horizontal" id="url-input-form">
<div class="form-group col-sm-12" id="main-form">
<div class="col-sm-10">
<input class="form-control" id="url" name="url"
placeholder="输入下载地址http/magnet/base64TorrentContent, GitHub的资源只需要粘贴源地址, 不要粘贴重定向到AWS的地址, 拖回本地时支持多线程下载工具">
</div>
<button type="button" class="btn btn-success col-sm-2" id="create_download_task">下载</button>
</div>
</form>
</div>
<p> </p>
<table class="table">
<thead class="files-info-thead">
<tr>
<th style="width: 50px;">序号</th>
<th>文件名</th>
<th style="width: 120px;">大小</th>
<th style="width: 80px;">速度</th>
<th style="width: 80px;">进度</th>
<th>下载地址</th>
<th>源</th>
<th style="width: 100px;">开始时间</th>
<th style="width: 80px;">用时</th>
<th style="width: 80px;">操作</th>
</tr>
</thead>
<tbody id="files-info-container"></tbody>
</table>
<script>
var TASK_URL = "/file_download_proxy/task";
var WS_URL = "ws://" + window.location.host + "/file_download_proxy/ws";
var DOWNLOAD_URL = "/download/";
var HUMAN_READ_UNIT = ["B", "KB", "MB", "GB", "TB", "EB"];
function get_human_read_size(size) {
var index = 0;
for (; size > 1024; index++) {
size /= 1024;
}
return Math.round(size * 100) / 100 + HUMAN_READ_UNIT[index]
}
// 更新进度环
function update_circular_progress(progress_element, complete_rate) {
var wrapper_left_rotate_deg = 0;
var wrapper_right_rotate_deg = 0;
if (complete_rate <= 50) {
wrapper_right_rotate_deg = 180 + complete_rate / 50 * 180;
progress_element.find(".progress-right").hide();
progress_element.find(".progress-wrapper-right").show();
}
else {
wrapper_left_rotate_deg = (complete_rate - 50) / 50 * 180;
progress_element.find(".progress-right").show();
progress_element.find(".progress-wrapper-right").hide();
}
progress_element.find(".progress-wrapper-left").css("transform", "rotate(" + wrapper_left_rotate_deg + "deg)");
progress_element.find(".progress-wrapper-right").css("transform", "rotate(" + wrapper_right_rotate_deg + "deg)");
}
function fetch_files_info(e) {
var $container = $("#files-info-container");
var length = $container.children('tr').length;
if (e.data == undefined) {
return
}
console.log(e.data);
var data = JSON.parse(e.data);
var row_counter = 1;
var index = 0;
for (index in data) {
file_info = data[index];
var td_size = get_human_read_size(file_info.Size) + " / " + get_human_read_size(file_info.ContentLength);
var td_download_speed = get_human_read_size(file_info.Speed) + "/s";
var complete_rate = file_info.ContentLength == 0 ? 0 : Math.ceil(file_info.Size / file_info.ContentLength * 100);
if (file_info.IsCompleted) {
complete_rate = 100
}
var td_complete_rate = "<div class='progress-container'></div><div class='progress-full'></div><div class='progress-wrapper-left'></div><div class='progress-wrapper-right'></div><div class='progress-right'></div><div class='progress-number'>" + complete_rate + "%</div></div>";
var td_download_url = "-";
if (file_info.IsCompleted || (file_info.Size > 0 && file_info.Size == file_info.ContentLength)) {
td_download_url = "<a href='" + DOWNLOAD_URL + file_info.FileName + "'>" + DOWNLOAD_URL + file_info.FileName + "</a>";
}
var td_source_url = file_info.SourceURL.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
if (file_info.IsError) {
td_source_url = "错误信息:" + file_info.Error + "<br/><br/> source_url:" + td_source_url
}
var td_start_time = file_info.StartTime;
var td_duration = new Number(file_info.Duration / 1e9).toFixed(1).toString() + " 秒";
var $tr;
if (row_counter <= length) {
// 仅更新表格数据不动DOM node
$tr = $($container.children('tr')[row_counter - 1]);
if (file_info.IsCompleted) {
$tr.removeClass("bg-info").removeClass("bg-danger");
} else {
if (file_info.IsError) {
$tr.addClass("bg-danger").removeClass("bg-info");
} else {
$tr.addClass("bg-info").removeClass("bg-danger");
}
}
$tr.children('.line_num').text(row_counter);
$tr.children('.filename').text(file_info.FileName);
$tr.children('.human_size').text(td_size);
$tr.children('.download_speed').text(td_download_speed);
$tr.children('.complete_rate').find('.progress-number').text(complete_rate + "%");
$tr.children('.download_url').html(td_download_url);
$tr.children('.source_url').text(td_source_url);
$tr.children('.start_time').text(td_start_time);
$tr.children('.duration').text(td_duration);
} else {
// 增加tr更新表格数据
var template = [];
if (file_info.IsCompleted) {
template.push("<tr>");
} else {
if (file_info.IsError) {
template.push("<tr class='bg-danger'>");
}
else {
template.push("<tr class='bg-info'>");
}
}
template.push("<td class='line_num'>" + row_counter + "</td>");
template.push("<td class='filename'>" + file_info.FileName + "</td>");
template.push("<td class='human_size'>" + td_size + "</td>");
template.push("<td class='download_speed'>" + td_download_speed + "</td>");
template.push("<td class='complete_rate'>" + td_complete_rate + "</td>");
template.push("<td class='download_url'>" + td_download_url + "</td>");
template.push("<td class='source_url'>" + td_source_url + "</td>");
template.push("<td class='start_time'>" + td_start_time + "</td>");
template.push("<td class='duration'>" + td_duration + "</td>");
template.push("<td class='delete_file'><button type='button' class='btn btn-default'> 删除 </button></td>");
template.push("</tr>");
$container.append(template.join(""));
$tr = $($container.children('tr')[row_counter - 1]);
}
row_counter++;
// 更新进度环
var $progress = $tr.children('.complete_rate');
update_circular_progress($progress, complete_rate);
// 绑定删除事件
$(".delete_file").off("click").on("click", function () {
var filename = $(this).parent().find(".filename").text();
var $self = $(this);
$.ajax({
url: TASK_URL + "?filename=" + filename,
method: "DELETE"
}).done(function (data) {
$self.parent().remove();
$(".alert").addClass("alert-success").append(data).removeClass("alert-danger");
}).fail(function (xhr, option, err) {
$(".alert").addClass("alert-danger").append(xhr.responseText + "<br/>").removeClass("alert-success");
});
})
}
if (length >= row_counter) {
$($container.children('tr').slice(row_counter - 1)).each(function () {
$(this).remove();
})
}
}
// 输入框内回车阻止提交form并点击下载
$("#url-input-form").on("submit", function (e) {
document.getElementById("create_download_task").click();
e.preventDefault();
});
$("#create_download_task").on("click", function () {
var $url_input = $("#url");
var url = $url_input.val();
$url_input.val("");
if (url != "") {
$.ajax({
url: TASK_URL,
method: "POST",
data: {
url: url
}
}).done(function (data) {
$(".alert").addClass("alert-success").append(data + "<br/>").removeClass("alert-danger");
}).fail(function (xhr, option, err) {
$(".alert").addClass("alert-danger").append(xhr.responseText + err.toString() + "<br/>").removeClass("alert-success");
})
}
});
ws = new WebSocket(WS_URL);
ws.onmessage = fetch_files_info;
</script>
</div>
</body>
</html>