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();
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();
314 var time_str
= chrono_update(upload_time_estimation_time());
316 show_upload_progression (p_str
, speed_str
, time_str
);
319 function async_upload_push (code
)
321 if (async_global_transfered
== async_global_file
.size
)
323 hide_upload_progression ();
324 async_upload_end (code
);
327 var req
= new XMLHttpRequest ();
328 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
329 req
.addEventListener ("error", pop_failure
, false);
330 req
.addEventListener ("abort", pop_failure
, false);
331 req
.onreadystatechange = function ()
333 if (req
.readyState
== 4 && req
.status
== 200)
335 var res
= req
.responseText
;
341 res
= res
.split ("\n");
343 async_global_transfered
= async_global_transfering
;
344 async_upload_push (code
);
347 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
349 var chunk_size
= parseInt (async_global_max_size
* 0.50);
350 var start
= async_global_transfered
;
351 var end
= start
+ chunk_size
;
352 if (end
>= async_global_file
.size
)
353 end
= async_global_file
.size
;
354 var blob
= async_global_file
.slice (start
, end
);
355 async_global_transfering
= end
;
357 var form
= new FormData();
358 form
.append ("ref", async_global_ref
);
359 form
.append ("data", blob
);
360 form
.append ("code", code
);
364 function async_upload_end (code
)
366 var req
= new XMLHttpRequest ();
367 req
.addEventListener ("error", pop_failure
, false);
368 req
.addEventListener ("abort", pop_failure
, false);
369 req
.onreadystatechange = function ()
371 if (req
.readyState
== 4 && req
.status
== 200)
373 var res
= req
.responseText
;
379 res
= res
.split ("\n");
380 if (async_global_time
!= 'none')
383 if (async_global_time
== 'minute')
384 d
.setSeconds (d
.getSeconds() + 60);
385 else if (async_global_time
== 'hour')
386 d
.setSeconds (d
.getSeconds() + 3600);
387 else if (async_global_time
== 'day')
388 d
.setSeconds (d
.getSeconds() + 86400);
389 else if (async_global_time
== 'week')
390 d
.setSeconds (d
.getSeconds() + 604800);
391 else if (async_global_time
== 'month')
392 d
.setSeconds (d
.getSeconds() + 2419200);
393 else if (async_global_time
== 'year')
394 d
.setSeconds (d
.getSeconds() + 29030400);
397 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
400 show_link (async_global_url
, res
[0], res
[1], res
[2]);
403 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
405 var form
= new FormData();
406 form
.append ("ref", async_global_ref
);
407 form
.append ("code", code
);
411 function upload (url
, max_size
)
413 if (check_html5_file_api ()
414 && document
.getElementById('file_select').files
[0].size
>= max_size
)
416 async_upload_start (url
,
418 document
.getElementById('file_select').files
[0],
419 document
.getElementById('select_time').value
,
420 document
.getElementById('input_key').value
,
421 document
.getElementById('one_time_download').checked
,
422 document
.getElementById('upload_password').value
428 document
.getElementById('file_select').files
[0],
429 document
.getElementById('select_time').value
,
430 document
.getElementById('input_key').value
,
431 document
.getElementById('one_time_download').checked
,
432 document
.getElementById('upload_password').value
437 var upload_time_estimation_total_size
= 42;
438 var upload_time_estimation_transfered_size
= 42;
439 var upload_time_estimation_transfered_date
= 42;
440 var upload_time_estimation_moving_average_speed
= 42;
442 function upload_time_estimation_init(total_size
)
444 upload_time_estimation_total_size
= total_size
;
445 upload_time_estimation_transfered_size
= 0;
446 upload_time_estimation_moving_average_speed
= 0;
448 upload_time_estimation_transfered_date
= d
.getTime();
451 function upload_time_estimation_add(total_transfered_size
)
453 // Let's compute the current speed
455 var speed
= upload_time_estimation_moving_average_speed
;
456 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
457 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
458 / (d
.getTime() - upload_time_estimation_transfered_date
);
459 // Let's compute moving average speed on 30 values
460 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
461 // Update global values
462 upload_time_estimation_transfered_size
= total_transfered_size
;
463 upload_time_estimation_transfered_date
= d
.getTime();
464 upload_time_estimation_moving_average_speed
= m
;
467 function upload_time_estimation_speed_string()
470 var s
= upload_time_estimation_moving_average_speed
* 1000;
478 else if (s
< 1000000)
480 res
= Math
.floor(s
/100) / 10;
485 res
= Math
.floor(s
/100000) / 10;
491 return res
.toString() + ' ' + scale
;
494 function milliseconds_to_time_string (milliseconds
)
496 function numberEnding (number
) {
497 return (number
> 1) ? 's' : '';
500 var temp
= Math
.floor(milliseconds
/ 1000);
501 var years
= Math
.floor(temp
/ 31536000);
503 return years
+ ' year' + numberEnding(years
);
505 var days
= Math
.floor((temp
%= 31536000) / 86400);
507 return days
+ ' day' + numberEnding(days
);
509 var hours
= Math
.floor((temp
%= 86400) / 3600);
511 return hours
+ ' hour' + numberEnding(hours
);
513 var minutes
= Math
.floor((temp
%= 3600) / 60);
515 return minutes
+ ' minute' + numberEnding(minutes
);
517 var seconds
= temp
% 60;
519 return seconds
+ ' second' + numberEnding(seconds
);
521 return 'less than a second';
524 function upload_time_estimation_time()
526 // Estimate remaining time
527 if (upload_time_estimation_moving_average_speed
== 0)
529 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
530 / upload_time_estimation_moving_average_speed
;
533 var chrono_last_update
= 0;
534 var chrono_time_ms
= 0;
535 var chrono_time_ms_last_update
= 0;
536 function chrono_update(time_ms
)
540 // Don't update too often
541 if (d
.getTime() - chrono_last_update
< 3000 &&
542 chrono_time_ms_last_update
> 0)
543 chrono
= chrono_time_ms
;
546 chrono_last_update
= d
.getTime();
547 chrono_time_ms
= time_ms
;
549 chrono_time_ms_last_update
= d
.getTime();
552 // Adjust chrono for smooth estimation
553 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
555 // Let's update chronometer
558 time_str
= milliseconds_to_time_string (chrono
);