]> git.p6c8.net - jirafeau_project.git/blob - lib/functions_v4.js
Translated using Weblate (Russian)
[jirafeau_project.git] / lib / functions_v4.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 speed_str = upload_speed_refresh_limiter(speed_str);
143 // Get time string
144 var time_str = chrono_update(upload_time_estimation_time());
145
146 show_upload_progression (p_str, speed_str, time_str);
147 }
148
149 function control_selected_file_size(max_size, error_str)
150 {
151 f_size = document.getElementById('file_select').files[0].size;
152 if (max_size > 0 && f_size > max_size * 1024 * 1024)
153 {
154 pop_failure(error_str);
155 document.getElementById('send').style.display = 'none';
156 }
157 else
158 {
159 document.getElementById('options').style.display = '';
160 document.getElementById('send').style.display = '';
161 document.getElementById('error_pop').style.display = 'none';
162 }
163 }
164
165 function pop_failure (e)
166 {
167 var text = "An error occured";
168 if (typeof e !== 'undefined')
169 text = e;
170 text = "<p>" + text + "</p>";
171 document.getElementById('error_pop').innerHTML = e;
172
173 document.getElementById('uploading').style.display = 'none';
174 document.getElementById('error_pop').style.display = '';
175 document.getElementById('upload').style.display = '';
176 document.getElementById('send').style.display = '';
177 }
178
179 function classic_upload (url, file, time, password, one_time, upload_password)
180 {
181 // Delay time estimation init as we can't have file size
182 upload_time_estimation_init(0);
183
184 var req = new XMLHttpRequest ();
185 req.upload.addEventListener ("progress", upload_progress, false);
186 req.addEventListener ("error", pop_failure, false);
187 req.addEventListener ("abort", pop_failure, false);
188 req.onreadystatechange = function ()
189 {
190 if (req.readyState == 4 && req.status == 200)
191 {
192 var res = req.responseText;
193 if (res == "Error")
194 {
195 pop_failure ();
196 return;
197 }
198 res = res.split ("\n");
199 if (time != 'none')
200 {
201 var d = new Date();
202 if (time == 'minute')
203 d.setSeconds (d.getSeconds() + 60);
204 else if (time == 'hour')
205 d.setSeconds (d.getSeconds() + 3600);
206 else if (time == 'day')
207 d.setSeconds (d.getSeconds() + 86400);
208 else if (time == 'week')
209 d.setSeconds (d.getSeconds() + 604800);
210 else if (time == 'month')
211 d.setSeconds (d.getSeconds() + 2419200);
212 else if (time == 'year')
213 d.setSeconds (d.getSeconds() + 29030400);
214 else
215 return;
216 show_link (url, res[0], res[1], res[2], d.toString());
217 }
218 else
219 show_link (url, res[0], res[1], res[2]);
220 }
221 }
222 req.open ("POST", url + 'script.php' , true);
223
224 var form = new FormData();
225 form.append ("file", file);
226 if (time)
227 form.append ("time", time);
228 if (password)
229 form.append ("key", password);
230 if (one_time)
231 form.append ("one_time_download", '1');
232 if (upload_password.length > 0)
233 form.append ("upload_password", upload_password);
234
235 req.send (form);
236 }
237
238 function check_html5_file_api ()
239 {
240 if (window.File && window.FileReader && window.FileList && window.Blob)
241 return true;
242 return false;
243 }
244
245 var async_global_transfered = 0;
246 var async_global_url = '';
247 var async_global_file;
248 var async_global_ref = '';
249 var async_global_max_size = 0;
250 var async_global_time;
251 var async_global_transfering = 0;
252
253 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
254 {
255 async_global_transfered = 0;
256 async_global_url = url;
257 async_global_file = file;
258 async_global_max_size = max_size;
259 async_global_time = time;
260
261 var req = new XMLHttpRequest ();
262 req.addEventListener ("error", pop_failure, false);
263 req.addEventListener ("abort", pop_failure, false);
264 req.onreadystatechange = function ()
265 {
266 if (req.readyState == 4 && req.status == 200)
267 {
268 var res = req.responseText;
269 if (res == "Error")
270 {
271 pop_failure ();
272 return;
273 }
274 res = res.split ("\n");
275 async_global_ref = res[0];
276 var code = res[1];
277 async_upload_push (code);
278 }
279 }
280 req.open ("POST", async_global_url + 'script.php?init_async' , true);
281
282 var form = new FormData();
283 form.append ("filename", async_global_file.name);
284 form.append ("type", async_global_file.type);
285 if (time)
286 form.append ("time", time);
287 if (password)
288 form.append ("key", password);
289 if (one_time)
290 form.append ("one_time_download", '1');
291 if (upload_password.length > 0)
292 form.append ("upload_password", upload_password);
293
294 // Start time estimation
295 upload_time_estimation_init(async_global_file.size);
296
297 req.send (form);
298 }
299
300 function async_upload_progress (e)
301 {
302 if (!e.lengthComputable && async_global_file.size != 0)
303 return;
304
305 // Compute percentage
306 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
307 var p_str = ' ';
308 if (p != 100)
309 p_str = p.toString() + '%';
310 // Update estimation speed
311 upload_time_estimation_add(e.loaded + async_global_transfered);
312 // Get speed string
313 var speed_str = upload_time_estimation_speed_string();
314 speed_str = upload_speed_refresh_limiter(speed_str);
315 // Get time string
316 var time_str = chrono_update(upload_time_estimation_time());
317
318 show_upload_progression (p_str, speed_str, time_str);
319 }
320
321 function async_upload_push (code)
322 {
323 if (async_global_transfered == async_global_file.size)
324 {
325 hide_upload_progression ();
326 async_upload_end (code);
327 return;
328 }
329 var req = new XMLHttpRequest ();
330 req.upload.addEventListener ("progress", async_upload_progress, false);
331 req.addEventListener ("error", pop_failure, false);
332 req.addEventListener ("abort", pop_failure, false);
333 req.onreadystatechange = function ()
334 {
335 if (req.readyState == 4 && req.status == 200)
336 {
337 var res = req.responseText;
338 if (res == "Error")
339 {
340 pop_failure ();
341 return;
342 }
343 res = res.split ("\n");
344 var code = res[0]
345 async_global_transfered = async_global_transfering;
346 async_upload_push (code);
347 }
348 }
349 req.open ("POST", async_global_url + 'script.php?push_async' , true);
350
351 var chunk_size = parseInt (async_global_max_size * 0.50);
352 var start = async_global_transfered;
353 var end = start + chunk_size;
354 if (end >= async_global_file.size)
355 end = async_global_file.size;
356 var blob = async_global_file.slice (start, end);
357 async_global_transfering = end;
358
359 var form = new FormData();
360 form.append ("ref", async_global_ref);
361 form.append ("data", blob);
362 form.append ("code", code);
363 req.send (form);
364 }
365
366 function async_upload_end (code)
367 {
368 var req = new XMLHttpRequest ();
369 req.addEventListener ("error", pop_failure, false);
370 req.addEventListener ("abort", pop_failure, false);
371 req.onreadystatechange = function ()
372 {
373 if (req.readyState == 4 && req.status == 200)
374 {
375 var res = req.responseText;
376 if (res == "Error")
377 {
378 pop_failure ();
379 return;
380 }
381 res = res.split ("\n");
382 if (async_global_time != 'none')
383 {
384 var d = new Date();
385 if (async_global_time == 'minute')
386 d.setSeconds (d.getSeconds() + 60);
387 else if (async_global_time == 'hour')
388 d.setSeconds (d.getSeconds() + 3600);
389 else if (async_global_time == 'day')
390 d.setSeconds (d.getSeconds() + 86400);
391 else if (async_global_time == 'week')
392 d.setSeconds (d.getSeconds() + 604800);
393 else if (async_global_time == 'month')
394 d.setSeconds (d.getSeconds() + 2419200);
395 else if (async_global_time == 'year')
396 d.setSeconds (d.getSeconds() + 29030400);
397 else
398 return;
399 show_link (async_global_url, res[0], res[1], res[2], d.toString());
400 }
401 else
402 show_link (async_global_url, res[0], res[1], res[2]);
403 }
404 }
405 req.open ("POST", async_global_url + 'script.php?end_async' , true);
406
407 var form = new FormData();
408 form.append ("ref", async_global_ref);
409 form.append ("code", code);
410 req.send (form);
411 }
412
413 function upload (url, max_size)
414 {
415 if (check_html5_file_api ()
416 && document.getElementById('file_select').files[0].size >= max_size)
417 {
418 async_upload_start (url,
419 max_size,
420 document.getElementById('file_select').files[0],
421 document.getElementById('select_time').value,
422 document.getElementById('input_key').value,
423 document.getElementById('one_time_download').checked,
424 document.getElementById('upload_password').value
425 );
426 }
427 else
428 {
429 classic_upload (url,
430 document.getElementById('file_select').files[0],
431 document.getElementById('select_time').value,
432 document.getElementById('input_key').value,
433 document.getElementById('one_time_download').checked,
434 document.getElementById('upload_password').value
435 );
436 }
437 }
438
439 var upload_time_estimation_total_size = 42;
440 var upload_time_estimation_transfered_size = 42;
441 var upload_time_estimation_transfered_date = 42;
442 var upload_time_estimation_moving_average_speed = 42;
443
444 function upload_time_estimation_init(total_size)
445 {
446 upload_time_estimation_total_size = total_size;
447 upload_time_estimation_transfered_size = 0;
448 upload_time_estimation_moving_average_speed = 0;
449 var d = new Date();
450 upload_time_estimation_transfered_date = d.getTime();
451 }
452
453 function upload_time_estimation_add(total_transfered_size)
454 {
455 // Let's compute the current speed
456 var d = new Date();
457 var speed = upload_time_estimation_moving_average_speed;
458 if (d.getTime() - upload_time_estimation_transfered_date != 0)
459 speed = (total_transfered_size - upload_time_estimation_transfered_size)
460 / (d.getTime() - upload_time_estimation_transfered_date);
461 // Let's compute moving average speed on 30 values
462 var m = (upload_time_estimation_moving_average_speed * 29 + speed) / 30;
463 // Update global values
464 upload_time_estimation_transfered_size = total_transfered_size;
465 upload_time_estimation_transfered_date = d.getTime();
466 upload_time_estimation_moving_average_speed = m;
467 }
468
469 function upload_time_estimation_speed_string()
470 {
471 // speed ms -> s
472 var s = upload_time_estimation_moving_average_speed * 1000;
473 var res = 0;
474 var scale = '';
475 if (s <= 1000)
476 {
477 res = s.toString();
478 scale = "o/s";
479 }
480 else if (s < 1000000)
481 {
482 res = Math.floor(s/100) / 10;
483 scale = "Ko/s";
484 }
485 else
486 {
487 res = Math.floor(s/100000) / 10;
488 scale = "Mo/s";
489 }
490 if (res == 0)
491 return '';
492 else
493 return res.toString() + ' ' + scale;
494 }
495
496 function milliseconds_to_time_string (milliseconds)
497 {
498 function numberEnding (number) {
499 return (number > 1) ? 's' : '';
500 }
501
502 var temp = Math.floor(milliseconds / 1000);
503 var years = Math.floor(temp / 31536000);
504 if (years) {
505 return years + ' year' + numberEnding(years);
506 }
507 var days = Math.floor((temp %= 31536000) / 86400);
508 if (days) {
509 return days + ' day' + numberEnding(days);
510 }
511 var hours = Math.floor((temp %= 86400) / 3600);
512 if (hours) {
513 return hours + ' hour' + numberEnding(hours);
514 }
515 var minutes = Math.floor((temp %= 3600) / 60);
516 if (minutes) {
517 return minutes + ' minute' + numberEnding(minutes);
518 }
519 var seconds = temp % 60;
520 if (seconds) {
521 return seconds + ' second' + numberEnding(seconds);
522 }
523 return 'less than a second';
524 }
525
526 function upload_time_estimation_time()
527 {
528 // Estimate remaining time
529 if (upload_time_estimation_moving_average_speed == 0)
530 return 0;
531 return (upload_time_estimation_total_size - upload_time_estimation_transfered_size)
532 / upload_time_estimation_moving_average_speed;
533 }
534
535 var chrono_last_update = 0;
536 var chrono_time_ms = 0;
537 var chrono_time_ms_last_update = 0;
538 function chrono_update(time_ms)
539 {
540 var d = new Date();
541 var chrono = 0;
542 // Don't update too often
543 if (d.getTime() - chrono_last_update < 3000 &&
544 chrono_time_ms_last_update > 0)
545 chrono = chrono_time_ms;
546 else
547 {
548 chrono_last_update = d.getTime();
549 chrono_time_ms = time_ms;
550 chrono = time_ms;
551 chrono_time_ms_last_update = d.getTime();
552 }
553
554 // Adjust chrono for smooth estimation
555 chrono = chrono - (d.getTime() - chrono_time_ms_last_update);
556
557 // Let's update chronometer
558 var time_str = '';
559 if (chrono > 0)
560 time_str = milliseconds_to_time_string (chrono);
561 return time_str;
562 }
563
564 var upload_speed_refresh_limiter_last_update = 0;
565 var upload_speed_refresh_limiter_last_value = '';
566 function upload_speed_refresh_limiter(speed_str)
567 {
568 var d = new Date();
569 if (d.getTime() - upload_speed_refresh_limiter_last_update > 1500)
570 {
571 upload_speed_refresh_limiter_last_value = speed_str;
572 upload_speed_refresh_limiter_last_update = d.getTime();
573 }
574 return upload_speed_refresh_limiter_last_value;
575 }

patrick-canterino.de