aboutsummaryrefslogtreecommitdiff
path: root/assets/cargohold.js
blob: 2a4ca76bc0b42d332b52ddeadd2b8dd958b8b897 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * TODO:
 *	check if files exist
 *	check file sizes
 */

var running = false;
var queue = [];

function queue_work(){
	let i = 0;
	for(; i < queue.length; i++){
		item = queue[i];
		if(!item.started){
			console.log(item);
			console.log("Starting " + item.file.name);
			item.started = true;

			let req = new XMLHttpRequest();
			let data = new FormData();
			data.append("file", item.file);                                
			req.upload.addEventListener("progress", function(evt){
				if(evt.lengthComputable){
					console.log("Upload for " + item.file.name + " at " + evt.loaded / evt.total);
					item.node.childNodes[1].value = evt.loaded;
				}
			});
			req.onloadstart = function(evt){
				console.log("Upload for " + item.file.name + " started");
				item.node.childNodes[1].max = item.file.size;
				item.node.childNodes[1].value = 0;
			};
			req.onloadend = function(evt){
				console.log("Upload for " + item.file.name + " ended");
				queue_work();
			};
			req.onerror = function(evt){
				console.log("Upload for " + item.file.name + " errored");
				item.node.className += " errored";
			};
			req.onabort = function(evt){
				console.log("Upload for " + item.file.name + " aborted");
				item.node.className += " errored";
			};
			req.onreadystatechange = function(evt){
				console.log("Upload for " + item.file.name + " state " + req.readyState);
				if(req.readyState === XMLHttpRequest.DONE && req.status != 200){
					console.log("Upload for " + item.file.name + " failed with " + req.status);
					item.node.className += " errored";
				}
			};
			req.open("POST", 'upload');
			req.send(data);
			return;
		}
	}

	console.log("Queue run done");
	running = false;
}

function queue_run(){
	if(running || queue.length == 0){
		return;
	}

	running = true;

	console.log("Starting queue run");
	queue_work();
}

function element(id){
	return document.getElementById(id);
}

function node(tag, style, text){
	let new_node = document.createElement(tag);
	new_node.className = style;
	if(text){
		new_node.textContent = text;
	}
	return new_node;
}

function upload_element(file){
	let new_node = node("div", "queue-entry");
	new_node.appendChild(node("span", "name", file.name));
	new_node.appendChild(node("progress", "progress"));
	element("queue").appendChild(new_node);
	return new_node;
}

function upload_start(element){
	for(let i = 0; i < element.target.files.length; i++){
		console.log(element.target.files[i]);
		queue.push({"file": element.target.files[i], "node": upload_element(element.target.files[i]), "progress": 0, "started": false});
	}
	queue_run();
}

function listing_clear(){
	element("dirlisting").innerHTML = "";
}

function interface_update(access){
	if(!access.includes("c")){
		element("tab-upload-label").style.display = "none";
	}

	if(!access.includes("r")){
		element("tab-view-label").style.display = "none";
		element("tab-upload").checked = "checked";
	}
}

function listing_add(file, style = "default"){
	let link = node("a", "listing-entry", file.name);
	link.href = "file/" + encodeURIComponent(file.name);
	link.setAttribute("data-filename", file.name)

	if(style == "gallery"){
		let image = node("img", "preview");
		image.src = "preview/" + encodeURIComponent(file.name);
		//link.insertBefore(image, link.firstChild);
		link.appendChild(image);
	}

	element("dirlisting").appendChild(link);
}

function listing_update(){
	listing_clear();
	let req = new XMLHttpRequest();
	req.onload = function(evt){
		let data = JSON.parse(req.response);
		interface_update(data.access);

		for(let i = 0; i < data.files.length; i++){
			listing_add(data.files[i], data.display);
		}
	};
	req.open("GET", "listing");
	req.send();
}

function download_all(ev){
	let links = document.getElementsByClassName("listing-entry");
	var current = 0;
	var interval = setInterval(function(){
		if(current < links.length){
			link = links[current];
			link.download = link.getAttribute("data-filename");
			link.click();
			link.removeAttribute("download");
			current++;
		}
		else{
			clearInterval(interval);
		}
	}, 1000);
}

function init(){
	element("file-submit").style.display = "none";
	element("files").onchange = upload_start;
	element("download-all").onclick = download_all;
	listing_update();
}

window.onload = init;