2 * Jirafeau, your web file repository
3 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
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.
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.
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/>.
19 function show_link (url
, reference
, delete_code
, crypt_key
, date
)
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%';
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)
32 download_link
+= '&k=' + crypt_key
;
33 download_link_href
+= '&k=' + crypt_key
;
35 if (!!document
.getElementById('upload_finished_download_page'))
37 document
.getElementById('upload_link').innerHTML
= download_link
;
38 document
.getElementById('upload_link').href
= download_link_href
;
42 var filename
= document
.getElementById('file_select').files
[0].name
;
43 var b
= "Download%20%22" + filename
+ "%22 :%0D" + download_link_href
+ "%0D";
44 document
.getElementById('upload_link_email').href
= "mailto:?body=" + b
+ "&subject=" + filename
;
47 var delete_link
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
48 var delete_link_href
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
49 document
.getElementById('delete_link').innerHTML
= delete_link
;
50 document
.getElementById('delete_link').href
= delete_link_href
;
55 document
.getElementById('date').innerHTML
= date
;
56 document
.getElementById('validity').style
.display
= '';
59 document
.getElementById('validity').style
.display
= 'none';
61 // Preview link (if allowed)
62 if (!!document
.getElementById('preview_link'))
64 document
.getElementById('upload_finished_preview').style
.display
= 'none';
65 var preview_link
= url
+ 'f.php?h=' + reference
+ '&p=1';
66 var preview_link_href
= url
+ 'f.php?h=' + reference
+ '&p=1';
67 if (crypt_key
.length
> 0)
69 preview_link
+= '&k=' + crypt_key
;
70 preview_link_href
+= '&k=' + crypt_key
;
73 // Test if content can be previewed
74 type
= document
.getElementById('file_select').files
[0].type
;
75 if (type
.indexOf("image") > -1 ||
76 type
.indexOf("audio") > -1 ||
77 type
.indexOf("text") > -1 ||
78 type
.indexOf("video") > -1)
80 document
.getElementById('preview_link').innerHTML
= preview_link
;
81 document
.getElementById('preview_link').href
= preview_link_href
;
82 document
.getElementById('upload_finished_preview').style
.display
= '';
86 // Direct download link
87 var direct_download_link
= url
+ 'f.php?h=' + reference
+ '&d=1';
88 var direct_download_link_href
= url
+ 'f.php?h=' + reference
+ '&d=1';
89 if (crypt_key
.length
> 0)
91 direct_download_link
+= '&k=' + crypt_key
;
92 direct_download_link_href
+= '&k=' + crypt_key
;
94 document
.getElementById('direct_link').innerHTML
= direct_download_link
;
95 document
.getElementById('direct_link').href
= direct_download_link_href
;
98 // Hide preview and direct download link if password is set
99 if (document
.getElementById('input_key').value
.length
> 0)
101 if (!!document
.getElementById('preview_link'))
102 document
.getElementById('upload_finished_preview').style
.display
= 'none';
103 document
.getElementById('upload_direct_download').style
.display
= 'none';
107 function show_upload_progression (percentage
, speed
, time_left
)
109 document
.getElementById('uploaded_percentage').innerHTML
= percentage
;
110 document
.getElementById('uploaded_speed').innerHTML
= speed
;
111 document
.getElementById('uploaded_time').innerHTML
= time_left
;
112 document
.title
= 'Jirafeau - ' + percentage
;
115 function hide_upload_progression ()
117 document
.getElementById('uploaded_percentage').style
.display
= 'none';
118 document
.getElementById('uploaded_speed').style
.display
= 'none';
119 document
.getElementById('uploaded_time').style
.display
= 'none';
120 document
.title
= 'Jirafeau';
123 function upload_progress (e
)
125 if (!e
.lengthComputable
)
128 // Init time estimation if needed
129 if (upload_time_estimation_total_size
== 0)
130 upload_time_estimation_total_size
= e
.total
;
132 // Compute percentage
133 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
136 p_str
= p
.toString() + '%';
137 // Update estimation speed
138 upload_time_estimation_add(e
.loaded
);
140 var speed_str
= upload_time_estimation_speed_string();
141 speed_str
= upload_speed_refresh_limiter(speed_str
);
143 var time_str
= chrono_update(upload_time_estimation_time());
145 show_upload_progression (p_str
, speed_str
, time_str
);
148 function control_selected_file_size(max_size
, error_str
)
150 f_size
= document
.getElementById('file_select').files
[0].size
;
151 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
153 pop_failure(error_str
);
154 document
.getElementById('send').style
.display
= 'none';
158 document
.getElementById('options').style
.display
= '';
159 document
.getElementById('send').style
.display
= '';
160 document
.getElementById('error_pop').style
.display
= 'none';
164 function pop_failure (e
)
166 var text
= "An error occured";
167 if (typeof e
!== 'undefined')
169 text
= "<p>" + text
+ "</p>";
170 document
.getElementById('error_pop').innerHTML
= e
;
172 document
.getElementById('uploading').style
.display
= 'none';
173 document
.getElementById('error_pop').style
.display
= '';
174 document
.getElementById('upload').style
.display
= '';
175 document
.getElementById('send').style
.display
= '';
178 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
180 // Delay time estimation init as we can't have file size
181 upload_time_estimation_init(0);
183 var req
= new XMLHttpRequest ();
184 req
.upload
.addEventListener ("progress", upload_progress
, false);
185 req
.addEventListener ("error", pop_failure
, false);
186 req
.addEventListener ("abort", pop_failure
, false);
187 req
.onreadystatechange = function ()
189 if (req
.readyState
== 4 && req
.status
== 200)
191 var res
= req
.responseText
;
197 res
= res
.split ("\n");
201 if (time
== 'minute')
202 d
.setSeconds (d
.getSeconds() + 60);
203 else if (time
== 'hour')
204 d
.setSeconds (d
.getSeconds() + 3600);
205 else if (time
== 'day')
206 d
.setSeconds (d
.getSeconds() + 86400);
207 else if (time
== 'week')
208 d
.setSeconds (d
.getSeconds() + 604800);
209 else if (time
== 'month')
210 d
.setSeconds (d
.getSeconds() + 2419200);
211 else if (time
== 'year')
212 d
.setSeconds (d
.getSeconds() + 29030400);
215 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
218 show_link (url
, res
[0], res
[1], res
[2]);
221 req
.open ("POST", url
+ 'script.php' , true);
223 var form
= new FormData();
224 form
.append ("file", file
);
226 form
.append ("time", time
);
228 form
.append ("key", password
);
230 form
.append ("one_time_download", '1');
231 if (upload_password
.length
> 0)
232 form
.append ("upload_password", upload_password
);
237 function check_html5_file_api ()
239 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
244 var async_global_transfered
= 0;
245 var async_global_url
= '';
246 var async_global_file
;
247 var async_global_ref
= '';
248 var async_global_max_size
= 0;
249 var async_global_time
;
250 var async_global_transfering
= 0;
252 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
254 async_global_transfered
= 0;
255 async_global_url
= url
;
256 async_global_file
= file
;
257 async_global_max_size
= max_size
;
258 async_global_time
= time
;
260 var req
= new XMLHttpRequest ();
261 req
.addEventListener ("error", pop_failure
, false);
262 req
.addEventListener ("abort", pop_failure
, false);
263 req
.onreadystatechange = function ()
265 if (req
.readyState
== 4 && req
.status
== 200)
267 var res
= req
.responseText
;
273 res
= res
.split ("\n");
274 async_global_ref
= res
[0];
276 async_upload_push (code
);
279 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
281 var form
= new FormData();
282 form
.append ("filename", async_global_file
.name
);
283 form
.append ("type", async_global_file
.type
);
285 form
.append ("time", time
);
287 form
.append ("key", password
);
289 form
.append ("one_time_download", '1');
290 if (upload_password
.length
> 0)
291 form
.append ("upload_password", upload_password
);
293 // Start time estimation
294 upload_time_estimation_init(async_global_file
.size
);
299 function async_upload_progress (e
)
301 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
304 // Compute percentage
305 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
308 p_str
= p
.toString() + '%';
309 // Update estimation speed
310 upload_time_estimation_add(e
.loaded
+ async_global_transfered
);
312 var speed_str
= upload_time_estimation_speed_string();
313 speed_str
= upload_speed_refresh_limiter(speed_str
);
315 var time_str
= chrono_update(upload_time_estimation_time());
317 show_upload_progression (p_str
, speed_str
, time_str
);
320 function async_upload_push (code
)
322 if (async_global_transfered
== async_global_file
.size
)
324 hide_upload_progression ();
325 async_upload_end (code
);
328 var req
= new XMLHttpRequest ();
329 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
330 req
.addEventListener ("error", pop_failure
, false);
331 req
.addEventListener ("abort", pop_failure
, false);
332 req
.onreadystatechange = function ()
334 if (req
.readyState
== 4 && req
.status
== 200)
336 var res
= req
.responseText
;
342 res
= res
.split ("\n");
344 async_global_transfered
= async_global_transfering
;
345 async_upload_push (code
);
348 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
350 var chunk_size
= parseInt (async_global_max_size
* 0.50);
351 var start
= async_global_transfered
;
352 var end
= start
+ chunk_size
;
353 if (end
>= async_global_file
.size
)
354 end
= async_global_file
.size
;
355 var blob
= async_global_file
.slice (start
, end
);
356 async_global_transfering
= end
;
358 var form
= new FormData();
359 form
.append ("ref", async_global_ref
);
360 form
.append ("data", blob
);
361 form
.append ("code", code
);
365 function async_upload_end (code
)
367 var req
= new XMLHttpRequest ();
368 req
.addEventListener ("error", pop_failure
, false);
369 req
.addEventListener ("abort", pop_failure
, false);
370 req
.onreadystatechange = function ()
372 if (req
.readyState
== 4 && req
.status
== 200)
374 var res
= req
.responseText
;
380 res
= res
.split ("\n");
381 if (async_global_time
!= 'none')
384 if (async_global_time
== 'minute')
385 d
.setSeconds (d
.getSeconds() + 60);
386 else if (async_global_time
== 'hour')
387 d
.setSeconds (d
.getSeconds() + 3600);
388 else if (async_global_time
== 'day')
389 d
.setSeconds (d
.getSeconds() + 86400);
390 else if (async_global_time
== 'week')
391 d
.setSeconds (d
.getSeconds() + 604800);
392 else if (async_global_time
== 'month')
393 d
.setSeconds (d
.getSeconds() + 2419200);
394 else if (async_global_time
== 'year')
395 d
.setSeconds (d
.getSeconds() + 29030400);
398 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
401 show_link (async_global_url
, res
[0], res
[1], res
[2]);
404 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
406 var form
= new FormData();
407 form
.append ("ref", async_global_ref
);
408 form
.append ("code", code
);
412 function upload (url
, max_size
)
414 if (check_html5_file_api ()
415 && document
.getElementById('file_select').files
[0].size
>= max_size
)
417 async_upload_start (url
,
419 document
.getElementById('file_select').files
[0],
420 document
.getElementById('select_time').value
,
421 document
.getElementById('input_key').value
,
422 document
.getElementById('one_time_download').checked
,
423 document
.getElementById('upload_password').value
429 document
.getElementById('file_select').files
[0],
430 document
.getElementById('select_time').value
,
431 document
.getElementById('input_key').value
,
432 document
.getElementById('one_time_download').checked
,
433 document
.getElementById('upload_password').value
438 var upload_time_estimation_total_size
= 42;
439 var upload_time_estimation_transfered_size
= 42;
440 var upload_time_estimation_transfered_date
= 42;
441 var upload_time_estimation_moving_average_speed
= 42;
443 function upload_time_estimation_init(total_size
)
445 upload_time_estimation_total_size
= total_size
;
446 upload_time_estimation_transfered_size
= 0;
447 upload_time_estimation_moving_average_speed
= 0;
449 upload_time_estimation_transfered_date
= d
.getTime();
452 function upload_time_estimation_add(total_transfered_size
)
454 // Let's compute the current speed
456 var speed
= upload_time_estimation_moving_average_speed
;
457 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
458 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
459 / (d
.getTime() - upload_time_estimation_transfered_date
);
460 // Let's compute moving average speed on 30 values
461 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
462 // Update global values
463 upload_time_estimation_transfered_size
= total_transfered_size
;
464 upload_time_estimation_transfered_date
= d
.getTime();
465 upload_time_estimation_moving_average_speed
= m
;
468 function upload_time_estimation_speed_string()
471 var s
= upload_time_estimation_moving_average_speed
* 1000;
479 else if (s
< 1000000)
481 res
= Math
.floor(s
/100) / 10;
486 res
= Math
.floor(s
/100000) / 10;
492 return res
.toString() + ' ' + scale
;
495 function milliseconds_to_time_string (milliseconds
)
497 function numberEnding (number
) {
498 return (number
> 1) ? 's' : '';
501 var temp
= Math
.floor(milliseconds
/ 1000);
502 var years
= Math
.floor(temp
/ 31536000);
504 return years
+ ' year' + numberEnding(years
);
506 var days
= Math
.floor((temp
%= 31536000) / 86400);
508 return days
+ ' day' + numberEnding(days
);
510 var hours
= Math
.floor((temp
%= 86400) / 3600);
512 return hours
+ ' hour' + numberEnding(hours
);
514 var minutes
= Math
.floor((temp
%= 3600) / 60);
516 return minutes
+ ' minute' + numberEnding(minutes
);
518 var seconds
= temp
% 60;
520 return seconds
+ ' second' + numberEnding(seconds
);
522 return 'less than a second';
525 function upload_time_estimation_time()
527 // Estimate remaining time
528 if (upload_time_estimation_moving_average_speed
== 0)
530 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
531 / upload_time_estimation_moving_average_speed
;
534 var chrono_last_update
= 0;
535 var chrono_time_ms
= 0;
536 var chrono_time_ms_last_update
= 0;
537 function chrono_update(time_ms
)
541 // Don't update too often
542 if (d
.getTime() - chrono_last_update
< 3000 &&
543 chrono_time_ms_last_update
> 0)
544 chrono
= chrono_time_ms
;
547 chrono_last_update
= d
.getTime();
548 chrono_time_ms
= time_ms
;
550 chrono_time_ms_last_update
= d
.getTime();
553 // Adjust chrono for smooth estimation
554 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
556 // Let's update chronometer
559 time_str
= milliseconds_to_time_string (chrono
);
563 var upload_speed_refresh_limiter_last_update
= 0;
564 var upload_speed_refresh_limiter_last_value
= '';
565 function upload_speed_refresh_limiter(speed_str
)
568 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
570 upload_speed_refresh_limiter_last_value
= speed_str
;
571 upload_speed_refresh_limiter_last_update
= d
.getTime();
573 return upload_speed_refresh_limiter_last_value
;