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 delete_link
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
43 var delete_link_href
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
44 document
.getElementById('delete_link').innerHTML
= delete_link
;
45 document
.getElementById('delete_link').href
= delete_link_href
;
50 document
.getElementById('date').innerHTML
= date
;
51 document
.getElementById('validity').style
.display
= '';
54 document
.getElementById('validity').style
.display
= 'none';
56 // Preview link (if allowed)
57 if (!!document
.getElementById('preview_link'))
59 document
.getElementById('upload_finished_preview').style
.display
= 'none';
60 var preview_link
= url
+ 'f.php?h=' + reference
+ '&p=1';
61 var preview_link_href
= url
+ 'f.php?h=' + reference
+ '&p=1';
62 if (crypt_key
.length
> 0)
64 preview_link
+= '&k=' + crypt_key
;
65 preview_link_href
+= '&k=' + crypt_key
;
68 // Test if content can be previewed
69 type
= document
.getElementById('file_select').files
[0].type
;
70 if (type
.indexOf("image") > -1 ||
71 type
.indexOf("audio") > -1 ||
72 type
.indexOf("text") > -1 ||
73 type
.indexOf("video") > -1)
75 document
.getElementById('preview_link').innerHTML
= preview_link
;
76 document
.getElementById('preview_link').href
= preview_link_href
;
77 document
.getElementById('upload_finished_preview').style
.display
= '';
81 // Direct download link
82 var direct_download_link
= url
+ 'f.php?h=' + reference
+ '&d=1';
83 var direct_download_link_href
= url
+ 'f.php?h=' + reference
+ '&d=1';
84 if (crypt_key
.length
> 0)
86 direct_download_link
+= '&k=' + crypt_key
;
87 direct_download_link_href
+= '&k=' + crypt_key
;
89 document
.getElementById('direct_link').innerHTML
= direct_download_link
;
90 document
.getElementById('direct_link').href
= direct_download_link_href
;
93 // Hide preview and direct download link if password is set
94 if (document
.getElementById('input_key').value
.length
> 0)
96 if (!!document
.getElementById('preview_link'))
97 document
.getElementById('upload_finished_preview').style
.display
= 'none';
98 document
.getElementById('upload_direct_download').style
.display
= 'none';
102 function show_upload_progression (percentage
, speed
, time_left
)
104 document
.getElementById('uploaded_percentage').innerHTML
= percentage
;
105 document
.getElementById('uploaded_speed').innerHTML
= speed
;
106 document
.getElementById('uploaded_time').innerHTML
= time_left
;
107 document
.title
= 'Jirafeau - ' + percentage
;
110 function hide_upload_progression ()
112 document
.getElementById('uploaded_percentage').style
.display
= 'none';
113 document
.getElementById('uploaded_speed').style
.display
= 'none';
114 document
.getElementById('uploaded_time').style
.display
= 'none';
115 document
.title
= 'Jirafeau';
118 function upload_progress (e
)
120 if (!e
.lengthComputable
)
123 // Init time estimation if needed
124 if (upload_time_estimation_total_size
== 0)
125 upload_time_estimation_total_size
= e
.total
;
127 // Compute percentage
128 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
131 p_str
= p
.toString() + '%';
132 // Update estimation speed
133 upload_time_estimation_add(e
.loaded
);
135 var speed_str
= upload_time_estimation_speed_string();
136 speed_str
= upload_speed_refresh_limiter(speed_str
);
138 var time_str
= chrono_update(upload_time_estimation_time());
140 show_upload_progression (p_str
, speed_str
, time_str
);
143 function control_selected_file_size(max_size
, error_str
)
145 f_size
= document
.getElementById('file_select').files
[0].size
;
146 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
148 pop_failure(error_str
);
149 document
.getElementById('send').style
.display
= 'none';
153 document
.getElementById('options').style
.display
= '';
154 document
.getElementById('send').style
.display
= '';
155 document
.getElementById('error_pop').style
.display
= 'none';
159 function pop_failure (e
)
161 var text
= "An error occured";
162 if (typeof e
!== 'undefined')
164 text
= "<p>" + text
+ "</p>";
165 document
.getElementById('error_pop').innerHTML
= e
;
167 document
.getElementById('uploading').style
.display
= 'none';
168 document
.getElementById('error_pop').style
.display
= '';
169 document
.getElementById('upload').style
.display
= '';
170 document
.getElementById('send').style
.display
= '';
173 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
175 // Delay time estimation init as we can't have file size
176 upload_time_estimation_init(0);
178 var req
= new XMLHttpRequest ();
179 req
.upload
.addEventListener ("progress", upload_progress
, false);
180 req
.addEventListener ("error", pop_failure
, false);
181 req
.addEventListener ("abort", pop_failure
, false);
182 req
.onreadystatechange = function ()
184 if (req
.readyState
== 4 && req
.status
== 200)
186 var res
= req
.responseText
;
192 res
= res
.split ("\n");
196 if (time
== 'minute')
197 d
.setSeconds (d
.getSeconds() + 60);
198 else if (time
== 'hour')
199 d
.setSeconds (d
.getSeconds() + 3600);
200 else if (time
== 'day')
201 d
.setSeconds (d
.getSeconds() + 86400);
202 else if (time
== 'week')
203 d
.setSeconds (d
.getSeconds() + 604800);
204 else if (time
== 'month')
205 d
.setSeconds (d
.getSeconds() + 2419200);
206 else if (time
== 'year')
207 d
.setSeconds (d
.getSeconds() + 29030400);
210 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
213 show_link (url
, res
[0], res
[1], res
[2]);
216 req
.open ("POST", url
+ 'script.php' , true);
218 var form
= new FormData();
219 form
.append ("file", file
);
221 form
.append ("time", time
);
223 form
.append ("key", password
);
225 form
.append ("one_time_download", '1');
226 if (upload_password
.length
> 0)
227 form
.append ("upload_password", upload_password
);
232 function check_html5_file_api ()
234 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
239 var async_global_transfered
= 0;
240 var async_global_url
= '';
241 var async_global_file
;
242 var async_global_ref
= '';
243 var async_global_max_size
= 0;
244 var async_global_time
;
245 var async_global_transfering
= 0;
247 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
249 async_global_transfered
= 0;
250 async_global_url
= url
;
251 async_global_file
= file
;
252 async_global_max_size
= max_size
;
253 async_global_time
= time
;
255 var req
= new XMLHttpRequest ();
256 req
.addEventListener ("error", pop_failure
, false);
257 req
.addEventListener ("abort", pop_failure
, false);
258 req
.onreadystatechange = function ()
260 if (req
.readyState
== 4 && req
.status
== 200)
262 var res
= req
.responseText
;
268 res
= res
.split ("\n");
269 async_global_ref
= res
[0];
271 async_upload_push (code
);
274 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
276 var form
= new FormData();
277 form
.append ("filename", async_global_file
.name
);
278 form
.append ("type", async_global_file
.type
);
280 form
.append ("time", time
);
282 form
.append ("key", password
);
284 form
.append ("one_time_download", '1');
285 if (upload_password
.length
> 0)
286 form
.append ("upload_password", upload_password
);
288 // Start time estimation
289 upload_time_estimation_init(async_global_file
.size
);
294 function async_upload_progress (e
)
296 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
299 // Compute percentage
300 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
303 p_str
= p
.toString() + '%';
304 // Update estimation speed
305 upload_time_estimation_add(e
.loaded
+ async_global_transfered
);
307 var speed_str
= upload_time_estimation_speed_string();
308 speed_str
= upload_speed_refresh_limiter(speed_str
);
310 var time_str
= chrono_update(upload_time_estimation_time());
312 show_upload_progression (p_str
, speed_str
, time_str
);
315 function async_upload_push (code
)
317 if (async_global_transfered
== async_global_file
.size
)
319 hide_upload_progression ();
320 async_upload_end (code
);
323 var req
= new XMLHttpRequest ();
324 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
325 req
.addEventListener ("error", pop_failure
, false);
326 req
.addEventListener ("abort", pop_failure
, false);
327 req
.onreadystatechange = function ()
329 if (req
.readyState
== 4 && req
.status
== 200)
331 var res
= req
.responseText
;
337 res
= res
.split ("\n");
339 async_global_transfered
= async_global_transfering
;
340 async_upload_push (code
);
343 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
345 var chunk_size
= parseInt (async_global_max_size
* 0.50);
346 var start
= async_global_transfered
;
347 var end
= start
+ chunk_size
;
348 if (end
>= async_global_file
.size
)
349 end
= async_global_file
.size
;
350 var blob
= async_global_file
.slice (start
, end
);
351 async_global_transfering
= end
;
353 var form
= new FormData();
354 form
.append ("ref", async_global_ref
);
355 form
.append ("data", blob
);
356 form
.append ("code", code
);
360 function async_upload_end (code
)
362 var req
= new XMLHttpRequest ();
363 req
.addEventListener ("error", pop_failure
, false);
364 req
.addEventListener ("abort", pop_failure
, false);
365 req
.onreadystatechange = function ()
367 if (req
.readyState
== 4 && req
.status
== 200)
369 var res
= req
.responseText
;
375 res
= res
.split ("\n");
376 if (async_global_time
!= 'none')
379 if (async_global_time
== 'minute')
380 d
.setSeconds (d
.getSeconds() + 60);
381 else if (async_global_time
== 'hour')
382 d
.setSeconds (d
.getSeconds() + 3600);
383 else if (async_global_time
== 'day')
384 d
.setSeconds (d
.getSeconds() + 86400);
385 else if (async_global_time
== 'week')
386 d
.setSeconds (d
.getSeconds() + 604800);
387 else if (async_global_time
== 'month')
388 d
.setSeconds (d
.getSeconds() + 2419200);
389 else if (async_global_time
== 'year')
390 d
.setSeconds (d
.getSeconds() + 29030400);
393 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
396 show_link (async_global_url
, res
[0], res
[1], res
[2]);
399 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
401 var form
= new FormData();
402 form
.append ("ref", async_global_ref
);
403 form
.append ("code", code
);
407 function upload (url
, max_size
)
409 if (check_html5_file_api ()
410 && document
.getElementById('file_select').files
[0].size
>= max_size
)
412 async_upload_start (url
,
414 document
.getElementById('file_select').files
[0],
415 document
.getElementById('select_time').value
,
416 document
.getElementById('input_key').value
,
417 document
.getElementById('one_time_download').checked
,
418 document
.getElementById('upload_password').value
424 document
.getElementById('file_select').files
[0],
425 document
.getElementById('select_time').value
,
426 document
.getElementById('input_key').value
,
427 document
.getElementById('one_time_download').checked
,
428 document
.getElementById('upload_password').value
433 var upload_time_estimation_total_size
= 42;
434 var upload_time_estimation_transfered_size
= 42;
435 var upload_time_estimation_transfered_date
= 42;
436 var upload_time_estimation_moving_average_speed
= 42;
438 function upload_time_estimation_init(total_size
)
440 upload_time_estimation_total_size
= total_size
;
441 upload_time_estimation_transfered_size
= 0;
442 upload_time_estimation_moving_average_speed
= 0;
444 upload_time_estimation_transfered_date
= d
.getTime();
447 function upload_time_estimation_add(total_transfered_size
)
449 // Let's compute the current speed
451 var speed
= upload_time_estimation_moving_average_speed
;
452 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
453 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
454 / (d
.getTime() - upload_time_estimation_transfered_date
);
455 // Let's compute moving average speed on 30 values
456 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
457 // Update global values
458 upload_time_estimation_transfered_size
= total_transfered_size
;
459 upload_time_estimation_transfered_date
= d
.getTime();
460 upload_time_estimation_moving_average_speed
= m
;
463 function upload_time_estimation_speed_string()
466 var s
= upload_time_estimation_moving_average_speed
* 1000;
474 else if (s
< 1000000)
476 res
= Math
.floor(s
/100) / 10;
481 res
= Math
.floor(s
/100000) / 10;
487 return res
.toString() + ' ' + scale
;
490 function milliseconds_to_time_string (milliseconds
)
492 function numberEnding (number
) {
493 return (number
> 1) ? 's' : '';
496 var temp
= Math
.floor(milliseconds
/ 1000);
497 var years
= Math
.floor(temp
/ 31536000);
499 return years
+ ' year' + numberEnding(years
);
501 var days
= Math
.floor((temp
%= 31536000) / 86400);
503 return days
+ ' day' + numberEnding(days
);
505 var hours
= Math
.floor((temp
%= 86400) / 3600);
507 return hours
+ ' hour' + numberEnding(hours
);
509 var minutes
= Math
.floor((temp
%= 3600) / 60);
511 return minutes
+ ' minute' + numberEnding(minutes
);
513 var seconds
= temp
% 60;
515 return seconds
+ ' second' + numberEnding(seconds
);
517 return 'less than a second';
520 function upload_time_estimation_time()
522 // Estimate remaining time
523 if (upload_time_estimation_moving_average_speed
== 0)
525 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
526 / upload_time_estimation_moving_average_speed
;
529 var chrono_last_update
= 0;
530 var chrono_time_ms
= 0;
531 var chrono_time_ms_last_update
= 0;
532 function chrono_update(time_ms
)
536 // Don't update too often
537 if (d
.getTime() - chrono_last_update
< 3000 &&
538 chrono_time_ms_last_update
> 0)
539 chrono
= chrono_time_ms
;
542 chrono_last_update
= d
.getTime();
543 chrono_time_ms
= time_ms
;
545 chrono_time_ms_last_update
= d
.getTime();
548 // Adjust chrono for smooth estimation
549 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
551 // Let's update chronometer
554 time_str
= milliseconds_to_time_string (chrono
);
558 var upload_speed_refresh_limiter_last_update
= 0;
559 var upload_speed_refresh_limiter_last_value
= '';
560 function upload_speed_refresh_limiter(speed_str
)
563 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
565 upload_speed_refresh_limiter_last_value
= speed_str
;
566 upload_speed_refresh_limiter_last_update
= d
.getTime();
568 return upload_speed_refresh_limiter_last_value
;