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

patrick-canterino.de