]> git.p6c8.net - jirafeau.git/blob - lib/functions_v7.js
temporaly remove content type detection
[jirafeau.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_string)
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))
249 {
250 return;
251 }
252 show_link (url, res[0], res[1], res[2], d.toString());
253 }
254 else
255 show_link (url, res[0], res[1], res[2]);
256 }
257 }
258 req.open ("POST", url + 'script.php' , true);
259
260 var form = new FormData();
261 form.append ("file", file);
262 if (time)
263 form.append ("time", time);
264 if (password)
265 form.append ("key", password);
266 if (one_time)
267 form.append ("one_time_download", '1');
268 if (upload_password.length > 0)
269 form.append ("upload_password", upload_password);
270
271 req.send (form);
272 }
273
274 function check_html5_file_api ()
275 {
276 return window.File && window.FileReader && window.FileList && window.Blob;
277 }
278
279 var async_global_transfered = 0;
280 var async_global_url = '';
281 var async_global_file;
282 var async_global_ref = '';
283 var async_global_max_size = 0;
284 var async_global_time;
285 var async_global_transfering = 0;
286
287 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
288 {
289 async_global_transfered = 0;
290 async_global_url = url;
291 async_global_file = file;
292 async_global_max_size = max_size;
293 async_global_time = time;
294
295 var req = new XMLHttpRequest ();
296 req.addEventListener ("error", pop_failure, false);
297 req.addEventListener ("abort", pop_failure, false);
298 req.onreadystatechange = function ()
299 {
300 if (req.readyState == 4 && req.status == 200)
301 {
302 var res = req.responseText;
303 if (res == "Error")
304 {
305 pop_failure ();
306 return;
307 }
308 res = res.split ("\n");
309 async_global_ref = res[0];
310 var code = res[1];
311 async_upload_push (code);
312 }
313 }
314 req.open ("POST", async_global_url + 'script.php?init_async' , true);
315
316 var form = new FormData();
317 form.append ("filename", async_global_file.name);
318 form.append ("type", async_global_file.type);
319 if (time)
320 form.append ("time", time);
321 if (password)
322 form.append ("key", password);
323 if (one_time)
324 form.append ("one_time_download", '1');
325 if (upload_password.length > 0)
326 form.append ("upload_password", upload_password);
327
328 // Start time estimation
329 upload_time_estimation_init(async_global_file.size);
330
331 req.send (form);
332 }
333
334 function async_upload_progress (e)
335 {
336 if (e == undefined || e == null || !e.lengthComputable && async_global_file.size != 0)
337 return;
338
339 // Compute percentage
340 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
341 var p_str = ' ';
342 if (p != 100)
343 p_str = p.toString() + '%';
344 // Update estimation speed
345 upload_time_estimation_add(e.loaded + async_global_transfered);
346 // Get speed string
347 var speed_str = upload_time_estimation_speed_string();
348 speed_str = upload_speed_refresh_limiter(speed_str);
349 // Get time string
350 var time_str = chrono_update(upload_time_estimation_time());
351
352 show_upload_progression (p_str, speed_str, time_str);
353 }
354
355 function async_upload_push (code)
356 {
357 if (async_global_transfered == async_global_file.size)
358 {
359 hide_upload_progression ();
360 async_upload_end (code);
361 return;
362 }
363 var req = new XMLHttpRequest ();
364 req.upload.addEventListener ("progress", async_upload_progress, false);
365 req.addEventListener ("error", pop_failure, false);
366 req.addEventListener ("abort", pop_failure, false);
367 req.onreadystatechange = function ()
368 {
369 if (req.readyState == 4 && req.status == 200)
370 {
371 var res = req.responseText;
372 if (res == "Error")
373 {
374 pop_failure ();
375 return;
376 }
377 res = res.split ("\n");
378 var code = res[0]
379 async_global_transfered = async_global_transfering;
380 async_upload_push (code);
381 }
382 }
383 req.open ("POST", async_global_url + 'script.php?push_async' , true);
384
385 var chunk_size = parseInt (async_global_max_size * 0.50);
386 var start = async_global_transfered;
387 var end = start + chunk_size;
388 if (end >= async_global_file.size)
389 end = async_global_file.size;
390 var blob = async_global_file.slice (start, end);
391 async_global_transfering = end;
392
393 var form = new FormData();
394 form.append ("ref", async_global_ref);
395 form.append ("data", blob);
396 form.append ("code", code);
397 req.send (form);
398 }
399
400 function async_upload_end (code)
401 {
402 var req = new XMLHttpRequest ();
403 req.addEventListener ("error", pop_failure, false);
404 req.addEventListener ("abort", pop_failure, false);
405 req.onreadystatechange = function ()
406 {
407 if (req.readyState == 4 && req.status == 200)
408 {
409 var res = req.responseText;
410 if (res == "Error")
411 {
412 pop_failure ();
413 return;
414 }
415 res = res.split ("\n");
416 if (async_global_time != 'none')
417 {
418 var d = new Date();
419 if(!add_time_string_to_date(d))
420 return;
421 show_link (async_global_url, res[0], res[1], res[2], d.toString());
422 }
423 else
424 show_link (async_global_url, res[0], res[1], res[2]);
425 }
426 }
427 req.open ("POST", async_global_url + 'script.php?end_async' , true);
428
429 var form = new FormData();
430 form.append ("ref", async_global_ref);
431 form.append ("code", code);
432 req.send (form);
433 }
434
435 function upload (url, max_size)
436 {
437 if (check_html5_file_api ()
438 && document.getElementById('file_select').files[0].size >= max_size)
439 {
440 async_upload_start (url,
441 max_size,
442 document.getElementById('file_select').files[0],
443 document.getElementById('select_time').value,
444 document.getElementById('input_key').value,
445 document.getElementById('one_time_download').checked,
446 document.getElementById('upload_password').value
447 );
448 }
449 else
450 {
451 classic_upload (url,
452 document.getElementById('file_select').files[0],
453 document.getElementById('select_time').value,
454 document.getElementById('input_key').value,
455 document.getElementById('one_time_download').checked,
456 document.getElementById('upload_password').value
457 );
458 }
459 }
460
461 var upload_time_estimation_total_size = 42;
462 var upload_time_estimation_transfered_size = 42;
463 var upload_time_estimation_transfered_date = 42;
464 var upload_time_estimation_moving_average_speed = 42;
465
466 function upload_time_estimation_init(total_size)
467 {
468 upload_time_estimation_total_size = total_size;
469 upload_time_estimation_transfered_size = 0;
470 upload_time_estimation_moving_average_speed = 0;
471 var d = new Date();
472 upload_time_estimation_transfered_date = d.getTime();
473 }
474
475 function upload_time_estimation_add(total_transfered_size)
476 {
477 // Let's compute the current speed
478 var d = new Date();
479 var speed = upload_time_estimation_moving_average_speed;
480 if (d.getTime() - upload_time_estimation_transfered_date != 0)
481 speed = (total_transfered_size - upload_time_estimation_transfered_size)
482 / (d.getTime() - upload_time_estimation_transfered_date);
483 // Let's compute moving average speed on 30 values
484 var m = (upload_time_estimation_moving_average_speed * 29 + speed) / 30;
485 // Update global values
486 upload_time_estimation_transfered_size = total_transfered_size;
487 upload_time_estimation_transfered_date = d.getTime();
488 upload_time_estimation_moving_average_speed = m;
489 }
490
491 function upload_time_estimation_speed_string()
492 {
493 // speed ms -> s
494 var s = upload_time_estimation_moving_average_speed * 1000;
495 var res = 0;
496 var scale = '';
497 if (s <= 1000)
498 {
499 res = s.toString();
500 scale = "o/s";
501 }
502 else if (s < 1000000)
503 {
504 res = Math.floor(s/100) / 10;
505 scale = "Ko/s";
506 }
507 else
508 {
509 res = Math.floor(s/100000) / 10;
510 scale = "Mo/s";
511 }
512 if (res == 0)
513 return '';
514 return res.toString() + ' ' + scale;
515 }
516
517 function milliseconds_to_time_string (milliseconds)
518 {
519 function numberEnding (number) {
520 return (number > 1) ? 's' : '';
521 }
522
523 var temp = Math.floor(milliseconds / 1000);
524 var years = Math.floor(temp / 31536000);
525 if (years) {
526 return years + ' year' + numberEnding(years);
527 }
528 var days = Math.floor((temp %= 31536000) / 86400);
529 if (days) {
530 return days + ' day' + numberEnding(days);
531 }
532 var hours = Math.floor((temp %= 86400) / 3600);
533 if (hours) {
534 return hours + ' hour' + numberEnding(hours);
535 }
536 var minutes = Math.floor((temp %= 3600) / 60);
537 if (minutes) {
538 return minutes + ' minute' + numberEnding(minutes);
539 }
540 var seconds = temp % 60;
541 if (seconds) {
542 return seconds + ' second' + numberEnding(seconds);
543 }
544 return 'less than a second';
545 }
546
547 function upload_time_estimation_time()
548 {
549 // Estimate remaining time
550 if (upload_time_estimation_moving_average_speed == 0)
551 return 0;
552 return (upload_time_estimation_total_size - upload_time_estimation_transfered_size)
553 / upload_time_estimation_moving_average_speed;
554 }
555
556 var chrono_last_update = 0;
557 var chrono_time_ms = 0;
558 var chrono_time_ms_last_update = 0;
559 function chrono_update(time_ms)
560 {
561 var d = new Date();
562 var chrono = 0;
563 // Don't update too often
564 if (d.getTime() - chrono_last_update < 3000 &&
565 chrono_time_ms_last_update > 0)
566 chrono = chrono_time_ms;
567 else
568 {
569 chrono_last_update = d.getTime();
570 chrono_time_ms = time_ms;
571 chrono = time_ms;
572 chrono_time_ms_last_update = d.getTime();
573 }
574
575 // Adjust chrono for smooth estimation
576 chrono = chrono - (d.getTime() - chrono_time_ms_last_update);
577
578 // Let's update chronometer
579 var time_str = '';
580 if (chrono > 0)
581 time_str = milliseconds_to_time_string (chrono);
582 return time_str;
583 }
584
585 var upload_speed_refresh_limiter_last_update = 0;
586 var upload_speed_refresh_limiter_last_value = '';
587 function upload_speed_refresh_limiter(speed_str)
588 {
589 var d = new Date();
590 if (d.getTime() - upload_speed_refresh_limiter_last_update > 1500)
591 {
592 upload_speed_refresh_limiter_last_value = speed_str;
593 upload_speed_refresh_limiter_last_update = d.getTime();
594 }
595 return upload_speed_refresh_limiter_last_value;
596 }

patrick-canterino.de