]> git.p6c8.net - jirafeau.git/blob - lib/functions_v3.js
Merge branch 'master' into 'master'
[jirafeau.git] / lib / functions_v3.js
1 /*
2 * Jirafeau, your web file repository
3 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 function show_link (url, reference, delete_code, crypt_key, date)
20 {
21 // Download page if element exists
22 var download_link = url + 'f.php?h=' + reference;
23 var download_link_href = url + 'f.php?h=' + reference;
24 if (crypt_key.length > 0)
25 {
26 download_link += '&amp;k=' + crypt_key;
27 download_link_href += '&k=' + crypt_key;
28 }
29 if (!!document.getElementById('upload_finished_download_page'))
30 {
31 document.getElementById('upload_link').innerHTML = download_link;
32 document.getElementById('upload_link').href = download_link_href;
33 }
34
35 // Is the preview allowed ?
36 if (!!document.getElementById('preview_link'))
37 {
38 document.getElementById('upload_finished_preview').style.display = 'none';
39 var preview_link = url + 'f.php?h=' + reference + '&amp;p=1';
40 var preview_link_href = url + 'f.php?h=' + reference + '&p=1';
41 if (crypt_key.length > 0)
42 {
43 preview_link += '&amp;k=' + crypt_key;
44 preview_link_href += '&k=' + crypt_key;
45 }
46
47 // Test if content can be previewed
48 type = document.getElementById('file_select').files[0].type;
49 if (type.indexOf("image") > -1 ||
50 type.indexOf("audio") > -1 ||
51 type.indexOf("text") > -1 ||
52 type.indexOf("video") > -1)
53 {
54 document.getElementById('preview_link').innerHTML = preview_link;
55 document.getElementById('preview_link').href = preview_link_href;
56 document.getElementById('upload_finished_preview').style.display = '';
57 }
58 }
59
60 // Only show link to password page if password is set
61 document.getElementById('upload_password_page').style.display = 'none';
62 if (document.getElementById('input_key').value.length > 0)
63 {
64 if (!!document.getElementById('upload_finished_download_page'))
65 document.getElementById('upload_finished_download_page').style.display = 'none';
66 document.getElementById('upload_password_page').style.display = '';
67 if (!!document.getElementById('upload_finished_preview'))
68 document.getElementById('upload_finished_preview').style.display = 'none';
69 document.getElementById('upload_direct_download').style.display = 'none';
70
71 document.getElementById('password_link').innerHTML = download_link;
72 document.getElementById('password_link').href = download_link_href;
73 }
74 // Direct download link
75 else
76 {
77 var direct_download_link = url + 'f.php?h=' + reference + '&amp;d=1';
78 var direct_download_link_href = url + 'f.php?h=' + reference + '&d=1';
79 if (crypt_key.length > 0)
80 {
81 direct_download_link += '&amp;k=' + crypt_key;
82 direct_download_link_href += '&k=' + crypt_key;
83 }
84 document.getElementById('direct_link').innerHTML = direct_download_link;
85 document.getElementById('direct_link').href = direct_download_link_href;
86 }
87
88 // Delete link
89 var delete_link = url + 'f.php?h=' + reference + '&amp;d=' + delete_code;
90 var delete_link_href = url + 'f.php?h=' + reference + '&d=' + delete_code;
91 document.getElementById('delete_link').innerHTML = delete_link;
92 document.getElementById('delete_link').href = delete_link_href;
93
94 if (date)
95 {
96 document.getElementById('date').innerHTML = date;
97 document.getElementById('validity').style.display = '';
98 }
99 else
100 document.getElementById('validity').style.display = 'none';
101
102 document.getElementById('uploading').style.display = 'none';
103 document.getElementById('upload').style.display = 'none';
104 document.getElementById('upload_finished').style.display = '';
105 document.title = 'Jirafeau - 100%';
106 }
107
108 function show_upload_progression (percentage, speed, time_left)
109 {
110 document.getElementById('uploaded_percentage').innerHTML = percentage;
111 document.getElementById('uploaded_speed').innerHTML = speed;
112 document.getElementById('uploaded_time').innerHTML = time_left;
113 document.title = 'Jirafeau - ' + percentage;
114 }
115
116 function hide_upload_progression ()
117 {
118 document.getElementById('uploaded_percentage').style.display = 'none';
119 document.getElementById('uploaded_speed').style.display = 'none';
120 document.getElementById('uploaded_time').style.display = 'none';
121 document.title = 'Jirafeau';
122 }
123
124 function upload_progress (e)
125 {
126 if (!e.lengthComputable)
127 return;
128
129 // Init time estimation if needed
130 if (upload_time_estimation_total_size == 0)
131 upload_time_estimation_total_size = e.total;
132
133 // Compute percentage
134 var p = Math.round (e.loaded * 100 / e.total);
135 var p_str = ' ';
136 if (p != 100)
137 p_str = p.toString() + '%';
138 // Update estimation speed
139 upload_time_estimation_add(e.loaded);
140 // Get speed string
141 var speed_str = upload_time_estimation_speed_string();
142 // Get time string
143 var time_str = chrono_update(upload_time_estimation_time());
144
145 show_upload_progression (p_str, speed_str, time_str);
146 }
147
148 function control_selected_file_size(max_size, error_str)
149 {
150 f_size = document.getElementById('file_select').files[0].size;
151 if (max_size > 0 && f_size > max_size * 1024 * 1024)
152 {
153 pop_failure(error_str);
154 document.getElementById('send').style.display = 'none';
155 }
156 else
157 {
158 document.getElementById('options').style.display = '';
159 document.getElementById('send').style.display = '';
160 document.getElementById('error_pop').style.display = 'none';
161 }
162 }
163
164 function pop_failure (e)
165 {
166 var text = "An error occured";
167 if (typeof e !== 'undefined')
168 text = e;
169 text = "<p>" + text + "</p>";
170 document.getElementById('error_pop').innerHTML = e;
171
172 document.getElementById('uploading').style.display = 'none';
173 document.getElementById('error_pop').style.display = '';
174 document.getElementById('upload').style.display = '';
175 document.getElementById('send').style.display = '';
176 }
177
178 function classic_upload (url, file, time, password, one_time, upload_password)
179 {
180 // Delay time estimation init as we can't have file size
181 upload_time_estimation_init(0);
182
183 var req = new XMLHttpRequest ();
184 req.upload.addEventListener ("progress", upload_progress, false);
185 req.addEventListener ("error", pop_failure, false);
186 req.addEventListener ("abort", pop_failure, false);
187 req.onreadystatechange = function ()
188 {
189 if (req.readyState == 4 && req.status == 200)
190 {
191 var res = req.responseText;
192 if (res == "Error")
193 {
194 pop_failure ();
195 return;
196 }
197 res = res.split ("\n");
198 if (time != 'none')
199 {
200 var d = new Date();
201 if (time == 'minute')
202 d.setSeconds (d.getSeconds() + 60);
203 else if (time == 'hour')
204 d.setSeconds (d.getSeconds() + 3600);
205 else if (time == 'day')
206 d.setSeconds (d.getSeconds() + 86400);
207 else if (time == 'week')
208 d.setSeconds (d.getSeconds() + 604800);
209 else if (time == 'month')
210 d.setSeconds (d.getSeconds() + 2419200);
211 else if (time == 'year')
212 d.setSeconds (d.getSeconds() + 29030400);
213 else
214 return;
215 show_link (url, res[0], res[1], res[2], d.toString());
216 }
217 else
218 show_link (url, res[0], res[1], res[2]);
219 }
220 }
221 req.open ("POST", url + 'script.php' , true);
222
223 var form = new FormData();
224 form.append ("file", file);
225 if (time)
226 form.append ("time", time);
227 if (password)
228 form.append ("key", password);
229 if (one_time)
230 form.append ("one_time_download", '1');
231 if (upload_password.length > 0)
232 form.append ("upload_password", upload_password);
233
234 req.send (form);
235 }
236
237 function check_html5_file_api ()
238 {
239 if (window.File && window.FileReader && window.FileList && window.Blob)
240 return true;
241 return false;
242 }
243
244 var async_global_transfered = 0;
245 var async_global_url = '';
246 var async_global_file;
247 var async_global_ref = '';
248 var async_global_max_size = 0;
249 var async_global_time;
250 var async_global_transfering = 0;
251
252 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
253 {
254 async_global_transfered = 0;
255 async_global_url = url;
256 async_global_file = file;
257 async_global_max_size = max_size;
258 async_global_time = time;
259
260 var req = new XMLHttpRequest ();
261 req.addEventListener ("error", pop_failure, false);
262 req.addEventListener ("abort", pop_failure, false);
263 req.onreadystatechange = function ()
264 {
265 if (req.readyState == 4 && req.status == 200)
266 {
267 var res = req.responseText;
268 if (res == "Error")
269 {
270 pop_failure ();
271 return;
272 }
273 res = res.split ("\n");
274 async_global_ref = res[0];
275 var code = res[1];
276 async_upload_push (code);
277 }
278 }
279 req.open ("POST", async_global_url + 'script.php?init_async' , true);
280
281 var form = new FormData();
282 form.append ("filename", async_global_file.name);
283 form.append ("type", async_global_file.type);
284 if (time)
285 form.append ("time", time);
286 if (password)
287 form.append ("key", password);
288 if (one_time)
289 form.append ("one_time_download", '1');
290 if (upload_password.length > 0)
291 form.append ("upload_password", upload_password);
292
293 // Start time estimation
294 upload_time_estimation_init(async_global_file.size);
295
296 req.send (form);
297 }
298
299 function async_upload_progress (e)
300 {
301 if (!e.lengthComputable && async_global_file.size != 0)
302 return;
303
304 // Compute percentage
305 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
306 var p_str = ' ';
307 if (p != 100)
308 p_str = p.toString() + '%';
309 // Update estimation speed
310 upload_time_estimation_add(e.loaded + async_global_transfered);
311 // Get speed string
312 var speed_str = upload_time_estimation_speed_string();
313 // Get time string
314 var time_str = chrono_update(upload_time_estimation_time());
315
316 show_upload_progression (p_str, speed_str, time_str);
317 }
318
319 function async_upload_push (code)
320 {
321 if (async_global_transfered == async_global_file.size)
322 {
323 hide_upload_progression ();
324 async_upload_end (code);
325 return;
326 }
327 var req = new XMLHttpRequest ();
328 req.upload.addEventListener ("progress", async_upload_progress, false);
329 req.addEventListener ("error", pop_failure, false);
330 req.addEventListener ("abort", pop_failure, false);
331 req.onreadystatechange = function ()
332 {
333 if (req.readyState == 4 && req.status == 200)
334 {
335 var res = req.responseText;
336 if (res == "Error")
337 {
338 pop_failure ();
339 return;
340 }
341 res = res.split ("\n");
342 var code = res[0]
343 async_global_transfered = async_global_transfering;
344 async_upload_push (code);
345 }
346 }
347 req.open ("POST", async_global_url + 'script.php?push_async' , true);
348
349 var chunk_size = parseInt (async_global_max_size * 0.50);
350 var start = async_global_transfered;
351 var end = start + chunk_size;
352 if (end >= async_global_file.size)
353 end = async_global_file.size;
354 var blob = async_global_file.slice (start, end);
355 async_global_transfering = end;
356
357 var form = new FormData();
358 form.append ("ref", async_global_ref);
359 form.append ("data", blob);
360 form.append ("code", code);
361 req.send (form);
362 }
363
364 function async_upload_end (code)
365 {
366 var req = new XMLHttpRequest ();
367 req.addEventListener ("error", pop_failure, false);
368 req.addEventListener ("abort", pop_failure, false);
369 req.onreadystatechange = function ()
370 {
371 if (req.readyState == 4 && req.status == 200)
372 {
373 var res = req.responseText;
374 if (res == "Error")
375 {
376 pop_failure ();
377 return;
378 }
379 res = res.split ("\n");
380 if (async_global_time != 'none')
381 {
382 var d = new Date();
383 if (async_global_time == 'minute')
384 d.setSeconds (d.getSeconds() + 60);
385 else if (async_global_time == 'hour')
386 d.setSeconds (d.getSeconds() + 3600);
387 else if (async_global_time == 'day')
388 d.setSeconds (d.getSeconds() + 86400);
389 else if (async_global_time == 'week')
390 d.setSeconds (d.getSeconds() + 604800);
391 else if (async_global_time == 'month')
392 d.setSeconds (d.getSeconds() + 2419200);
393 else if (async_global_time == 'year')
394 d.setSeconds (d.getSeconds() + 29030400);
395 else
396 return;
397 show_link (async_global_url, res[0], res[1], res[2], d.toString());
398 }
399 else
400 show_link (async_global_url, res[0], res[1], res[2]);
401 }
402 }
403 req.open ("POST", async_global_url + 'script.php?end_async' , true);
404
405 var form = new FormData();
406 form.append ("ref", async_global_ref);
407 form.append ("code", code);
408 req.send (form);
409 }
410
411 function upload (url, max_size)
412 {
413 if (check_html5_file_api ()
414 && document.getElementById('file_select').files[0].size >= max_size)
415 {
416 async_upload_start (url,
417 max_size,
418 document.getElementById('file_select').files[0],
419 document.getElementById('select_time').value,
420 document.getElementById('input_key').value,
421 document.getElementById('one_time_download').checked,
422 document.getElementById('upload_password').value
423 );
424 }
425 else
426 {
427 classic_upload (url,
428 document.getElementById('file_select').files[0],
429 document.getElementById('select_time').value,
430 document.getElementById('input_key').value,
431 document.getElementById('one_time_download').checked,
432 document.getElementById('upload_password').value
433 );
434 }
435 }
436
437 var upload_time_estimation_total_size = 42;
438 var upload_time_estimation_transfered_size = 42;
439 var upload_time_estimation_transfered_date = 42;
440 var upload_time_estimation_moving_average_speed = 42;
441
442 function upload_time_estimation_init(total_size)
443 {
444 upload_time_estimation_total_size = total_size;
445 upload_time_estimation_transfered_size = 0;
446 upload_time_estimation_moving_average_speed = 0;
447 var d = new Date();
448 upload_time_estimation_transfered_date = d.getTime();
449 }
450
451 function upload_time_estimation_add(total_transfered_size)
452 {
453 // Let's compute the current speed
454 var d = new Date();
455 var speed = upload_time_estimation_moving_average_speed;
456 if (d.getTime() - upload_time_estimation_transfered_date != 0)
457 speed = (total_transfered_size - upload_time_estimation_transfered_size)
458 / (d.getTime() - upload_time_estimation_transfered_date);
459 // Let's compute moving average speed on 30 values
460 var m = (upload_time_estimation_moving_average_speed * 29 + speed) / 30;
461 // Update global values
462 upload_time_estimation_transfered_size = total_transfered_size;
463 upload_time_estimation_transfered_date = d.getTime();
464 upload_time_estimation_moving_average_speed = m;
465 }
466
467 function upload_time_estimation_speed_string()
468 {
469 // speed ms -> s
470 var s = upload_time_estimation_moving_average_speed * 1000;
471 var res = 0;
472 var scale = '';
473 if (s <= 1000)
474 {
475 res = s.toString();
476 scale = "o/s";
477 }
478 else if (s < 1000000)
479 {
480 res = Math.floor(s/100) / 10;
481 scale = "Ko/s";
482 }
483 else
484 {
485 res = Math.floor(s/100000) / 10;
486 scale = "Mo/s";
487 }
488 if (res == 0)
489 return '';
490 else
491 return res.toString() + ' ' + scale;
492 }
493
494 function milliseconds_to_time_string (milliseconds)
495 {
496 function numberEnding (number) {
497 return (number > 1) ? 's' : '';
498 }
499
500 var temp = Math.floor(milliseconds / 1000);
501 var years = Math.floor(temp / 31536000);
502 if (years) {
503 return years + ' year' + numberEnding(years);
504 }
505 var days = Math.floor((temp %= 31536000) / 86400);
506 if (days) {
507 return days + ' day' + numberEnding(days);
508 }
509 var hours = Math.floor((temp %= 86400) / 3600);
510 if (hours) {
511 return hours + ' hour' + numberEnding(hours);
512 }
513 var minutes = Math.floor((temp %= 3600) / 60);
514 if (minutes) {
515 return minutes + ' minute' + numberEnding(minutes);
516 }
517 var seconds = temp % 60;
518 if (seconds) {
519 return seconds + ' second' + numberEnding(seconds);
520 }
521 return 'less than a second';
522 }
523
524 function upload_time_estimation_time()
525 {
526 // Estimate remaining time
527 if (upload_time_estimation_moving_average_speed == 0)
528 return 0;
529 return (upload_time_estimation_total_size - upload_time_estimation_transfered_size)
530 / upload_time_estimation_moving_average_speed;
531 }
532
533 var chrono_last_update = 0;
534 var chrono_time_ms = 0;
535 var chrono_time_ms_last_update = 0;
536 function chrono_update(time_ms)
537 {
538 var d = new Date();
539 var chrono = 0;
540 // Don't update too often
541 if (d.getTime() - chrono_last_update < 3000 &&
542 chrono_time_ms_last_update > 0)
543 chrono = chrono_time_ms;
544 else
545 {
546 chrono_last_update = d.getTime();
547 chrono_time_ms = time_ms;
548 chrono = time_ms;
549 chrono_time_ms_last_update = d.getTime();
550 }
551
552 // Adjust chrono for smooth estimation
553 chrono = chrono - (d.getTime() - chrono_time_ms_last_update);
554
555 // Let's update chronometer
556 var time_str = '';
557 if (chrono > 0)
558 time_str = milliseconds_to_time_string (chrono);
559 return time_str;
560 }

patrick-canterino.de