3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
5 * Copyright (C) 2015 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 define ('JIRAFEAU_ROOT', dirname (__FILE__
) . '/../');
22 require (JIRAFEAU_ROOT
. 'lib/config.original.php');
23 require (JIRAFEAU_ROOT
. 'lib/settings.php');
24 require (JIRAFEAU_ROOT
. 'lib/functions.php');
25 require (JIRAFEAU_ROOT
. 'lib/lang.php');
28 function translate (expr
)
30 var lang_array
= <?php
echo json_lang_generator () ?
>;
31 if (lang_array
.hasOwnProperty(expr
))
32 return lang_array
[expr
];
36 function show_link (url
, reference
, delete_code
, crypt_key
, date
)
39 document
.getElementById('uploading').style
.display
= 'none';
40 document
.getElementById('upload').style
.display
= 'none';
41 document
.getElementById('upload_finished').style
.display
= '';
42 document
.title
= 'Jirafeau - 100%';
45 var download_link
= url +
'f.php?h=' + reference
;
46 var download_link_href
= url +
'f.php?h=' + reference
;
47 if (crypt_key
.length
> 0)
49 download_link +
= '&k=' + crypt_key
;
50 download_link_href +
= '&k=' + crypt_key
;
52 if (!!document
.getElementById('upload_finished_download_page'))
54 document
.getElementById('upload_link').innerHTML
= download_link
;
55 document
.getElementById('upload_link').href
= download_link_href
;
59 var filename
= document
.getElementById('file_select').files
[0].name
;
60 var b
= encodeURIComponent("Download file \"" + filename +
"\":") +
"%0D";
61 b +
= encodeURIComponent(download_link_href
) +
"%0D";
63 b +
= "%0D" +
encodeURIComponent("This file will be available until " + date
) +
"%0D";
64 document
.getElementById('upload_link_email').href
= "mailto:?body=" + b +
"&subject=" +
encodeURIComponent(filename
);
67 var delete_link
= url +
'f.php?h=' + reference +
'&d=' + delete_code
;
68 var delete_link_href
= url +
'f.php?h=' + reference +
'&d=' + delete_code
;
69 document
.getElementById('delete_link').innerHTML
= delete_link
;
70 document
.getElementById('delete_link').href
= delete_link_href
;
75 document
.getElementById('date').innerHTML
= date
;
76 document
.getElementById('validity').style
.display
= '';
79 document
.getElementById('validity').style
.display
= 'none';
81 // Preview link (if allowed)
82 if (!!document
.getElementById('preview_link'))
84 document
.getElementById('upload_finished_preview').style
.display
= 'none';
85 var preview_link
= url +
'f.php?h=' + reference +
'&p=1';
86 var preview_link_href
= url +
'f.php?h=' + reference +
'&p=1';
87 if (crypt_key
.length
> 0)
89 preview_link +
= '&k=' + crypt_key
;
90 preview_link_href +
= '&k=' + crypt_key
;
93 // Test if content can be previewed
94 type
= document
.getElementById('file_select').files
[0].type
;
95 if (type
.indexOf("image") > -1 ||
96 type
.indexOf("audio") > -1 ||
97 type
.indexOf("text") > -1 ||
98 type
.indexOf("video") > -1)
100 document
.getElementById('preview_link').innerHTML
= preview_link
;
101 document
.getElementById('preview_link').href
= preview_link_href
;
102 document
.getElementById('upload_finished_preview').style
.display
= '';
106 // Direct download link
107 var direct_download_link
= url +
'f.php?h=' + reference +
'&d=1';
108 var direct_download_link_href
= url +
'f.php?h=' + reference +
'&d=1';
109 if (crypt_key
.length
> 0)
111 direct_download_link +
= '&k=' + crypt_key
;
112 direct_download_link_href +
= '&k=' + crypt_key
;
114 document
.getElementById('direct_link').innerHTML
= direct_download_link
;
115 document
.getElementById('direct_link').href
= direct_download_link_href
;
118 // Hide preview and direct download link if password is set
119 if (document
.getElementById('input_key').value
.length
> 0)
121 if (!!document
.getElementById('preview_link'))
122 document
.getElementById('upload_finished_preview').style
.display
= 'none';
123 document
.getElementById('upload_direct_download').style
.display
= 'none';
127 function show_upload_progression (percentage
, speed
, time_left
)
129 document
.getElementById('uploaded_percentage').innerHTML
= percentage
;
130 document
.getElementById('uploaded_speed').innerHTML
= speed
;
131 document
.getElementById('uploaded_time').innerHTML
= time_left
;
132 document
.title
= 'Jirafeau - ' + percentage
;
135 function hide_upload_progression ()
137 document
.getElementById('uploaded_percentage').style
.display
= 'none';
138 document
.getElementById('uploaded_speed').style
.display
= 'none';
139 document
.getElementById('uploaded_time').style
.display
= 'none';
140 document
.title
= 'Jirafeau';
143 function upload_progress (e
)
145 if (e
== undefined || e
== null ||
!e
.lengthComputable
)
148 // Init time estimation if needed
149 if (upload_time_estimation_total_size
== 0)
150 upload_time_estimation_total_size
= e
.total
;
152 // Compute percentage
153 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
156 p_str
= p
.toString() +
'%';
157 // Update estimation speed
158 upload_time_estimation_add(e
.loaded
);
160 var speed_str
= upload_time_estimation_speed_string();
161 speed_str
= upload_speed_refresh_limiter(speed_str
);
163 var time_str
= chrono_update(upload_time_estimation_time());
165 show_upload_progression (p_str
, speed_str
, time_str
);
168 function control_selected_file_size(max_size
, error_str
)
170 f_size
= document
.getElementById('file_select').files
[0].size
;
171 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
173 pop_failure(error_str
);
174 document
.getElementById('send').style
.display
= 'none';
178 document
.getElementById('options').style
.display
= '';
179 document
.getElementById('send').style
.display
= '';
180 document
.getElementById('error_pop').style
.display
= 'none';
181 document
.getElementById('file_select').style
.left
= 'inherit';
182 document
.getElementById('file_select').style
.height
= 'inherit';
183 document
.getElementById('file_select').style
.opacity
= '1';
187 function pop_failure (e
)
189 var text
= "An error occured";
190 if (typeof e
!== 'undefined')
192 text
= "<p>" + text +
"</p>";
193 document
.getElementById('error_pop').innerHTML
= e
;
195 document
.getElementById('uploading').style
.display
= 'none';
196 document
.getElementById('error_pop').style
.display
= '';
197 document
.getElementById('upload').style
.display
= '';
198 document
.getElementById('send').style
.display
= '';
201 function add_time_string_to_date(d
, time
)
203 if(typeof(d
) != 'object' ||
!(d
instanceof Date
))
208 if (time
== 'minute')
210 d
.setSeconds (d
.getSeconds() +
60);
215 d
.setSeconds (d
.getSeconds() +
3600);
220 d
.setSeconds (d
.getSeconds() +
86400);
225 d
.setSeconds (d
.getSeconds() +
604800);
230 d
.setSeconds (d
.getSeconds() +
2419200);
235 d
.setSeconds (d
.getSeconds() +
29030400);
241 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
243 // Delay time estimation init as we can't have file size
244 upload_time_estimation_init(0);
246 var req
= new XMLHttpRequest ();
247 req
.upload
.addEventListener ("progress", upload_progress
, false);
248 req
.addEventListener ("error", pop_failure
, false);
249 req
.addEventListener ("abort", pop_failure
, false);
250 req
.onreadystatechange
= function ()
252 if (req
.readyState
== 4 && req
.status
== 200)
254 var res
= req
.responseText
;
260 res
= res
.split ("\n");
264 if(!add_time_string_to_date(d
, time
))
266 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
269 show_link (url
, res
[0], res
[1], res
[2]);
272 req
.open ("POST", url +
'script.php' , true);
274 var form
= new FormData();
275 form
.append ("file", file
);
277 form
.append ("time", time
);
279 form
.append ("key", password
);
281 form
.append ("one_time_download", '1');
282 if (upload_password
.length
> 0)
283 form
.append ("upload_password", upload_password
);
288 function check_html5_file_api ()
290 return window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
;
293 var async_global_transfered
= 0;
294 var async_global_url
= '';
295 var async_global_file
;
296 var async_global_ref
= '';
297 var async_global_max_size
= 0;
298 var async_global_time
;
299 var async_global_transfering
= 0;
301 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
303 async_global_transfered
= 0;
304 async_global_url
= url
;
305 async_global_file
= file
;
306 async_global_max_size
= max_size
;
307 async_global_time
= time
;
309 var req
= new XMLHttpRequest ();
310 req
.addEventListener ("error", pop_failure
, false);
311 req
.addEventListener ("abort", pop_failure
, false);
312 req
.onreadystatechange
= function ()
314 if (req
.readyState
== 4 && req
.status
== 200)
316 var res
= req
.responseText
;
322 res
= res
.split ("\n");
323 async_global_ref
= res
[0];
325 async_upload_push (code
);
328 req
.open ("POST", async_global_url +
'script.php?init_async' , true);
330 var form
= new FormData();
331 form
.append ("filename", async_global_file
.name
);
332 form
.append ("type", async_global_file
.type
);
334 form
.append ("time", time
);
336 form
.append ("key", password
);
338 form
.append ("one_time_download", '1');
339 if (upload_password
.length
> 0)
340 form
.append ("upload_password", upload_password
);
342 // Start time estimation
343 upload_time_estimation_init(async_global_file
.size
);
348 function async_upload_progress (e
)
350 if (e
== undefined || e
== null ||
!e
.lengthComputable
&& async_global_file
.size
!= 0)
353 // Compute percentage
354 var p
= Math
.round ((e
.loaded + async_global_transfered
) * 100 / (async_global_file
.size
));
357 p_str
= p
.toString() +
'%';
358 // Update estimation speed
359 upload_time_estimation_add(e
.loaded + async_global_transfered
);
361 var speed_str
= upload_time_estimation_speed_string();
362 speed_str
= upload_speed_refresh_limiter(speed_str
);
364 var time_str
= chrono_update(upload_time_estimation_time());
366 show_upload_progression (p_str
, speed_str
, time_str
);
369 function async_upload_push (code
)
371 if (async_global_transfered
== async_global_file
.size
)
373 hide_upload_progression ();
374 async_upload_end (code
);
377 var req
= new XMLHttpRequest ();
378 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
379 req
.addEventListener ("error", pop_failure
, false);
380 req
.addEventListener ("abort", pop_failure
, false);
381 req
.onreadystatechange
= function ()
383 if (req
.readyState
== 4 && req
.status
== 200)
385 var res
= req
.responseText
;
391 res
= res
.split ("\n");
393 async_global_transfered
= async_global_transfering
;
394 async_upload_push (code
);
397 req
.open ("POST", async_global_url +
'script.php?push_async' , true);
399 var chunk_size
= parseInt (async_global_max_size
* 0.50);
400 var start
= async_global_transfered
;
401 var end
= start + chunk_size
;
402 if (end
>= async_global_file
.size
)
403 end
= async_global_file
.size
;
404 var blob
= async_global_file
.slice (start
, end
);
405 async_global_transfering
= end
;
407 var form
= new FormData();
408 form
.append ("ref", async_global_ref
);
409 form
.append ("data", blob
);
410 form
.append ("code", code
);
414 function async_upload_end (code
)
416 var req
= new XMLHttpRequest ();
417 req
.addEventListener ("error", pop_failure
, false);
418 req
.addEventListener ("abort", pop_failure
, false);
419 req
.onreadystatechange
= function ()
421 if (req
.readyState
== 4 && req
.status
== 200)
423 var res
= req
.responseText
;
429 res
= res
.split ("\n");
430 if (async_global_time
!= 'none')
433 if(!add_time_string_to_date(d
, async_global_time
))
435 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
438 show_link (async_global_url
, res
[0], res
[1], res
[2]);
441 req
.open ("POST", async_global_url +
'script.php?end_async' , true);
443 var form
= new FormData();
444 form
.append ("ref", async_global_ref
);
445 form
.append ("code", code
);
449 function upload (url
, max_size
)
451 if (check_html5_file_api ()
452 && document
.getElementById('file_select').files
[0].size
>= max_size
)
454 async_upload_start (url
,
456 document
.getElementById('file_select').files
[0],
457 document
.getElementById('select_time').value
,
458 document
.getElementById('input_key').value
,
459 document
.getElementById('one_time_download').checked
,
460 document
.getElementById('upload_password').value
466 document
.getElementById('file_select').files
[0],
467 document
.getElementById('select_time').value
,
468 document
.getElementById('input_key').value
,
469 document
.getElementById('one_time_download').checked
,
470 document
.getElementById('upload_password').value
475 var upload_time_estimation_total_size
= 42;
476 var upload_time_estimation_transfered_size
= 42;
477 var upload_time_estimation_transfered_date
= 42;
478 var upload_time_estimation_moving_average_speed
= 42;
480 function upload_time_estimation_init(total_size
)
482 upload_time_estimation_total_size
= total_size
;
483 upload_time_estimation_transfered_size
= 0;
484 upload_time_estimation_moving_average_speed
= 0;
486 upload_time_estimation_transfered_date
= d
.getTime();
489 function upload_time_estimation_add(total_transfered_size
)
491 // Let's compute the current speed
493 var speed
= upload_time_estimation_moving_average_speed
;
494 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
495 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
496 / (d
.getTime() - upload_time_estimation_transfered_date
);
497 // Let's compute moving average speed on 30 values
498 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
499 // Update global values
500 upload_time_estimation_transfered_size
= total_transfered_size
;
501 upload_time_estimation_transfered_date
= d
.getTime();
502 upload_time_estimation_moving_average_speed
= m
;
505 function upload_time_estimation_speed_string()
508 var s
= upload_time_estimation_moving_average_speed
* 1000;
516 else if (s
< 1000000)
518 res
= Math
.floor(s
/100) / 10;
523 res
= Math
.floor(s
/100000) / 10;
528 return res
.toString() +
' ' + scale
;
531 function milliseconds_to_time_string (milliseconds
)
533 function numberEnding (number
) {
534 return (number
> 1) ?
's' : '';
537 var temp
= Math
.floor(milliseconds
/ 1000);
538 var years
= Math
.floor(temp
/ 31536000);
540 return years +
' ' +
translate ('year') +
numberEnding(years
);
542 var days
= Math
.floor((temp %
= 31536000) / 86400);
544 return days +
' ' +
translate ('day') +
numberEnding(days
);
546 var hours
= Math
.floor((temp %
= 86400) / 3600);
548 return hours +
' ' +
translate ('hour') +
numberEnding(hours
);
550 var minutes
= Math
.floor((temp %
= 3600) / 60);
552 return minutes +
' ' +
translate ('minute') +
numberEnding(minutes
);
554 var seconds
= temp %
60;
556 return seconds +
' ' +
translate ('second') +
numberEnding(seconds
);
558 return translate ('less than a second');
561 function upload_time_estimation_time()
563 // Estimate remaining time
564 if (upload_time_estimation_moving_average_speed
== 0)
566 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
567 / upload_time_estimation_moving_average_speed
;
570 var chrono_last_update
= 0;
571 var chrono_time_ms
= 0;
572 var chrono_time_ms_last_update
= 0;
573 function chrono_update(time_ms
)
577 // Don't update too often
578 if (d
.getTime() - chrono_last_update
< 3000 &&
579 chrono_time_ms_last_update
> 0)
580 chrono
= chrono_time_ms
;
583 chrono_last_update
= d
.getTime();
584 chrono_time_ms
= time_ms
;
586 chrono_time_ms_last_update
= d
.getTime();
589 // Adjust chrono for smooth estimation
590 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
592 // Let's update chronometer
595 time_str
= milliseconds_to_time_string (chrono
);
599 var upload_speed_refresh_limiter_last_update
= 0;
600 var upload_speed_refresh_limiter_last_value
= '';
601 function upload_speed_refresh_limiter(speed_str
)
604 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
606 upload_speed_refresh_limiter_last_value
= speed_str
;
607 upload_speed_refresh_limiter_last_update
= d
.getTime();
609 return upload_speed_refresh_limiter_last_value
;