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
= encodeURIComponent("Download file \"" + filename
+ "\":") + "%0D";
44 b
+= encodeURIComponent(download_link_href
) + "%0D";
46 b
+= "%0D" + encodeURIComponent("This file will be available until " + date
) + "%0D";
47 document
.getElementById('upload_link_email').href
= "mailto:?body=" + b
+ "&subject=" + encodeURIComponent(filename
);
50 var delete_link
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
51 var delete_link_href
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
52 document
.getElementById('delete_link').innerHTML
= delete_link
;
53 document
.getElementById('delete_link').href
= delete_link_href
;
58 document
.getElementById('date').innerHTML
= date
;
59 document
.getElementById('validity').style
.display
= '';
62 document
.getElementById('validity').style
.display
= 'none';
64 // Preview link (if allowed)
65 if (!!document
.getElementById('preview_link'))
67 document
.getElementById('upload_finished_preview').style
.display
= 'none';
68 var preview_link
= url
+ 'f.php?h=' + reference
+ '&p=1';
69 var preview_link_href
= url
+ 'f.php?h=' + reference
+ '&p=1';
70 if (crypt_key
.length
> 0)
72 preview_link
+= '&k=' + crypt_key
;
73 preview_link_href
+= '&k=' + crypt_key
;
76 // Test if content can be previewed
77 type
= document
.getElementById('file_select').files
[0].type
;
78 if (type
.indexOf("image") > -1 ||
79 type
.indexOf("audio") > -1 ||
80 type
.indexOf("text") > -1 ||
81 type
.indexOf("video") > -1)
83 document
.getElementById('preview_link').innerHTML
= preview_link
;
84 document
.getElementById('preview_link').href
= preview_link_href
;
85 document
.getElementById('upload_finished_preview').style
.display
= '';
89 // Direct download link
90 var direct_download_link
= url
+ 'f.php?h=' + reference
+ '&d=1';
91 var direct_download_link_href
= url
+ 'f.php?h=' + reference
+ '&d=1';
92 if (crypt_key
.length
> 0)
94 direct_download_link
+= '&k=' + crypt_key
;
95 direct_download_link_href
+= '&k=' + crypt_key
;
97 document
.getElementById('direct_link').innerHTML
= direct_download_link
;
98 document
.getElementById('direct_link').href
= direct_download_link_href
;
101 // Hide preview and direct download link if password is set
102 if (document
.getElementById('input_key').value
.length
> 0)
104 if (!!document
.getElementById('preview_link'))
105 document
.getElementById('upload_finished_preview').style
.display
= 'none';
106 document
.getElementById('upload_direct_download').style
.display
= 'none';
110 function show_upload_progression (percentage
, speed
, time_left
)
112 document
.getElementById('uploaded_percentage').innerHTML
= percentage
;
113 document
.getElementById('uploaded_speed').innerHTML
= speed
;
114 document
.getElementById('uploaded_time').innerHTML
= time_left
;
115 document
.title
= 'Jirafeau - ' + percentage
;
118 function hide_upload_progression ()
120 document
.getElementById('uploaded_percentage').style
.display
= 'none';
121 document
.getElementById('uploaded_speed').style
.display
= 'none';
122 document
.getElementById('uploaded_time').style
.display
= 'none';
123 document
.title
= 'Jirafeau';
126 function upload_progress (e
)
128 if (!e
.lengthComputable
)
131 // Init time estimation if needed
132 if (upload_time_estimation_total_size
== 0)
133 upload_time_estimation_total_size
= e
.total
;
135 // Compute percentage
136 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
139 p_str
= p
.toString() + '%';
140 // Update estimation speed
141 upload_time_estimation_add(e
.loaded
);
143 var speed_str
= upload_time_estimation_speed_string();
144 speed_str
= upload_speed_refresh_limiter(speed_str
);
146 var time_str
= chrono_update(upload_time_estimation_time());
148 show_upload_progression (p_str
, speed_str
, time_str
);
151 function control_selected_file_size(max_size
, error_str
)
153 f_size
= document
.getElementById('file_select').files
[0].size
;
154 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
156 pop_failure(error_str
);
157 document
.getElementById('send').style
.display
= 'none';
161 document
.getElementById('options').style
.display
= '';
162 document
.getElementById('send').style
.display
= '';
163 document
.getElementById('error_pop').style
.display
= 'none';
164 document
.getElementById('file_select').style
.left
= 'inherit';
165 document
.getElementById('file_select').style
.height
= 'inherit';
166 document
.getElementById('file_select').style
.opacity
= '1';
170 function pop_failure (e
)
172 var text
= "An error occured";
173 if (typeof e
!== 'undefined')
175 text
= "<p>" + text
+ "</p>";
176 document
.getElementById('error_pop').innerHTML
= e
;
178 document
.getElementById('uploading').style
.display
= 'none';
179 document
.getElementById('error_pop').style
.display
= '';
180 document
.getElementById('upload').style
.display
= '';
181 document
.getElementById('send').style
.display
= '';
184 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
186 // Delay time estimation init as we can't have file size
187 upload_time_estimation_init(0);
189 var req
= new XMLHttpRequest ();
190 req
.upload
.addEventListener ("progress", upload_progress
, false);
191 req
.addEventListener ("error", pop_failure
, false);
192 req
.addEventListener ("abort", pop_failure
, false);
193 req
.onreadystatechange = function ()
195 if (req
.readyState
== 4 && req
.status
== 200)
197 var res
= req
.responseText
;
203 res
= res
.split ("\n");
207 if (time
== 'minute')
208 d
.setSeconds (d
.getSeconds() + 60);
209 else if (time
== 'hour')
210 d
.setSeconds (d
.getSeconds() + 3600);
211 else if (time
== 'day')
212 d
.setSeconds (d
.getSeconds() + 86400);
213 else if (time
== 'week')
214 d
.setSeconds (d
.getSeconds() + 604800);
215 else if (time
== 'month')
216 d
.setSeconds (d
.getSeconds() + 2419200);
217 else if (time
== 'year')
218 d
.setSeconds (d
.getSeconds() + 29030400);
221 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
224 show_link (url
, res
[0], res
[1], res
[2]);
227 req
.open ("POST", url
+ 'script.php' , true);
229 var form
= new FormData();
230 form
.append ("file", file
);
232 form
.append ("time", time
);
234 form
.append ("key", password
);
236 form
.append ("one_time_download", '1');
237 if (upload_password
.length
> 0)
238 form
.append ("upload_password", upload_password
);
243 function check_html5_file_api ()
245 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
250 var async_global_transfered
= 0;
251 var async_global_url
= '';
252 var async_global_file
;
253 var async_global_ref
= '';
254 var async_global_max_size
= 0;
255 var async_global_time
;
256 var async_global_transfering
= 0;
258 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
260 async_global_transfered
= 0;
261 async_global_url
= url
;
262 async_global_file
= file
;
263 async_global_max_size
= max_size
;
264 async_global_time
= time
;
266 var req
= new XMLHttpRequest ();
267 req
.addEventListener ("error", pop_failure
, false);
268 req
.addEventListener ("abort", pop_failure
, false);
269 req
.onreadystatechange = function ()
271 if (req
.readyState
== 4 && req
.status
== 200)
273 var res
= req
.responseText
;
279 res
= res
.split ("\n");
280 async_global_ref
= res
[0];
282 async_upload_push (code
);
285 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
287 var form
= new FormData();
288 form
.append ("filename", async_global_file
.name
);
289 form
.append ("type", async_global_file
.type
);
291 form
.append ("time", time
);
293 form
.append ("key", password
);
295 form
.append ("one_time_download", '1');
296 if (upload_password
.length
> 0)
297 form
.append ("upload_password", upload_password
);
299 // Start time estimation
300 upload_time_estimation_init(async_global_file
.size
);
305 function async_upload_progress (e
)
307 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
310 // Compute percentage
311 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
314 p_str
= p
.toString() + '%';
315 // Update estimation speed
316 upload_time_estimation_add(e
.loaded
+ async_global_transfered
);
318 var speed_str
= upload_time_estimation_speed_string();
319 speed_str
= upload_speed_refresh_limiter(speed_str
);
321 var time_str
= chrono_update(upload_time_estimation_time());
323 show_upload_progression (p_str
, speed_str
, time_str
);
326 function async_upload_push (code
)
328 if (async_global_transfered
== async_global_file
.size
)
330 hide_upload_progression ();
331 async_upload_end (code
);
334 var req
= new XMLHttpRequest ();
335 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
336 req
.addEventListener ("error", pop_failure
, false);
337 req
.addEventListener ("abort", pop_failure
, false);
338 req
.onreadystatechange = function ()
340 if (req
.readyState
== 4 && req
.status
== 200)
342 var res
= req
.responseText
;
348 res
= res
.split ("\n");
350 async_global_transfered
= async_global_transfering
;
351 async_upload_push (code
);
354 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
356 var chunk_size
= parseInt (async_global_max_size
* 0.50);
357 var start
= async_global_transfered
;
358 var end
= start
+ chunk_size
;
359 if (end
>= async_global_file
.size
)
360 end
= async_global_file
.size
;
361 var blob
= async_global_file
.slice (start
, end
);
362 async_global_transfering
= end
;
364 var form
= new FormData();
365 form
.append ("ref", async_global_ref
);
366 form
.append ("data", blob
);
367 form
.append ("code", code
);
371 function async_upload_end (code
)
373 var req
= new XMLHttpRequest ();
374 req
.addEventListener ("error", pop_failure
, false);
375 req
.addEventListener ("abort", pop_failure
, false);
376 req
.onreadystatechange = function ()
378 if (req
.readyState
== 4 && req
.status
== 200)
380 var res
= req
.responseText
;
386 res
= res
.split ("\n");
387 if (async_global_time
!= 'none')
390 if (async_global_time
== 'minute')
391 d
.setSeconds (d
.getSeconds() + 60);
392 else if (async_global_time
== 'hour')
393 d
.setSeconds (d
.getSeconds() + 3600);
394 else if (async_global_time
== 'day')
395 d
.setSeconds (d
.getSeconds() + 86400);
396 else if (async_global_time
== 'week')
397 d
.setSeconds (d
.getSeconds() + 604800);
398 else if (async_global_time
== 'month')
399 d
.setSeconds (d
.getSeconds() + 2419200);
400 else if (async_global_time
== 'year')
401 d
.setSeconds (d
.getSeconds() + 29030400);
404 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
407 show_link (async_global_url
, res
[0], res
[1], res
[2]);
410 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
412 var form
= new FormData();
413 form
.append ("ref", async_global_ref
);
414 form
.append ("code", code
);
418 function upload (url
, max_size
)
420 if (check_html5_file_api ()
421 && document
.getElementById('file_select').files
[0].size
>= max_size
)
423 async_upload_start (url
,
425 document
.getElementById('file_select').files
[0],
426 document
.getElementById('select_time').value
,
427 document
.getElementById('input_key').value
,
428 document
.getElementById('one_time_download').checked
,
429 document
.getElementById('upload_password').value
435 document
.getElementById('file_select').files
[0],
436 document
.getElementById('select_time').value
,
437 document
.getElementById('input_key').value
,
438 document
.getElementById('one_time_download').checked
,
439 document
.getElementById('upload_password').value
444 var upload_time_estimation_total_size
= 42;
445 var upload_time_estimation_transfered_size
= 42;
446 var upload_time_estimation_transfered_date
= 42;
447 var upload_time_estimation_moving_average_speed
= 42;
449 function upload_time_estimation_init(total_size
)
451 upload_time_estimation_total_size
= total_size
;
452 upload_time_estimation_transfered_size
= 0;
453 upload_time_estimation_moving_average_speed
= 0;
455 upload_time_estimation_transfered_date
= d
.getTime();
458 function upload_time_estimation_add(total_transfered_size
)
460 // Let's compute the current speed
462 var speed
= upload_time_estimation_moving_average_speed
;
463 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
464 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
465 / (d
.getTime() - upload_time_estimation_transfered_date
);
466 // Let's compute moving average speed on 30 values
467 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
468 // Update global values
469 upload_time_estimation_transfered_size
= total_transfered_size
;
470 upload_time_estimation_transfered_date
= d
.getTime();
471 upload_time_estimation_moving_average_speed
= m
;
474 function upload_time_estimation_speed_string()
477 var s
= upload_time_estimation_moving_average_speed
* 1000;
485 else if (s
< 1000000)
487 res
= Math
.floor(s
/100) / 10;
492 res
= Math
.floor(s
/100000) / 10;
498 return res
.toString() + ' ' + scale
;
501 function milliseconds_to_time_string (milliseconds
)
503 function numberEnding (number
) {
504 return (number
> 1) ? 's' : '';
507 var temp
= Math
.floor(milliseconds
/ 1000);
508 var years
= Math
.floor(temp
/ 31536000);
510 return years
+ ' year' + numberEnding(years
);
512 var days
= Math
.floor((temp
%= 31536000) / 86400);
514 return days
+ ' day' + numberEnding(days
);
516 var hours
= Math
.floor((temp
%= 86400) / 3600);
518 return hours
+ ' hour' + numberEnding(hours
);
520 var minutes
= Math
.floor((temp
%= 3600) / 60);
522 return minutes
+ ' minute' + numberEnding(minutes
);
524 var seconds
= temp
% 60;
526 return seconds
+ ' second' + numberEnding(seconds
);
528 return 'less than a second';
531 function upload_time_estimation_time()
533 // Estimate remaining time
534 if (upload_time_estimation_moving_average_speed
== 0)
536 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
537 / upload_time_estimation_moving_average_speed
;
540 var chrono_last_update
= 0;
541 var chrono_time_ms
= 0;
542 var chrono_time_ms_last_update
= 0;
543 function chrono_update(time_ms
)
547 // Don't update too often
548 if (d
.getTime() - chrono_last_update
< 3000 &&
549 chrono_time_ms_last_update
> 0)
550 chrono
= chrono_time_ms
;
553 chrono_last_update
= d
.getTime();
554 chrono_time_ms
= time_ms
;
556 chrono_time_ms_last_update
= d
.getTime();
559 // Adjust chrono for smooth estimation
560 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
562 // Let's update chronometer
565 time_str
= milliseconds_to_time_string (chrono
);
569 var upload_speed_refresh_limiter_last_update
= 0;
570 var upload_speed_refresh_limiter_last_value
= '';
571 function upload_speed_refresh_limiter(speed_str
)
574 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
576 upload_speed_refresh_limiter_last_value
= speed_str
;
577 upload_speed_refresh_limiter_last_update
= d
.getTime();
579 return upload_speed_refresh_limiter_last_value
;