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>
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.
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.
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/>.
20 function show_link (url
, reference
, delete_code
, crypt_key
, date
)
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%';
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)
33 download_link
+= '&k=' + crypt_key
;
34 download_link_href
+= '&k=' + crypt_key
;
36 if (!!document
.getElementById('upload_finished_download_page'))
38 document
.getElementById('upload_link').innerHTML
= download_link
;
39 document
.getElementById('upload_link').href
= download_link_href
;
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";
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
);
51 var delete_link
= url
+ 'f.php?h=' + reference
+ '&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
;
59 document
.getElementById('date').innerHTML
= date
;
60 document
.getElementById('validity').style
.display
= '';
63 document
.getElementById('validity').style
.display
= 'none';
65 // Preview link (if allowed)
66 if (!!document
.getElementById('preview_link'))
68 document
.getElementById('upload_finished_preview').style
.display
= 'none';
69 var preview_link
= url
+ 'f.php?h=' + reference
+ '&p=1';
70 var preview_link_href
= url
+ 'f.php?h=' + reference
+ '&p=1';
71 if (crypt_key
.length
> 0)
73 preview_link
+= '&k=' + crypt_key
;
74 preview_link_href
+= '&k=' + crypt_key
;
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)
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
= '';
90 // Direct download link
91 var direct_download_link
= url
+ 'f.php?h=' + reference
+ '&d=1';
92 var direct_download_link_href
= url
+ 'f.php?h=' + reference
+ '&d=1';
93 if (crypt_key
.length
> 0)
95 direct_download_link
+= '&k=' + crypt_key
;
96 direct_download_link_href
+= '&k=' + crypt_key
;
98 document
.getElementById('direct_link').innerHTML
= direct_download_link
;
99 document
.getElementById('direct_link').href
= direct_download_link_href
;
102 // Hide preview and direct download link if password is set
103 if (document
.getElementById('input_key').value
.length
> 0)
105 if (!!document
.getElementById('preview_link'))
106 document
.getElementById('upload_finished_preview').style
.display
= 'none';
107 document
.getElementById('upload_direct_download').style
.display
= 'none';
111 function show_upload_progression (percentage
, speed
, time_left
)
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
;
119 function hide_upload_progression ()
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';
127 function upload_progress (e
)
129 if (e
== undefined || e
== null || !e
.lengthComputable
)
132 // Init time estimation if needed
133 if (upload_time_estimation_total_size
== 0)
134 upload_time_estimation_total_size
= e
.total
;
136 // Compute percentage
137 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
140 p_str
= p
.toString() + '%';
141 // Update estimation speed
142 upload_time_estimation_add(e
.loaded
);
144 var speed_str
= upload_time_estimation_speed_string();
145 speed_str
= upload_speed_refresh_limiter(speed_str
);
147 var time_str
= chrono_update(upload_time_estimation_time());
149 show_upload_progression (p_str
, speed_str
, time_str
);
152 function control_selected_file_size(max_size
, error_str
)
154 f_size
= document
.getElementById('file_select').files
[0].size
;
155 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
157 pop_failure(error_str
);
158 document
.getElementById('send').style
.display
= 'none';
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';
171 function pop_failure (e
)
173 var text
= "An error occured";
174 if (typeof e
!== 'undefined')
176 text
= "<p>" + text
+ "</p>";
177 document
.getElementById('error_pop').innerHTML
= e
;
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
= '';
185 function add_time_string_to_date(d
, time_string
)
187 if(typeof(d
) != 'object' || !(d
instanceof Date
))
192 if (time
== 'minute')
194 d
.setSeconds (d
.getSeconds() + 60);
199 d
.setSeconds (d
.getSeconds() + 3600);
204 d
.setSeconds (d
.getSeconds() + 86400);
209 d
.setSeconds (d
.getSeconds() + 604800);
214 d
.setSeconds (d
.getSeconds() + 2419200);
219 d
.setSeconds (d
.getSeconds() + 29030400);
225 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
227 // Delay time estimation init as we can't have file size
228 upload_time_estimation_init(0);
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 ()
236 if (req
.readyState
== 4 && req
.status
== 200)
238 var res
= req
.responseText
;
244 res
= res
.split ("\n");
248 if(!add_time_string_to_date(d
))
252 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
255 show_link (url
, res
[0], res
[1], res
[2]);
258 req
.open ("POST", url
+ 'script.php' , true);
260 var form
= new FormData();
261 form
.append ("file", file
);
263 form
.append ("time", time
);
265 form
.append ("key", password
);
267 form
.append ("one_time_download", '1');
268 if (upload_password
.length
> 0)
269 form
.append ("upload_password", upload_password
);
274 function check_html5_file_api ()
276 return window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
;
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;
287 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
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
;
295 var req
= new XMLHttpRequest ();
296 req
.addEventListener ("error", pop_failure
, false);
297 req
.addEventListener ("abort", pop_failure
, false);
298 req
.onreadystatechange = function ()
300 if (req
.readyState
== 4 && req
.status
== 200)
302 var res
= req
.responseText
;
308 res
= res
.split ("\n");
309 async_global_ref
= res
[0];
311 async_upload_push (code
);
314 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
316 var form
= new FormData();
317 form
.append ("filename", async_global_file
.name
);
318 form
.append ("type", async_global_file
.type
);
320 form
.append ("time", time
);
322 form
.append ("key", password
);
324 form
.append ("one_time_download", '1');
325 if (upload_password
.length
> 0)
326 form
.append ("upload_password", upload_password
);
328 // Start time estimation
329 upload_time_estimation_init(async_global_file
.size
);
334 function async_upload_progress (e
)
336 if (e
== undefined || e
== null || !e
.lengthComputable
&& async_global_file
.size
!= 0)
339 // Compute percentage
340 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
343 p_str
= p
.toString() + '%';
344 // Update estimation speed
345 upload_time_estimation_add(e
.loaded
+ async_global_transfered
);
347 var speed_str
= upload_time_estimation_speed_string();
348 speed_str
= upload_speed_refresh_limiter(speed_str
);
350 var time_str
= chrono_update(upload_time_estimation_time());
352 show_upload_progression (p_str
, speed_str
, time_str
);
355 function async_upload_push (code
)
357 if (async_global_transfered
== async_global_file
.size
)
359 hide_upload_progression ();
360 async_upload_end (code
);
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 ()
369 if (req
.readyState
== 4 && req
.status
== 200)
371 var res
= req
.responseText
;
377 res
= res
.split ("\n");
379 async_global_transfered
= async_global_transfering
;
380 async_upload_push (code
);
383 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
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
;
393 var form
= new FormData();
394 form
.append ("ref", async_global_ref
);
395 form
.append ("data", blob
);
396 form
.append ("code", code
);
400 function async_upload_end (code
)
402 var req
= new XMLHttpRequest ();
403 req
.addEventListener ("error", pop_failure
, false);
404 req
.addEventListener ("abort", pop_failure
, false);
405 req
.onreadystatechange = function ()
407 if (req
.readyState
== 4 && req
.status
== 200)
409 var res
= req
.responseText
;
415 res
= res
.split ("\n");
416 if (async_global_time
!= 'none')
419 if(!add_time_string_to_date(d
))
421 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
424 show_link (async_global_url
, res
[0], res
[1], res
[2]);
427 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
429 var form
= new FormData();
430 form
.append ("ref", async_global_ref
);
431 form
.append ("code", code
);
435 function upload (url
, max_size
)
437 if (check_html5_file_api ()
438 && document
.getElementById('file_select').files
[0].size
>= max_size
)
440 async_upload_start (url
,
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
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
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;
466 function upload_time_estimation_init(total_size
)
468 upload_time_estimation_total_size
= total_size
;
469 upload_time_estimation_transfered_size
= 0;
470 upload_time_estimation_moving_average_speed
= 0;
472 upload_time_estimation_transfered_date
= d
.getTime();
475 function upload_time_estimation_add(total_transfered_size
)
477 // Let's compute the current speed
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
;
491 function upload_time_estimation_speed_string()
494 var s
= upload_time_estimation_moving_average_speed
* 1000;
502 else if (s
< 1000000)
504 res
= Math
.floor(s
/100) / 10;
509 res
= Math
.floor(s
/100000) / 10;
514 return res
.toString() + ' ' + scale
;
517 function milliseconds_to_time_string (milliseconds
)
519 function numberEnding (number
) {
520 return (number
> 1) ? 's' : '';
523 var temp
= Math
.floor(milliseconds
/ 1000);
524 var years
= Math
.floor(temp
/ 31536000);
526 return years
+ ' year' + numberEnding(years
);
528 var days
= Math
.floor((temp
%= 31536000) / 86400);
530 return days
+ ' day' + numberEnding(days
);
532 var hours
= Math
.floor((temp
%= 86400) / 3600);
534 return hours
+ ' hour' + numberEnding(hours
);
536 var minutes
= Math
.floor((temp
%= 3600) / 60);
538 return minutes
+ ' minute' + numberEnding(minutes
);
540 var seconds
= temp
% 60;
542 return seconds
+ ' second' + numberEnding(seconds
);
544 return 'less than a second';
547 function upload_time_estimation_time()
549 // Estimate remaining time
550 if (upload_time_estimation_moving_average_speed
== 0)
552 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
553 / upload_time_estimation_moving_average_speed
;
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
)
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
;
569 chrono_last_update
= d
.getTime();
570 chrono_time_ms
= time_ms
;
572 chrono_time_ms_last_update
= d
.getTime();
575 // Adjust chrono for smooth estimation
576 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
578 // Let's update chronometer
581 time_str
= milliseconds_to_time_string (chrono
);
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
)
590 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
592 upload_speed_refresh_limiter_last_value
= speed_str
;
593 upload_speed_refresh_limiter_last_update
= d
.getTime();
595 return upload_speed_refresh_limiter_last_value
;