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

patrick-canterino.de