]> git.p6c8.net - jirafeau.git/blob - lib/functions_v6.js
lib/locales: fill Finnish language with template
[jirafeau.git] / lib / functions_v6.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 // Upload finished
22 document.getElementById('uploading').style.display = 'none';
23 document.getElementById('upload').style.display = 'none';
24 document.getElementById('upload_finished').style.display = '';
25 document.title = 'Jirafeau - 100%';
26
27 // Download page
28 var download_link = url + 'f.php?h=' + reference;
29 var download_link_href = url + 'f.php?h=' + reference;
30 if (crypt_key.length > 0)
31 {
32 download_link += '&amp;k=' + crypt_key;
33 download_link_href += '&k=' + crypt_key;
34 }
35 if (!!document.getElementById('upload_finished_download_page'))
36 {
37 document.getElementById('upload_link').innerHTML = download_link;
38 document.getElementById('upload_link').href = download_link_href;
39 }
40
41 // Email link
42 var filename = document.getElementById('file_select').files[0].name;
43 var b = "Download%20%22" + filename + "%22 :%0D" + download_link_href + "%0D";
44 document.getElementById('upload_link_email').href = "mailto:?body=" + b + "&subject=" + filename;
45
46 // Delete link
47 var delete_link = url + 'f.php?h=' + reference + '&amp;d=' + delete_code;
48 var delete_link_href = url + 'f.php?h=' + reference + '&d=' + delete_code;
49 document.getElementById('delete_link').innerHTML = delete_link;
50 document.getElementById('delete_link').href = delete_link_href;
51
52 // Validity date
53 if (date)
54 {
55 document.getElementById('date').innerHTML = date;
56 document.getElementById('validity').style.display = '';
57 }
58 else
59 document.getElementById('validity').style.display = 'none';
60
61 // Preview link (if allowed)
62 if (!!document.getElementById('preview_link'))
63 {
64 document.getElementById('upload_finished_preview').style.display = 'none';
65 var preview_link = url + 'f.php?h=' + reference + '&amp;p=1';
66 var preview_link_href = url + 'f.php?h=' + reference + '&p=1';
67 if (crypt_key.length > 0)
68 {
69 preview_link += '&amp;k=' + crypt_key;
70 preview_link_href += '&k=' + crypt_key;
71 }
72
73 // Test if content can be previewed
74 type = document.getElementById('file_select').files[0].type;
75 if (type.indexOf("image") > -1 ||
76 type.indexOf("audio") > -1 ||
77 type.indexOf("text") > -1 ||
78 type.indexOf("video") > -1)
79 {
80 document.getElementById('preview_link').innerHTML = preview_link;
81 document.getElementById('preview_link').href = preview_link_href;
82 document.getElementById('upload_finished_preview').style.display = '';
83 }
84 }
85
86 // Direct download link
87 var direct_download_link = url + 'f.php?h=' + reference + '&amp;d=1';
88 var direct_download_link_href = url + 'f.php?h=' + reference + '&d=1';
89 if (crypt_key.length > 0)
90 {
91 direct_download_link += '&amp;k=' + crypt_key;
92 direct_download_link_href += '&k=' + crypt_key;
93 }
94 document.getElementById('direct_link').innerHTML = direct_download_link;
95 document.getElementById('direct_link').href = direct_download_link_href;
96
97
98 // Hide preview and direct download link if password is set
99 if (document.getElementById('input_key').value.length > 0)
100 {
101 if (!!document.getElementById('preview_link'))
102 document.getElementById('upload_finished_preview').style.display = 'none';
103 document.getElementById('upload_direct_download').style.display = 'none';
104 }
105 }
106
107 function show_upload_progression (percentage, speed, time_left)
108 {
109 document.getElementById('uploaded_percentage').innerHTML = percentage;
110 document.getElementById('uploaded_speed').innerHTML = speed;
111 document.getElementById('uploaded_time').innerHTML = time_left;
112 document.title = 'Jirafeau - ' + percentage;
113 }
114
115 function hide_upload_progression ()
116 {
117 document.getElementById('uploaded_percentage').style.display = 'none';
118 document.getElementById('uploaded_speed').style.display = 'none';
119 document.getElementById('uploaded_time').style.display = 'none';
120 document.title = 'Jirafeau';
121 }
122
123 function upload_progress (e)
124 {
125 if (!e.lengthComputable)
126 return;
127
128 // Init time estimation if needed
129 if (upload_time_estimation_total_size == 0)
130 upload_time_estimation_total_size = e.total;
131
132 // Compute percentage
133 var p = Math.round (e.loaded * 100 / e.total);
134 var p_str = ' ';
135 if (p != 100)
136 p_str = p.toString() + '%';
137 // Update estimation speed
138 upload_time_estimation_add(e.loaded);
139 // Get speed string
140 var speed_str = upload_time_estimation_speed_string();
141 speed_str = upload_speed_refresh_limiter(speed_str);
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 speed_str = upload_speed_refresh_limiter(speed_str);
314 // Get time string
315 var time_str = chrono_update(upload_time_estimation_time());
316
317 show_upload_progression (p_str, speed_str, time_str);
318 }
319
320 function async_upload_push (code)
321 {
322 if (async_global_transfered == async_global_file.size)
323 {
324 hide_upload_progression ();
325 async_upload_end (code);
326 return;
327 }
328 var req = new XMLHttpRequest ();
329 req.upload.addEventListener ("progress", async_upload_progress, false);
330 req.addEventListener ("error", pop_failure, false);
331 req.addEventListener ("abort", pop_failure, false);
332 req.onreadystatechange = function ()
333 {
334 if (req.readyState == 4 && req.status == 200)
335 {
336 var res = req.responseText;
337 if (res == "Error")
338 {
339 pop_failure ();
340 return;
341 }
342 res = res.split ("\n");
343 var code = res[0]
344 async_global_transfered = async_global_transfering;
345 async_upload_push (code);
346 }
347 }
348 req.open ("POST", async_global_url + 'script.php?push_async' , true);
349
350 var chunk_size = parseInt (async_global_max_size * 0.50);
351 var start = async_global_transfered;
352 var end = start + chunk_size;
353 if (end >= async_global_file.size)
354 end = async_global_file.size;
355 var blob = async_global_file.slice (start, end);
356 async_global_transfering = end;
357
358 var form = new FormData();
359 form.append ("ref", async_global_ref);
360 form.append ("data", blob);
361 form.append ("code", code);
362 req.send (form);
363 }
364
365 function async_upload_end (code)
366 {
367 var req = new XMLHttpRequest ();
368 req.addEventListener ("error", pop_failure, false);
369 req.addEventListener ("abort", pop_failure, false);
370 req.onreadystatechange = function ()
371 {
372 if (req.readyState == 4 && req.status == 200)
373 {
374 var res = req.responseText;
375 if (res == "Error")
376 {
377 pop_failure ();
378 return;
379 }
380 res = res.split ("\n");
381 if (async_global_time != 'none')
382 {
383 var d = new Date();
384 if (async_global_time == 'minute')
385 d.setSeconds (d.getSeconds() + 60);
386 else if (async_global_time == 'hour')
387 d.setSeconds (d.getSeconds() + 3600);
388 else if (async_global_time == 'day')
389 d.setSeconds (d.getSeconds() + 86400);
390 else if (async_global_time == 'week')
391 d.setSeconds (d.getSeconds() + 604800);
392 else if (async_global_time == 'month')
393 d.setSeconds (d.getSeconds() + 2419200);
394 else if (async_global_time == 'year')
395 d.setSeconds (d.getSeconds() + 29030400);
396 else
397 return;
398 show_link (async_global_url, res[0], res[1], res[2], d.toString());
399 }
400 else
401 show_link (async_global_url, res[0], res[1], res[2]);
402 }
403 }
404 req.open ("POST", async_global_url + 'script.php?end_async' , true);
405
406 var form = new FormData();
407 form.append ("ref", async_global_ref);
408 form.append ("code", code);
409 req.send (form);
410 }
411
412 function upload (url, max_size)
413 {
414 if (check_html5_file_api ()
415 && document.getElementById('file_select').files[0].size >= max_size)
416 {
417 async_upload_start (url,
418 max_size,
419 document.getElementById('file_select').files[0],
420 document.getElementById('select_time').value,
421 document.getElementById('input_key').value,
422 document.getElementById('one_time_download').checked,
423 document.getElementById('upload_password').value
424 );
425 }
426 else
427 {
428 classic_upload (url,
429 document.getElementById('file_select').files[0],
430 document.getElementById('select_time').value,
431 document.getElementById('input_key').value,
432 document.getElementById('one_time_download').checked,
433 document.getElementById('upload_password').value
434 );
435 }
436 }
437
438 var upload_time_estimation_total_size = 42;
439 var upload_time_estimation_transfered_size = 42;
440 var upload_time_estimation_transfered_date = 42;
441 var upload_time_estimation_moving_average_speed = 42;
442
443 function upload_time_estimation_init(total_size)
444 {
445 upload_time_estimation_total_size = total_size;
446 upload_time_estimation_transfered_size = 0;
447 upload_time_estimation_moving_average_speed = 0;
448 var d = new Date();
449 upload_time_estimation_transfered_date = d.getTime();
450 }
451
452 function upload_time_estimation_add(total_transfered_size)
453 {
454 // Let's compute the current speed
455 var d = new Date();
456 var speed = upload_time_estimation_moving_average_speed;
457 if (d.getTime() - upload_time_estimation_transfered_date != 0)
458 speed = (total_transfered_size - upload_time_estimation_transfered_size)
459 / (d.getTime() - upload_time_estimation_transfered_date);
460 // Let's compute moving average speed on 30 values
461 var m = (upload_time_estimation_moving_average_speed * 29 + speed) / 30;
462 // Update global values
463 upload_time_estimation_transfered_size = total_transfered_size;
464 upload_time_estimation_transfered_date = d.getTime();
465 upload_time_estimation_moving_average_speed = m;
466 }
467
468 function upload_time_estimation_speed_string()
469 {
470 // speed ms -> s
471 var s = upload_time_estimation_moving_average_speed * 1000;
472 var res = 0;
473 var scale = '';
474 if (s <= 1000)
475 {
476 res = s.toString();
477 scale = "o/s";
478 }
479 else if (s < 1000000)
480 {
481 res = Math.floor(s/100) / 10;
482 scale = "Ko/s";
483 }
484 else
485 {
486 res = Math.floor(s/100000) / 10;
487 scale = "Mo/s";
488 }
489 if (res == 0)
490 return '';
491 else
492 return res.toString() + ' ' + scale;
493 }
494
495 function milliseconds_to_time_string (milliseconds)
496 {
497 function numberEnding (number) {
498 return (number > 1) ? 's' : '';
499 }
500
501 var temp = Math.floor(milliseconds / 1000);
502 var years = Math.floor(temp / 31536000);
503 if (years) {
504 return years + ' year' + numberEnding(years);
505 }
506 var days = Math.floor((temp %= 31536000) / 86400);
507 if (days) {
508 return days + ' day' + numberEnding(days);
509 }
510 var hours = Math.floor((temp %= 86400) / 3600);
511 if (hours) {
512 return hours + ' hour' + numberEnding(hours);
513 }
514 var minutes = Math.floor((temp %= 3600) / 60);
515 if (minutes) {
516 return minutes + ' minute' + numberEnding(minutes);
517 }
518 var seconds = temp % 60;
519 if (seconds) {
520 return seconds + ' second' + numberEnding(seconds);
521 }
522 return 'less than a second';
523 }
524
525 function upload_time_estimation_time()
526 {
527 // Estimate remaining time
528 if (upload_time_estimation_moving_average_speed == 0)
529 return 0;
530 return (upload_time_estimation_total_size - upload_time_estimation_transfered_size)
531 / upload_time_estimation_moving_average_speed;
532 }
533
534 var chrono_last_update = 0;
535 var chrono_time_ms = 0;
536 var chrono_time_ms_last_update = 0;
537 function chrono_update(time_ms)
538 {
539 var d = new Date();
540 var chrono = 0;
541 // Don't update too often
542 if (d.getTime() - chrono_last_update < 3000 &&
543 chrono_time_ms_last_update > 0)
544 chrono = chrono_time_ms;
545 else
546 {
547 chrono_last_update = d.getTime();
548 chrono_time_ms = time_ms;
549 chrono = time_ms;
550 chrono_time_ms_last_update = d.getTime();
551 }
552
553 // Adjust chrono for smooth estimation
554 chrono = chrono - (d.getTime() - chrono_time_ms_last_update);
555
556 // Let's update chronometer
557 var time_str = '';
558 if (chrono > 0)
559 time_str = milliseconds_to_time_string (chrono);
560 return time_str;
561 }
562
563 var upload_speed_refresh_limiter_last_update = 0;
564 var upload_speed_refresh_limiter_last_value = '';
565 function upload_speed_refresh_limiter(speed_str)
566 {
567 var d = new Date();
568 if (d.getTime() - upload_speed_refresh_limiter_last_update > 1500)
569 {
570 upload_speed_refresh_limiter_last_value = speed_str;
571 upload_speed_refresh_limiter_last_update = d.getTime();
572 }
573 return upload_speed_refresh_limiter_last_value;
574 }

patrick-canterino.de