aboutsummaryrefslogtreecommitdiff
path: root/static/sizechk.js
blob: 63786ccb85c27315ff119d31b35f6a9cbc892aa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* SPDX-License-Identifier: CC0-1.0 */

function humanize_size(bytes) {
	if (Math.abs(bytes) < 1024) {
		return bytes + ' B';
	}
	const units = [' KiB', ' MiB', ' GiB'];
	let u = -1;
	const r = 10;
	do {
		bytes /= 1024;
		++u;
	} while (Math.round(Math.abs(bytes) * r) / r >= 1024 &&
		u < units.length - 1);
	return bytes.toFixed(1) + units[u];
}

const uploadField = document.getElementById("fileupload");
uploadField.onchange = function() {
	if (this.files[0].size > max_file_size) {
		alert(`File size ${humanize_size(this.files[0].size)} ` +
			`exceeds ${humanize_size(max_file_size)}. ` +
			`Either submit a smaller file, ` +
			`or use a file hosting service and submit the URL.`);
		this.value = "";
	}
};