-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
388 lines (326 loc) · 8.97 KB
/
index.html
File metadata and controls
388 lines (326 loc) · 8.97 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<html>
<head>
<title>Music Player</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="foundation-icons/foundation-icons.css" />
<script src="crypto-js.min.js"></script>
<script src="jsmediatags.min.js"></script>
</head>
<body>
<script>
const player = (function() {
myHash = null;
audioPlayer = null;
current = -1;
songs = [];
function init() {
updateDesc();
loadPlaylist();
audioPlayer = new Audio();
audioPlayer.addEventListener('ended', next);
document.querySelector("#uploader").addEventListener('submit', function(e) {
e.preventDefault();
login();
});
}
function play() {
if (!audioPlayer) return;
if (current < 0) {
next();
};
if (current >=0) {
audioPlayer.play();
}
updateDesc();
}
function pause() {
if (!audioPlayer) return;
audioPlayer.pause();
updateDesc();
}
function reset() {
if (!audioPlayer) return;
current = -1;
next();
}
async function next() {
if (!audioPlayer) return;
current++;
if (current >= songs.length) current = 0;
await setSource(songs[current]);
if (audioPlayer.paused) play();
updateDesc();
}
async function previous() {
if (!audioPlayer) return;
current--;
if (current < 0) current = songs.length - 1;
await setSource(songs[current]);
if (audioPlayer.paused) play();
updateDesc();
}
function toggle() {
const node = document.querySelector('#play');
if (audioPlayer.paused) {
play();
node.classList.remove('fi-play');
node.classList.add('fi-pause');
} else {
pause();
node.classList.remove('fi-pause');
node.classList.add('fi-play');
}
}
function updateDesc(text) {
const node = document.querySelector('#desc');
if (text) {
node.innerHTML = text;
return;
}
let desc = '';
if (!audioPlayer) {
desc = 'Hello';
}
if (audioPlayer) {
const splitted = songs[current].split('|');
const metaData = splitted[1];
const decryptedMetaData = JSON.parse(decrypt(metaData));
desc = `${decryptedMetaData.tags.title} ${decryptedMetaData.tags.artist}`;
if (audioPlayer.paused) desc += ' (paused)';
}
node.innerHTML = desc;
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
// create a view into the buffer
var ia = new Uint8Array(ab);
// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: mimeString});
return blob;
}
async function setSource(playlistItem) {
const fileName = playlistItem.split('|')[0];
isLoading();
const response = await fetch(fileName, {
cache: 'force-cache',
headers: {
'Cache-Control': 'max-age=3600',
'Pragma': 'max-age=3600'
}
});
const data = await response.blob();
const reader = new FileReader();
const promise = new Promise(resolve => {
reader.onload = () => {
const rawData = reader.result;
const decrypted = decrypt(rawData);
audioPlayer.src = decrypted;
stoppedLoading();
resolve();
}
reader.readAsBinaryString(data);
});
return promise;
}
function isLoading() {
document.querySelector('#loading').innerHTML = '...';
}
function stoppedLoading() {
document.querySelector('#loading').innerHTML = '';
}
async function readMetaData(data) {
return new Promise((resolve, reject) => {
new jsmediatags.Reader(dataURItoBlob(data)).read({
onSuccess: (tag) => {
resolve(tag);
},
onError: (error) => {
reject(error);
}
});
});
}
async function upload() {
const node = document.querySelector('#file');
if (node.files.length < 1) return;
const file = node.files[0];
const reader = new FileReader()
reader.onload = async (event) => {
const data = event.target.result;
const metaData = await readMetaData(data);
const encryptedMetaData = encrypt(JSON.stringify(metaData));
const encrypted = encrypt(data);
const form_data = new FormData();
form_data.append('file', encrypted);
const name = hash(file.name);
form_data.append('name', name);
form_data.append('metaData', encryptedMetaData);
form_data.append('secret', CryptoJS.MD5(name).toString());
fetch("connector.php", {
method:"POST",
body : form_data
}).then( function(response) {
return response.text();
}).then( function(responseData) {
if (responseData != 'ok') {
updateDesc(responseData);
} else {
loadPlaylist();
updateDesc(file.name + ' uploaded!');
node.value = '';
closeAdding();
}
});
}
reader.readAsDataURL(file)
}
function openAdding() {
document.querySelector('#adding').style.display = 'block';
}
function closeAdding() {
document.querySelector('#adding').style.display = 'none';
}
function loadPlaylist() {
fetch('playlist.txt', {cache: "no-store"})
.then(function(response) {
return response.text()
})
.then(function(response) {
songs = response
.split(/\r?\n/)
.filter(function(file) {
return file != '';
});
});
}
function encrypt(data) {
return CryptoJS.AES
.encrypt(data, myHash)
.toString();
}
function decrypt(data) {
return CryptoJS.AES
.decrypt(data, myHash)
.toString(CryptoJS.enc.Utf8);
}
function hash(text) {
return CryptoJS.SHA3(text).toString();
}
function login() {
const node = document.querySelector('#pass');
myHash = hash(node.value);
node.value = '';
const passView = document.querySelector('#passView');
passView.style.display = 'none';
const mainNode = document.querySelector('#main');
mainNode.style.display = 'block';
}
addEventListener("load", () => {
init();
});
return {
upload,
login,
reset,
play,
pause,
next,
previous,
toggle,
openAdding
};
})();
</script>
<style>
#main {
display: none;
}
body {
background-color: black;
color: #EEE;
padding: 10px;
}
.controls {
text-align: center;
position: fixed;
top: calc(100% - 60px);
width: 100%;
}
.control {
font-size: 40px;
padding: 20px;
cursor: pointer;
}
#desc {
font-size: 40px;
}
#passView {
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#uploader {
font-size: 40px;
width: fit-content;
}
#pass {
font-size: 20px;
}
#openAddingButton {
position: fixed;
left: calc(100% - 60px);
top: calc(100% - 60px);
padding: 0;
}
#adding {
display: none;
}
#header {
overflow: hidden;
}
</style>
<div id='main'>
<div id='header'>
<span id='desc'> </span>
<span id='loading'> </span>
</div>
<div class='controls'>
<i class="fi-stop control" onclick='player.reset();'></i>
<i class="fi-previous control" onclick='player.previous();'></i>
<i class="fi-play control" onclick='player.toggle();' id='play'></i>
<i class="fi-next control" onclick='player.next();'></i>
</div>
<i class="fi-plus control" onclick='player.openAdding();' id='openAddingButton'></i>
<div id='adding'>
<input type='file' id='file' />
<input type='button' value='Upload' onclick='player.upload();'/>
</div>
</div>
<div id='passView'>
<form id='uploader'>
Hello
<input type='password' id='pass' />
</form>
</div>
</body>
</html>