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
)
21 // Download page if element exists
22 var download_link
= url
+ 'f.php?h=' + reference
;
23 var download_link_href
= url
+ 'f.php?h=' + reference
;
24 if (crypt_key
.length
> 0)
26 download_link
+= '&k=' + crypt_key
;
27 download_link_href
+= '&k=' + crypt_key
;
29 if (!!document
.getElementById('upload_finished_download_page'))
31 document
.getElementById('upload_link').innerHTML
= download_link
;
32 document
.getElementById('upload_link').href
= download_link_href
;
35 // Is the preview allowed ?
36 if (!!document
.getElementById('preview_link'))
38 document
.getElementById('upload_finished_preview').style
.display
= 'none';
39 var preview_link
= url
+ 'f.php?h=' + reference
+ '&p=1';
40 var preview_link_href
= url
+ 'f.php?h=' + reference
+ '&p=1';
41 if (crypt_key
.length
> 0)
43 preview_link
+= '&k=' + crypt_key
;
44 preview_link_href
+= '&k=' + crypt_key
;
47 // Test if content can be previewed
48 type
= document
.getElementById('file_select').files
[0].type
;
49 if (type
.indexOf("image") > -1 ||
50 type
.indexOf("audio") > -1 ||
51 type
.indexOf("text") > -1 ||
52 type
.indexOf("video") > -1)
54 document
.getElementById('preview_link').innerHTML
= preview_link
;
55 document
.getElementById('preview_link').href
= preview_link_href
;
56 document
.getElementById('upload_finished_preview').style
.display
= '';
60 // Only show link to password page if password is set
61 document
.getElementById('upload_password_page').style
.display
= 'none';
62 if (document
.getElementById('input_key').value
.length
> 0)
64 if (!!document
.getElementById('upload_finished_download_page'))
65 document
.getElementById('upload_finished_download_page').style
.display
= 'none';
66 document
.getElementById('upload_password_page').style
.display
= '';
67 if (!!document
.getElementById('upload_finished_preview'))
68 document
.getElementById('upload_finished_preview').style
.display
= 'none';
69 document
.getElementById('upload_direct_download').style
.display
= 'none';
71 document
.getElementById('password_link').innerHTML
= download_link
;
72 document
.getElementById('password_link').href
= download_link_href
;
74 // Direct download link
77 var direct_download_link
= url
+ 'f.php?h=' + reference
+ '&d=1';
78 var direct_download_link_href
= url
+ 'f.php?h=' + reference
+ '&d=1';
79 if (crypt_key
.length
> 0)
81 direct_download_link
+= '&k=' + crypt_key
;
82 direct_download_link_href
+= '&k=' + crypt_key
;
84 document
.getElementById('direct_link').innerHTML
= direct_download_link
;
85 document
.getElementById('direct_link').href
= direct_download_link_href
;
89 var delete_link
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
90 var delete_link_href
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
91 document
.getElementById('delete_link').innerHTML
= delete_link
;
92 document
.getElementById('delete_link').href
= delete_link_href
;
96 document
.getElementById('date').innerHTML
= date
;
97 document
.getElementById('validity').style
.display
= '';
100 document
.getElementById('validity').style
.display
= 'none';
102 document
.getElementById('uploading').style
.display
= 'none';
103 document
.getElementById('upload').style
.display
= 'none';
104 document
.getElementById('upload_finished').style
.display
= '';
105 document
.title
= 'Jirafeau - 100%';
108 function show_upload_progression (percentage
, speed
, time_left
)
110 document
.getElementById('uploaded_percentage').innerHTML
= percentage
;
111 document
.getElementById('uploaded_speed').innerHTML
= speed
;
112 document
.getElementById('uploaded_time').innerHTML
= time_left
;
113 document
.title
= 'Jirafeau - ' + percentage
;
116 function hide_upload_progression ()
118 document
.getElementById('uploaded_percentage').style
.display
= 'none';
119 document
.getElementById('uploaded_speed').style
.display
= 'none';
120 document
.getElementById('uploaded_time').style
.display
= 'none';
121 document
.title
= 'Jirafeau';
124 function upload_progress (e
)
126 if (!e
.lengthComputable
)
129 // Init time estimation if needed
130 if (upload_time_estimation_total_size
== 0)
131 upload_time_estimation_total_size
= e
.total
;
133 // Compute percentage
134 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
137 p_str
= p
.toString() + '%';
138 // Update estimation speed
139 upload_time_estimation_add(e
.loaded
);
141 var speed_str
= upload_time_estimation_speed_string();
142 speed_str
= upload_speed_refresh_limiter(speed_str
);
144 var time_str
= chrono_update(upload_time_estimation_time());
146 show_upload_progression (p_str
, speed_str
, time_str
);
149 function control_selected_file_size(max_size
, error_str
)
151 f_size
= document
.getElementById('file_select').files
[0].size
;
152 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
154 pop_failure(error_str
);
155 document
.getElementById('send').style
.display
= 'none';
159 document
.getElementById('options').style
.display
= '';
160 document
.getElementById('send').style
.display
= '';
161 document
.getElementById('error_pop').style
.display
= 'none';
165 function pop_failure (e
)
167 var text
= "An error occured";
168 if (typeof e
!== 'undefined')
170 text
= "<p>" + text
+ "</p>";
171 document
.getElementById('error_pop').innerHTML
= e
;
173 document
.getElementById('uploading').style
.display
= 'none';
174 document
.getElementById('error_pop').style
.display
= '';
175 document
.getElementById('upload').style
.display
= '';
176 document
.getElementById('send').style
.display
= '';
179 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
181 // Delay time estimation init as we can't have file size
182 upload_time_estimation_init(0);
184 var req
= new XMLHttpRequest ();
185 req
.upload
.addEventListener ("progress", upload_progress
, false);
186 req
.addEventListener ("error", pop_failure
, false);
187 req
.addEventListener ("abort", pop_failure
, false);
188 req
.onreadystatechange = function ()
190 if (req
.readyState
== 4 && req
.status
== 200)
192 var res
= req
.responseText
;
198 res
= res
.split ("\n");
202 if (time
== 'minute')
203 d
.setSeconds (d
.getSeconds() + 60);
204 else if (time
== 'hour')
205 d
.setSeconds (d
.getSeconds() + 3600);
206 else if (time
== 'day')
207 d
.setSeconds (d
.getSeconds() + 86400);
208 else if (time
== 'week')
209 d
.setSeconds (d
.getSeconds() + 604800);
210 else if (time
== 'month')
211 d
.setSeconds (d
.getSeconds() + 2419200);
212 else if (time
== 'year')
213 d
.setSeconds (d
.getSeconds() + 29030400);
216 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
219 show_link (url
, res
[0], res
[1], res
[2]);
222 req
.open ("POST", url
+ 'script.php' , true);
224 var form
= new FormData();
225 form
.append ("file", file
);
227 form
.append ("time", time
);
229 form
.append ("key", password
);
231 form
.append ("one_time_download", '1');
232 if (upload_password
.length
> 0)
233 form
.append ("upload_password", upload_password
);
238 function check_html5_file_api ()
240 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
245 var async_global_transfered
= 0;
246 var async_global_url
= '';
247 var async_global_file
;
248 var async_global_ref
= '';
249 var async_global_max_size
= 0;
250 var async_global_time
;
251 var async_global_transfering
= 0;
253 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
255 async_global_transfered
= 0;
256 async_global_url
= url
;
257 async_global_file
= file
;
258 async_global_max_size
= max_size
;
259 async_global_time
= time
;
261 var req
= new XMLHttpRequest ();
262 req
.addEventListener ("error", pop_failure
, false);
263 req
.addEventListener ("abort", pop_failure
, false);
264 req
.onreadystatechange = function ()
266 if (req
.readyState
== 4 && req
.status
== 200)
268 var res
= req
.responseText
;
274 res
= res
.split ("\n");
275 async_global_ref
= res
[0];
277 async_upload_push (code
);
280 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
282 var form
= new FormData();
283 form
.append ("filename", async_global_file
.name
);
284 form
.append ("type", async_global_file
.type
);
286 form
.append ("time", time
);
288 form
.append ("key", password
);
290 form
.append ("one_time_download", '1');
291 if (upload_password
.length
> 0)
292 form
.append ("upload_password", upload_password
);
294 // Start time estimation
295 upload_time_estimation_init(async_global_file
.size
);
300 function async_upload_progress (e
)
302 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
305 // Compute percentage
306 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
309 p_str
= p
.toString() + '%';
310 // Update estimation speed
311 upload_time_estimation_add(e
.loaded
+ async_global_transfered
);
313 var speed_str
= upload_time_estimation_speed_string();
314 speed_str
= upload_speed_refresh_limiter(speed_str
);
316 var time_str
= chrono_update(upload_time_estimation_time());
318 show_upload_progression (p_str
, speed_str
, time_str
);
321 function async_upload_push (code
)
323 if (async_global_transfered
== async_global_file
.size
)
325 hide_upload_progression ();
326 async_upload_end (code
);
329 var req
= new XMLHttpRequest ();
330 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
331 req
.addEventListener ("error", pop_failure
, false);
332 req
.addEventListener ("abort", pop_failure
, false);
333 req
.onreadystatechange = function ()
335 if (req
.readyState
== 4 && req
.status
== 200)
337 var res
= req
.responseText
;
343 res
= res
.split ("\n");
345 async_global_transfered
= async_global_transfering
;
346 async_upload_push (code
);
349 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
351 var chunk_size
= parseInt (async_global_max_size
* 0.50);
352 var start
= async_global_transfered
;
353 var end
= start
+ chunk_size
;
354 if (end
>= async_global_file
.size
)
355 end
= async_global_file
.size
;
356 var blob
= async_global_file
.slice (start
, end
);
357 async_global_transfering
= end
;
359 var form
= new FormData();
360 form
.append ("ref", async_global_ref
);
361 form
.append ("data", blob
);
362 form
.append ("code", code
);
366 function async_upload_end (code
)
368 var req
= new XMLHttpRequest ();
369 req
.addEventListener ("error", pop_failure
, false);
370 req
.addEventListener ("abort", pop_failure
, false);
371 req
.onreadystatechange = function ()
373 if (req
.readyState
== 4 && req
.status
== 200)
375 var res
= req
.responseText
;
381 res
= res
.split ("\n");
382 if (async_global_time
!= 'none')
385 if (async_global_time
== 'minute')
386 d
.setSeconds (d
.getSeconds() + 60);
387 else if (async_global_time
== 'hour')
388 d
.setSeconds (d
.getSeconds() + 3600);
389 else if (async_global_time
== 'day')
390 d
.setSeconds (d
.getSeconds() + 86400);
391 else if (async_global_time
== 'week')
392 d
.setSeconds (d
.getSeconds() + 604800);
393 else if (async_global_time
== 'month')
394 d
.setSeconds (d
.getSeconds() + 2419200);
395 else if (async_global_time
== 'year')
396 d
.setSeconds (d
.getSeconds() + 29030400);
399 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
402 show_link (async_global_url
, res
[0], res
[1], res
[2]);
405 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
407 var form
= new FormData();
408 form
.append ("ref", async_global_ref
);
409 form
.append ("code", code
);
413 function upload (url
, max_size
)
415 if (check_html5_file_api ()
416 && document
.getElementById('file_select').files
[0].size
>= max_size
)
418 async_upload_start (url
,
420 document
.getElementById('file_select').files
[0],
421 document
.getElementById('select_time').value
,
422 document
.getElementById('input_key').value
,
423 document
.getElementById('one_time_download').checked
,
424 document
.getElementById('upload_password').value
430 document
.getElementById('file_select').files
[0],
431 document
.getElementById('select_time').value
,
432 document
.getElementById('input_key').value
,
433 document
.getElementById('one_time_download').checked
,
434 document
.getElementById('upload_password').value
439 var upload_time_estimation_total_size
= 42;
440 var upload_time_estimation_transfered_size
= 42;
441 var upload_time_estimation_transfered_date
= 42;
442 var upload_time_estimation_moving_average_speed
= 42;
444 function upload_time_estimation_init(total_size
)
446 upload_time_estimation_total_size
= total_size
;
447 upload_time_estimation_transfered_size
= 0;
448 upload_time_estimation_moving_average_speed
= 0;
450 upload_time_estimation_transfered_date
= d
.getTime();
453 function upload_time_estimation_add(total_transfered_size
)
455 // Let's compute the current speed
457 var speed
= upload_time_estimation_moving_average_speed
;
458 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
459 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
460 / (d
.getTime() - upload_time_estimation_transfered_date
);
461 // Let's compute moving average speed on 30 values
462 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
463 // Update global values
464 upload_time_estimation_transfered_size
= total_transfered_size
;
465 upload_time_estimation_transfered_date
= d
.getTime();
466 upload_time_estimation_moving_average_speed
= m
;
469 function upload_time_estimation_speed_string()
472 var s
= upload_time_estimation_moving_average_speed
* 1000;
480 else if (s
< 1000000)
482 res
= Math
.floor(s
/100) / 10;
487 res
= Math
.floor(s
/100000) / 10;
493 return res
.toString() + ' ' + scale
;
496 function milliseconds_to_time_string (milliseconds
)
498 function numberEnding (number
) {
499 return (number
> 1) ? 's' : '';
502 var temp
= Math
.floor(milliseconds
/ 1000);
503 var years
= Math
.floor(temp
/ 31536000);
505 return years
+ ' year' + numberEnding(years
);
507 var days
= Math
.floor((temp
%= 31536000) / 86400);
509 return days
+ ' day' + numberEnding(days
);
511 var hours
= Math
.floor((temp
%= 86400) / 3600);
513 return hours
+ ' hour' + numberEnding(hours
);
515 var minutes
= Math
.floor((temp
%= 3600) / 60);
517 return minutes
+ ' minute' + numberEnding(minutes
);
519 var seconds
= temp
% 60;
521 return seconds
+ ' second' + numberEnding(seconds
);
523 return 'less than a second';
526 function upload_time_estimation_time()
528 // Estimate remaining time
529 if (upload_time_estimation_moving_average_speed
== 0)
531 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
532 / upload_time_estimation_moving_average_speed
;
535 var chrono_last_update
= 0;
536 var chrono_time_ms
= 0;
537 var chrono_time_ms_last_update
= 0;
538 function chrono_update(time_ms
)
542 // Don't update too often
543 if (d
.getTime() - chrono_last_update
< 3000 &&
544 chrono_time_ms_last_update
> 0)
545 chrono
= chrono_time_ms
;
548 chrono_last_update
= d
.getTime();
549 chrono_time_ms
= time_ms
;
551 chrono_time_ms_last_update
= d
.getTime();
554 // Adjust chrono for smooth estimation
555 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
557 // Let's update chronometer
560 time_str
= milliseconds_to_time_string (chrono
);
564 var upload_speed_refresh_limiter_last_update
= 0;
565 var upload_speed_refresh_limiter_last_value
= '';
566 function upload_speed_refresh_limiter(speed_str
)
569 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
571 upload_speed_refresh_limiter_last_value
= speed_str
;
572 upload_speed_refresh_limiter_last_update
= d
.getTime();
574 return upload_speed_refresh_limiter_last_value
;