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

patrick-canterino.de