3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
5 * Copyright (C) 2015 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 header('Content-Type: text/javascript');
22 define('JIRAFEAU_ROOT', dirname(__FILE__
) . '/../');
24 require(JIRAFEAU_ROOT
. 'lib/settings.php');
25 require(JIRAFEAU_ROOT
. 'lib/functions.php');
26 require(JIRAFEAU_ROOT
. 'lib/lang.php');
28 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
29 var web_root
= "<?php echo $cfg['web_root']; ?>";
31 var lang_array
= <?php
echo json_lang_generator(null); ?
>;
32 var lang_array_fallback
= <?php
echo json_lang_generator("en"); ?
>;
34 function translate (expr
) {
35 if (lang_array
.hasOwnProperty(expr
)) {
36 var e
= lang_array
[expr
];
40 if (lang_array_fallback
.hasOwnProperty(expr
)) {
41 var e
= lang_array_fallback
[expr
];
45 return "FIXME: " + expr
;
48 function isEmpty(str
) {
49 return (!str ||
0 === str
.length
);
52 // Extend date object with format method
53 Date
.prototype
.format
= function(format
) {
54 format
= format ||
'YYYY-MM-DD hh:mm';
56 var zeropad
= function(number
, length
) {
57 number
= number
.toString();
59 while(number
.length
< length
)
60 number
= '0' + number
;
64 YYYY
: this
.getFullYear(),
65 MM
: zeropad(this
.getMonth() +
1),
66 DD
: zeropad(this
.getDate()),
67 hh
: zeropad(this
.getHours()),
68 mm
: zeropad(this
.getMinutes()),
71 sign
= (localDate
.getTimezoneOffset() > 0) ?
'-' : '+';
72 offset
= Math
.abs(localDate
.getTimezoneOffset());
73 hours
= zeropad(Math
.floor(offset
/ 60));
74 minutes
= zeropad(offset %
60);
75 return sign + hours +
":" + minutes
;
78 pattern
= '(' +
Object.keys(formats
).join(')|(') +
')';
80 return format
.replace(new RegExp(pattern
, 'g'), function(match
) {
81 return formats
[match
];
85 function dateFromUtcString(datestring
) {
86 // matches »YYYY-MM-DD hh:mm«
87 var m
= datestring
.match(/(\d+
)-(\d+
)-(\d+
)\s+
(\d+
):(\d+
)/);
88 return new Date(Date
.UTC(+m
[1], +m
[2] - 1, +m
[3], +m
[4], +m
[5], 0));
91 function dateFromUtcTimestamp(datetimestamp
) {
92 return new Date(parseInt(datetimestamp
) * 1000)
95 function dateToUtcString(datelocal
) {
97 datelocal
.getUTCFullYear(),
98 datelocal
.getUTCMonth(),
99 datelocal
.getUTCDate(),
100 datelocal
.getUTCHours(),
101 datelocal
.getUTCMinutes(),
102 datelocal
.getUTCSeconds()
106 function dateToUtcTimestamp(datelocal
) {
108 datelocal
.getUTCFullYear(),
109 datelocal
.getUTCMonth(),
110 datelocal
.getUTCDate(),
111 datelocal
.getUTCHours(),
112 datelocal
.getUTCMinutes(),
113 datelocal
.getUTCSeconds()
117 function convertAllDatetimeFields() {
118 datefields
= document
.getElementsByClassName('datetime')
119 for(var i
=0; i
<datefields
.length
; i++
) {
120 dateUTC
= datefields
[i
].getAttribute('data-datetime');
121 datefields
[i
].setAttribute('title', dateUTC +
' (GMT)');
122 datefields
[i
].innerHTML
= dateFromUtcString(dateUTC
).format('YYYY-MM-DD hh:mm (GMT O)');
126 function show_link (reference
, delete_code
, crypt_key
, date
)
129 document
.getElementById('uploading').style
.display
= 'none';
130 document
.getElementById('upload').style
.display
= 'none';
131 document
.getElementById('upload_finished').style
.display
= '';
132 document
.title
= "100% - <?php echo empty($cfg['title']) ? 'Jirafeau' : $cfg['title']; ?>";
135 var download_link_href
= 'f.php?h=' + reference
;
136 if (crypt_key
.length
> 0)
138 download_link_href +
= '&k=' + crypt_key
;
140 if (!!document
.getElementById('upload_finished_download_page'))
142 document
.getElementById('upload_link').href
= download_link_href
;
143 document
.getElementById('upload_link_text').innerHTML
= web_root + download_link_href
;
147 var filename
= document
.getElementById('file_select').files
[0].name
;
148 var b
= encodeURIComponent("<?php echo t("DL
"); ?> \"" + filename +
"\":") +
"%0D" +
"%0A";
149 b +
= encodeURIComponent(web_root + download_link_href
) +
"%0D" +
"%0A";
150 if (false == isEmpty(date
))
152 b +
= "%0D" +
"%0A" +
encodeURIComponent("<?php echo t("VALID_UNTIL
"); ?>: " + date
.format('YYYY-MM-DD hh:mm (GMT O)')) +
"%0D" +
"%0A";
153 document
.getElementById('upload_link_email').href
= "mailto:?body=" + b +
"&subject=" +
encodeURIComponent(filename
);
157 var delete_link_href
= 'f.php?h=' + reference +
'&d=' + delete_code
;
158 document
.getElementById('delete_link').href
= delete_link_href
;
159 document
.getElementById('delete_link_text').innerHTML
= web_root + delete_link_href
;
164 document
.getElementById('date').style
.display
= 'none';
167 document
.getElementById('date').innerHTML
= '<span class="datetime" title="'
168 +
dateToUtcString(date
) +
' (GMT)">'
169 + date
.format('YYYY-MM-DD hh:mm (GMT O)')
171 document
.getElementById('date').style
.display
= '';
174 // Preview link (if allowed)
175 if (!!document
.getElementById('preview_link'))
177 document
.getElementById('upload_finished_preview').style
.display
= 'none';
178 var preview_link_href
= 'f.php?h=' + reference +
'&p=1';
179 if (crypt_key
.length
> 0)
181 preview_link_href +
= '&k=' + crypt_key
;
184 // Test if content can be previewed
185 type
= document
.getElementById('file_select').files
[0].type
;
186 if ((type
.startsWith('image/')
187 || type
.startsWith('audio')
188 || type
.startsWith('text/plain')
189 || type
.startsWith('video/'))
190 && !type
.includes('image/svg+xml'))
192 document
.getElementById('preview_link').href
= preview_link_href
;
193 document
.getElementById('preview_link_text').innerHTML
= web_root + preview_link_href
;
194 document
.getElementById('upload_finished_preview').style
.display
= '';
198 // Direct download link
199 var direct_download_link_href
= 'f.php?h=' + reference +
'&d=1';
200 if (crypt_key
.length
> 0)
202 direct_download_link_href +
= '&k=' + crypt_key
;
204 document
.getElementById('direct_link').href
= direct_download_link_href
;
205 document
.getElementById('direct_link_text').innerHTML
= web_root + direct_download_link_href
;
207 // Hide preview and direct download link if password is set
208 if (document
.getElementById('input_key').value
.length
> 0)
210 if (!!document
.getElementById('preview_link'))
211 document
.getElementById('upload_finished_preview').style
.display
= 'none';
212 document
.getElementById('upload_direct_download').style
.display
= 'none';
216 function show_upload_progression (percentage
, speed
, time_left
)
218 document
.getElementById('uploaded_percentage').innerHTML
= percentage
;
219 document
.getElementById('uploaded_speed').innerHTML
= speed
;
220 document
.getElementById('uploaded_time').innerHTML
= time_left
;
221 document
.title
= percentage +
" - <?php echo empty($cfg['title']) ? 'Jirafeau' : $cfg['title']; ?>";
224 function hide_upload_progression ()
226 document
.getElementById('uploaded_percentage').style
.display
= 'none';
227 document
.getElementById('uploaded_speed').style
.display
= 'none';
228 document
.getElementById('uploaded_time').style
.display
= 'none';
229 document
.title
= "<?php echo empty($cfg['title']) ? 'Jirafeau' : $cfg['title']; ?>";
232 function upload_progress (e
)
234 if (e
== undefined || e
== null ||
!e
.lengthComputable
)
237 // Init time estimation if needed
238 if (upload_time_estimation_total_size
== 0)
239 upload_time_estimation_total_size
= e
.total
;
241 // Compute percentage
242 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
245 p_str
= p
.toString() +
'%';
246 // Update estimation speed
247 upload_time_estimation_add(e
.loaded
);
249 var speed_str
= upload_time_estimation_speed_string();
250 speed_str
= upload_speed_refresh_limiter(speed_str
);
252 var time_str
= chrono_update(upload_time_estimation_time());
254 show_upload_progression (p_str
, speed_str
, time_str
);
257 function control_selected_file_size(max_size
, error_str
)
259 f_size
= document
.getElementById('file_select').files
[0].size
;
260 if (max_size
> 0 && f_size
> max_size
* 1024 * 1024)
262 pop_failure(error_str
);
263 document
.getElementById('send').style
.display
= 'none';
267 // add class to restyle upload form in next step
268 document
.getElementById('upload').setAttribute('class', 'file-selected');
270 document
.getElementById('options').style
.display
= 'block';
271 document
.getElementById('send').style
.display
= 'block';
272 document
.getElementById('error_pop').style
.display
= 'none';
273 document
.getElementById('send').focus();
277 function XHRErrorHandler(e
)
279 var text
= "${e.type}: ${e.loaded} bytes transferred"
283 function pop_failure (e
)
285 var text
= "<p>An error occured";
286 if (typeof e
!== 'undefined')
289 document
.getElementById('error_pop').innerHTML
= e
;
291 document
.getElementById('uploading').style
.display
= 'none';
292 document
.getElementById('error_pop').style
.display
= '';
293 document
.getElementById('upload').style
.display
= '';
294 document
.getElementById('send').style
.display
= '';
297 function add_time_string_to_date(d
, time
)
299 if(typeof(d
) != 'object' ||
!(d
instanceof Date
))
304 if (time
== 'minute')
306 d
.setSeconds (d
.getSeconds() +
60);
311 d
.setSeconds (d
.getSeconds() +
3600);
316 d
.setSeconds (d
.getSeconds() +
86400);
321 d
.setSeconds (d
.getSeconds() +
604800);
324 if (time
== 'fortnight')
326 d
.setSeconds (d
.getSeconds() +
1209600);
331 d
.setSeconds (d
.getSeconds() +
2592000);
334 if (time
== 'quarter')
336 d
.setSeconds (d
.getSeconds() +
7776000);
341 d
.setSeconds (d
.getSeconds() +
31536000);
347 function classic_upload (file
, time
, password
, one_time
, upload_password
)
349 // Delay time estimation init as we can't have file size
350 upload_time_estimation_init(0);
352 var req
= new XMLHttpRequest ();
353 req
.upload
.addEventListener ("progress", upload_progress
, false);
354 req
.addEventListener ("error", XHRErrorHandler
, false);
355 req
.addEventListener ("abort", XHRErrorHandler
, false);
356 req
.onreadystatechange
= function ()
358 if (req
.readyState
== 4 && req
.status
== 200)
360 var res
= req
.responseText
;
362 // if response starts with "Error" then show a failure
363 if (/^Error
/.test(res
))
369 res
= res
.split ("\n");
373 // convert time (local time + selected expiry date)
374 var localDatetime
= new Date();
375 if(!add_time_string_to_date(localDatetime
, time
))
377 pop_failure ('Error: Date can not be parsed');
380 expiryDate
= localDatetime
;
383 show_link (res
[0], res
[1], res
[2], expiryDate
);
387 pop_failure ("<?php echo t("ERR_OCC
"); ?>");
390 req
.open ("POST", 'script.php' , true);
392 var form
= new FormData();
393 form
.append ("file", file
);
395 form
.append ("time", time
);
397 form
.append ("key", password
);
399 form
.append ("one_time_download", '1');
400 if (upload_password
.length
> 0)
401 form
.append ("upload_password", upload_password
);
406 function check_html5_file_api ()
408 return window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
;
411 var async_global_transfered
= 0;
412 var async_global_file
;
413 var async_global_ref
= '';
414 var async_global_max_size
= 0;
415 var async_global_time
;
416 var async_global_transfering
= 0;
417 var async_global_last_code
;
419 function async_upload_start (max_size
, file
, time
, password
, one_time
, upload_password
)
421 async_global_transfered
= 0;
422 async_global_file
= file
;
423 async_global_max_size
= max_size
;
424 async_global_time
= time
;
426 var req
= new XMLHttpRequest ();
427 req
.addEventListener ("error", XHRErrorHandler
, false);
428 req
.addEventListener ("abort", XHRErrorHandler
, false);
429 req
.onreadystatechange
= function ()
431 if (req
.readyState
== 4 && req
.status
== 200)
433 var res
= req
.responseText
;
435 if (/^Error
/.test(res
))
441 res
= res
.split ("\n");
442 async_global_ref
= res
[0];
444 async_upload_push (code
);
447 req
.open ("POST", 'script.php?init_async' , true);
449 var form
= new FormData();
450 form
.append ("filename", async_global_file
.name
);
451 form
.append ("type", async_global_file
.type
);
453 form
.append ("time", time
);
455 form
.append ("key", password
);
457 form
.append ("one_time_download", '1');
458 if (upload_password
.length
> 0)
459 form
.append ("upload_password", upload_password
);
461 // Start time estimation
462 upload_time_estimation_init(async_global_file
.size
);
467 function async_upload_progress (e
)
469 if (e
== undefined || e
== null ||
!e
.lengthComputable
&& async_global_file
.size
!= 0)
472 // Compute percentage
473 var p
= Math
.round ((e
.loaded + async_global_transfered
) * 100 / (async_global_file
.size
));
476 p_str
= p
.toString() +
'%';
477 // Update estimation speed
478 upload_time_estimation_add(e
.loaded + async_global_transfered
);
480 var speed_str
= upload_time_estimation_speed_string();
481 speed_str
= upload_speed_refresh_limiter(speed_str
);
483 var time_str
= chrono_update(upload_time_estimation_time());
485 show_upload_progression (p_str
, speed_str
, time_str
);
488 function async_upload_push (code
)
490 async_global_last_code
= code
;
491 if (async_global_transfered
== async_global_file
.size
)
493 hide_upload_progression ();
494 async_upload_end (code
);
497 var req
= new XMLHttpRequest ();
498 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
499 req
.addEventListener ("error", XHRErrorHandler
, false);
500 req
.addEventListener ("abort", XHRErrorHandler
, false);
501 req
.onreadystatechange
= function ()
503 if (req
.readyState
== 4)
505 if (req
.status
== 200)
507 var res
= req
.responseText
;
509 // This error may be triggered when Jirafeau does not receive any file in POST.
510 // This may be due to bad php configuration where post_max_size is too low
511 // comparing to upload_max_filesize. Let's retry with lower file size.
512 if (res
=== "Error 23")
514 async_global_max_size
= Math
.max(1, async_global_max_size
- 500);
515 async_upload_push (async_global_last_code
);
518 else if (/^Error
/.test(res
))
524 res
= res
.split ("\n");
526 async_global_transfered
= async_global_transfering
;
527 async_upload_push (code
);
532 if (req
.status
== 413) // Request Entity Too Large
534 // lower async_global_max_size and retry
535 async_global_max_size
= Math
.max(1, parseInt (async_global_max_size
* 0.8));
537 async_upload_push (async_global_last_code
);
542 req
.open ("POST", 'script.php?push_async' , true);
544 var start
= async_global_transfered
;
545 var end
= start + async_global_max_size
;
546 if (end
>= async_global_file
.size
)
547 end
= async_global_file
.size
;
548 var blob
= async_global_file
.slice (start
, end
);
549 async_global_transfering
= end
;
551 var form
= new FormData();
552 form
.append ("ref", async_global_ref
);
553 form
.append ("data", blob
);
554 form
.append ("code", code
);
558 function async_upload_end (code
)
560 var req
= new XMLHttpRequest ();
561 req
.addEventListener ("error", XHRErrorHandler
, false);
562 req
.addEventListener ("abort", XHRErrorHandler
, false);
563 req
.onreadystatechange
= function ()
565 if (req
.readyState
== 4 && req
.status
== 200)
567 var res
= req
.responseText
;
569 if (/^Error
/.test(res
))
575 res
= res
.split ("\n");
577 if (async_global_time
!= 'none')
579 // convert time (local time + selected expiry date)
580 var localDatetime
= new Date();
581 if(!add_time_string_to_date(localDatetime
, async_global_time
)) {
582 pop_failure ('Error: Date can not be parsed');
585 expiryDate
= localDatetime
;
588 show_link (res
[0], res
[1], res
[2], expiryDate
);
591 req
.open ("POST", 'script.php?end_async' , true);
593 var form
= new FormData();
594 form
.append ("ref", async_global_ref
);
595 form
.append ("code", code
);
599 function upload (max_chunk_size
)
601 var one_time_checkbox
= document
.getElementById('one_time_download');
602 var one_time
= one_time_checkbox
!== null ? one_time_checkbox
.checked
: false;
603 if (check_html5_file_api ())
607 document
.getElementById('file_select').files
[0],
608 document
.getElementById('select_time').value
,
609 document
.getElementById('input_key').value
,
611 document
.getElementById('upload_password').value
617 document
.getElementById('file_select').files
[0],
618 document
.getElementById('select_time').value
,
619 document
.getElementById('input_key').value
,
621 document
.getElementById('upload_password').value
626 var upload_time_estimation_total_size
= 42;
627 var upload_time_estimation_transfered_size
= 42;
628 var upload_time_estimation_transfered_date
= 42;
629 var upload_time_estimation_moving_average_speed
= 42;
631 function upload_time_estimation_init(total_size
)
633 upload_time_estimation_total_size
= total_size
;
634 upload_time_estimation_transfered_size
= 0;
635 upload_time_estimation_moving_average_speed
= 0;
637 upload_time_estimation_transfered_date
= d
.getTime();
640 function upload_time_estimation_add(total_transfered_size
)
642 // Let's compute the current speed
644 var speed
= upload_time_estimation_moving_average_speed
;
645 if (d
.getTime() - upload_time_estimation_transfered_date
!= 0)
646 speed
= (total_transfered_size
- upload_time_estimation_transfered_size
)
647 / (d
.getTime() - upload_time_estimation_transfered_date
);
648 // Let's compute moving average speed on 30 values
649 var m
= (upload_time_estimation_moving_average_speed
* 29 + speed
) / 30;
650 // Update global values
651 upload_time_estimation_transfered_size
= total_transfered_size
;
652 upload_time_estimation_transfered_date
= d
.getTime();
653 upload_time_estimation_moving_average_speed
= m
;
656 function upload_time_estimation_speed_string()
659 var s
= upload_time_estimation_moving_average_speed
* 1000;
667 else if (s
< 1000000)
669 res
= Math
.floor(s
/100) / 10;
674 res
= Math
.floor(s
/100000) / 10;
679 return res
.toString() +
' ' + scale
;
682 function milliseconds_to_time_string (milliseconds
)
684 function numberEnding (number
) {
685 return (number
> 1) ?
translate ('PLURAL_ENDING') : '';
688 var temp
= Math
.floor(milliseconds
/ 1000);
689 var years
= Math
.floor(temp
/ 31536000);
691 return years +
' ' +
translate ('YEAR') +
numberEnding(years
);
693 var days
= Math
.floor((temp %
= 31536000) / 86400);
695 return days +
' ' +
translate ('DAY') +
numberEnding(days
);
697 var hours
= Math
.floor((temp %
= 86400) / 3600);
699 return hours +
' ' +
translate ('HOUR') +
numberEnding(hours
);
701 var minutes
= Math
.floor((temp %
= 3600) / 60);
703 return minutes +
' ' +
translate ('MINUTE') +
numberEnding(minutes
);
705 var seconds
= temp %
60;
707 return seconds +
' ' +
translate ('SECOND') +
numberEnding(seconds
);
709 return translate ('LESS_1_SEC');
712 function upload_time_estimation_time()
714 // Estimate remaining time
715 if (upload_time_estimation_moving_average_speed
== 0)
717 return (upload_time_estimation_total_size
- upload_time_estimation_transfered_size
)
718 / upload_time_estimation_moving_average_speed
;
721 var chrono_last_update
= 0;
722 var chrono_time_ms
= 0;
723 var chrono_time_ms_last_update
= 0;
724 function chrono_update(time_ms
)
728 // Don't update too often
729 if (d
.getTime() - chrono_last_update
< 3000 &&
730 chrono_time_ms_last_update
> 0)
731 chrono
= chrono_time_ms
;
734 chrono_last_update
= d
.getTime();
735 chrono_time_ms
= time_ms
;
737 chrono_time_ms_last_update
= d
.getTime();
740 // Adjust chrono for smooth estimation
741 chrono
= chrono
- (d
.getTime() - chrono_time_ms_last_update
);
743 // Let's update chronometer
746 time_str
= milliseconds_to_time_string (chrono
);
750 var upload_speed_refresh_limiter_last_update
= 0;
751 var upload_speed_refresh_limiter_last_value
= '';
752 function upload_speed_refresh_limiter(speed_str
)
755 if (d
.getTime() - upload_speed_refresh_limiter_last_update
> 1500)
757 upload_speed_refresh_limiter_last_value
= speed_str
;
758 upload_speed_refresh_limiter_last_update
= d
.getTime();
760 return upload_speed_refresh_limiter_last_value
;
764 document
.addEventListener('DOMContentLoaded', function(event
) {
765 // Search for all datetime fields and convert the time to local timezone
766 convertAllDatetimeFields();
769 // Add copy event listeners
770 function copyLinkToClipboard(link_id
) {
771 var focus
= document
.activeElement
;
772 var e
= document
.getElementById(link_id
);
774 var tmp
= document
.createElement("textarea");
775 document
.body
.appendChild(tmp
);
776 tmp
.textContent
= e
.href
;
778 tmp
.setSelectionRange(0, tmp
.value
.length
);
779 document
.execCommand("copy");
780 document
.body
.removeChild(tmp
);
785 function addCopyListener(button_id
, link_id
) {
786 if(document
.getElementById(button_id
)){
787 document
.getElementById(button_id
)
788 .addEventListener("click", function() {
789 copyLinkToClipboard(link_id
);});