forked from bedrock-viz/bedrock-viz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontend.js
652 lines (601 loc) · 24.8 KB
/
frontend.js
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
frontend.js - bedrock-viz GUI front end
by Alex Matulich
v1.0, 29 April 2022
This application requires node.js.
Download the Windows installer from https://nodejs.org/en/download/
After installing node.js, add the path to node.exe to your %PATH%
environment variable.
Once you have installed node.js, start this bedrock-viz front-end application
like this:
> cd [your bedrock-viz folder]
> node frontend.js
Then, from your browser, access the app using the address
localhost:3333
That's it! When you create a map, the bedrock-viz output appears in the
commandline console window from which you started the app.
*/
// ---------- VALUE INITIALIZATIONS ----------
const http = require('http') // http server features
const fs = require('fs') // local file handling
const os = require('os') // operating system specific functions
const util = require('util') // util functions
const path = require('path') // basic path name parsing
const { spawn } = require('child_process') // this is what we use to invoke bedrock-viz
const PORT = 3333 // http port to use
let lastIP = '0' // last IP address seen
let worlds = [] // array of world objects
// directory locations
const worldPath = process.env.WORLDS_PATH || '/tmp/worlds' // where our worlds are stored (can and should be readonly)
const mapsPath = process.env.MAPS_PATH || '/tmp/maps' // where our maps get stored
const managementKey = process.env.MANAGEMENT_KEY // management key for enabling management functions
// logging initialization
const funcs = {
log: console.log.bind(console),
info: console.info.bind(console),
warn: console.warn.bind(console),
error: console.error.bind(console),
debug: (console.debug || console.log).bind(console)
}
/**
* @param {function|string} fn
*/
function logPrefix (fn) {
Object.keys(funcs).forEach(function (k) {
console[k] = function () {
const s = typeof fn === 'function' ? fn() : fn
arguments[0] = util.format(s, arguments[0])
funcs[k].apply(console, arguments)
}
})
}
/**
* @param {function|string} [prefix] prefix
*/
function patch (prefix) {
if (typeof prefix === 'function') {
logPrefix(prefix)
} else if (typeof prefix === 'string' && prefix) {
logPrefix(() => prefix + ' ' + timestamp())
} else {
logPrefix(timestamp)
}
}
/**
* @returns {string}
*/
function timestamp () {
return '[' + new Date().toISOString() + ']'
}
patch()
// ---------- CODE INITIALIZATIONS ----------
// if worldPath doesn't exist...
if (!fs.existsSync(worldPath)) {
console.log(`Fatal error: Unable to read ${worldPath}`)
process.abort()
}
// if mapsPath doesn't exist...
if (!fs.existsSync(mapsPath)) {
try {
fs.mkdirSync(mapsPath)
} catch (err) {
console.log(`Fatal error: Unable to create ${mapsPath}`)
process.abort()
}
}
// populate worlds[] array
refreshWorlds()
/*
Static file and dynamic content server loop:
* Files that exist in the 'mapsPath' path are served.
* If the path is "/thumbnails/<icon_name>.jpg" then the icon found in "'worldPath'/icon_name/world_icon.jpeg" is served.
* If the path is "/" or "/index.html" then the output is dynamic, and the content depends on whether the user is local or remote.
Local users can create and delete bedrock-viz maps, and make them public or private.
Remote users can only view public maps.
*/
const server = http.createServer(function (request, response) {
let isManagementUser = false
const ip = (request.headers['x-forwarded-for'] || '').split(',')[0] || request.connection.remoteAddress
if (ip === '::1' || ip === 'ffff:127.0.0.1' || ip === '127.0.0.1') {
isManagementUser = true
}
const cookies = parseCookies(request)
if (managementKey && cookies.MANAGEMENTKEY === managementKey) {
isManagementUser = true
}
if (ip !== lastIP) {
// display log message only if IP address changes
console.log(`IP address=${ip}`, isManagementUser ? ' local' : ' remote')
lastIP = ip
}
let url = request.url.replace(/^(\.)+/, '.') // for security, prevent paths like ../../../ to get to root
if (url === '/') url = '/index.html'
const filePath = (url.indexOf('thumbnails') > 0)
? worldPath + '/' + path.basename(url, '.jpg') + '/world_icon.jpeg'
: mapsPath + url
const extname = String(path.extname(filePath)).toLowerCase()
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
}
const contentType = mimeTypes[extname] || 'application/octet-stream'
let poststring
if (request.method === 'POST') {
poststring = ''
// get POST data
request.on('data', function (chunk) {
poststring += chunk
})
// process POST data
request.on('end', function () {
const params = new URLSearchParams(poststring)
// operation to perform (newmap, delete, html_buttonPublicToggle)
const op = params.get('op')
// which map to perform it on
const mapname = params.get('mapname')
const scrollposparm = params.get('scrollpos')
const scrollpos = (scrollposparm === null) ? 0 : Number(scrollposparm)
const customConfig = params.get('customconfig')
if (op === 'newmap') {
const mapdetail = params.get('mapdetail')
const outputDirectory = `${mapsPath}${path.sep}${mapname}`
const tmpDir = `${os.tmpdir()}${path.sep}`
const sourceDirectory = `${worldPath}${path.sep}${mapname}`
const workingDirectory = fs.mkdtempSync(tmpDir)
console.log(`Working directory: ${workingDirectory}`)
fs.cpSync(sourceDirectory, workingDirectory, { recursive: true })
console.log(`COPIED ${sourceDirectory} to ${workingDirectory}`)
deleteMap(mapname)
const commandArgs = ['--db', workingDirectory, '--out', outputDirectory, mapdetail]
if (customConfig !== null) {
commandArgs.push('--cfg', `${mapsPath}${path.sep}${mapname}_bedrock_viz.cfg`)
}
console.log(`running bedrock-viz with [${commandArgs.join(' ')}]`)
// invoke bedrock-viz
const bedrockViz = spawn('bedrock-viz', commandArgs, {
shell: true,
cwd: __dirname
})
bedrockViz.stdout.on('data', data => {
console.log(`stdout: ${data}`)
})
bedrockViz.stderr.on('data', data => {
console.log(`stderr: ${data}`)
})
bedrockViz.on('error', (error) => {
console.log(`error: ${error.message}`)
fs.rmSync(workingDirectory, { recursive: true, force: true })
console.log(`DELETED ${workingDirectory}`)
})
bedrockViz.on('exit', code => {
console.log(`bedrock-viz exited with code ${code}`)
if (code === 0) {
// created map successfully
// write some additional json data into the map's folder for later retrieval
const wname = params.get('worldname')
const publicmap = (params.get('publicmap') !== 'false')
writeMetafile({ name: mapname, worldname: wname, mapdetail, publicmap })
}
response.writeHead(200, { 'Content-Type': contentType })
response.end(makeMapList(isManagementUser, '', scrollpos), 'utf-8')
fs.rmSync(workingDirectory, { recursive: true, force: true })
console.log(`DELETED ${workingDirectory}`)
})
} else {
let excludeworld = ''
if (op === 'delete') {
deleteMap(mapname)
// used if asynchronous deletion isn't done when the map list is regenerated
excludeworld = mapname
} else if (op === 'html_buttonPublicToggle') {
const publicmap = params.get('publicmap')
const k = findWorldIndex(mapname)
if (k >= 0) {
worlds[k].publicmap = (publicmap !== 'false')
writeMetafile(worlds[k])
}
} else if (op === 'refresh') {
refreshWorlds()
}
response.writeHead(200, { 'Content-Type': contentType })
response.end(makeMapList(isManagementUser, excludeworld, scrollpos), 'utf-8')
}
})
// done with POST processing
} else if (url === '/index.html') {
// output map list page if '/' or '/index.html' is requested
response.writeHead(200, { 'Content-Type': contentType })
response.end(makeMapList(isManagementUser, '', 0), 'utf-8')
} else {
// otherwise just process the HTTP GET request normally
fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code === 'ENOENT') {
response.writeHead(404, { 'Content-Type': 'text/html' })
response.end('<!DOCTYPE html><html><head></head><body><h1>Error 404: Page not found</body></html>', 'utf-8')
} else {
response.writeHead(500)
response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n')
}
} else {
response.writeHead(200, { 'Content-Type': contentType })
response.end(content, 'utf-8')
}
})
}
})
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT} (${JSON.stringify(server.address())})`)
})
console.log(`CWD: ${__dirname}`)
// end of server loop
// ---------- FUNCTIONS ----------
/**
* Read the Minecraft worlds directory and build a list of relevant information (directory name, location, timestamp)
*/
function refreshWorlds () {
let stats, world, worldName, i, j, customConfigLocation
console.log('getting Minecraft worlds information')
const files = fs.readdirSync(worldPath)
if (files.length === 0) return
worlds = []
for (i = 0, j = 0; i < files.length; i++) {
world = `${worldPath}${path.sep}${files[i]}${path.sep}`
try {
stats = fs.statSync(world)
} catch (err) {
console.log(`Warning: Unable to get information on ${world}`)
continue
}
if (stats.isDirectory()) {
try {
worldName = fs.readFileSync(`${world}levelname.txt`, 'utf8')
} catch (err) {
worldName = 'Unknown'
}
try {
customConfigLocation = fs.existsSync(`${mapsPath}${path.sep}${files[i]}_bedrock_viz.cfg`)
} catch (err) {
customConfigLocation = false
}
worlds[j++] = {
name: files[i],
worldname: worldName,
mtimeMs: stats.mtimeMs,
mtime: stats.mtime,
hasmap: false,
mappable: true,
mapdetail: '--html-all',
publicmap: true,
customConfig: customConfigLocation
}
const worldMapsPath = `${mapsPath}${path.sep}${files[i]}`
if (!fs.existsSync(worldMapsPath)) {
fs.mkdirSync(worldMapsPath)
}
}
worlds.sort((a, b) => {
return b.mtimeMs - a.mtimeMs
})
console.log(`found following worlds: '${worlds.map(a => a.name).join(',')}'`)
}
}
/**
* generate the top of the main HTML page
* @param {boolean} isManagementUser
* @returns {string}
*/
function htmlPageTop (isManagementUser) {
const header = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<title>Minecraft maps from bedrock-viz</title>\n\
<style>\n\
body { background: #060606; color: #ffffffCC; font-size: 16px; font-family:sans-serif; }\n\
a:link, a:visited { color: #00BB00; }\
a:hover { color: #00EEFF; }\n\
a:active { color: yellow; }\n\
.row { display:flex; flex-wrap:wrap; align-items:center; border: 1px solid green; width:99%; padding:0.5em 1em 0.5em 1em; }\n\
.row .image { width:320px; height:180px; border:3px ridge silver; text-align:center; margin:0.5em 1em 0.5em 1em; }\n\
.row .image img {filter: brightness(95%)}\n\
.row .desc { text-align:left; margin: 0.5em; }\n\
.row .desc h2 { margin-top:0; }\n\
form.mapaction { display:inline-block; font-size:larger; }\n\
form.refresh { float:right; margin-right:2em; font-size:larger; }\n\
form.mapaction input, form.mapaction select, form.refresh input { background:#BBDDCC; font-size:inherit; }\n\
form.mapaction div.private { border: 1px solid red; padding:0.1em; background:#550000; }\n\
form.mapaction div.public { border: 1px solid #00FF00; padding:0.1em; background:#005500; }\n\
.date { font-size: smaller; font-style:italic; }\n\
.modal { display:none; align-content:center; position: fixed; z-index:1; left:0; top:0; width:100%; height:100%; background-color:#060606; background-color:rgba(6,6,6,0.5); }\n\
.modal-content { width:150px; height:180px; margin:auto; text-align:center; color:yellow; }\n\
.modal-content img { display:inline-block; }\n\
#footer p { font-size:smaller; text-align:center; }\n\
</style>\n\
<script>\n\
function setscrollpos(id) { document.getElementById(id).value=window.scrollY.toString(); return true; }\n\
function spinner() { document.getElementById('spinner').style.display='flex'; }\n\
function confirmdelete(name,wname) { setscrollpos('d'+name); return confirm('Confirm DELETE the map for \"'+wname+'\".'); }\n\
function confirmcreate(name,wname,repl) { setscrollpos('n'+name); var act = repl ? 'REPLACE map for ' : 'Create new map for '; if(confirm(act+wname+'?')) { spinner(); return true; } else return false; }\n\
function refreshmodal() { spinner(); }\n\
</script>\n\
</head>\n<body>\n<div id=\"content\">"
return header +
(isManagementUser ? '<form class="refresh" action="/" method="POST"><input type="hidden" name="op" value="refresh"><input type="submit" value="🔄️ Refresh world list" title="Click if you played Minecraft since starting the server" onclick="refreshmodal();"></form>' : '') +
'<h1><img src="https://static.wikia.nocookie.net/minecraft_gamepedia/images/0/09/Minecraft_Twitter_logo.jpeg" style="width:40px; vertical-align:middle;" alt="[logo]"> <cite>Minecraft</cite> maps</h1>\n'
}
/**
* Generate a row in the worlds table. If the world has a map, link to it. If isManagementUser=true, include buttons to delete the map and create a new map.
*
* @param {boolean} isManagementUser
* @param {*} world
* @param {string} imgPath
* @param {Date|number|string} mapTime
*/
function htmlMakeRow (isManagementUser, world, imgPath, mapTime) {
const indexHtmlPath = world.name + '/index.html'
let row = '<div class="row' + (world.hasmap ? ' hasmap' : '') + '"><div class="image">'
row += (world.hasmap ? '<a href="' + indexHtmlPath + '">' + imgPath + '</a>\n' : imgPath) + '</div>\n<div class="desc"><h2>'
row += (world.hasmap ? '<a href="' + indexHtmlPath + '">' + world.worldname + '</a>' : world.worldname) + '</h2>\n'
if (world.mappable && isManagementUser) {
row += htmlButtonNewMap(world)
}
row += (world.mtime === 'Unknown' ? '<p class="date">World no longer exists</p>\n' : '<p class="date">World updated: ' + world.mtime + '</p>\n')
if (world.hasmap) {
row += '<p class="date">Map updated: ' + mapTime + '</p>\n'
if (isManagementUser) {
row += (htmlButtonDelete(world) + htmlButtonPublicToggle(world))
}
}
row += '</div>\n'
return row + '</div>\n'
}
/**
* delete button - called by htmlMakeRow()
* @param {*} world
* @returns {string}
*/
function htmlButtonDelete (world) {
return '<form class="mapaction" method="post" action="/"><input type="hidden" name="mapname" value="' +
world.name + '"><input type="hidden" name="op" value="delete"><input type="hidden" id="d' + world.name + '" name="scrollpos" value="0">' +
"<input type=\"submit\" value=\"🗑️ Delete map\" onclick=\"return confirmdelete('" +
world.name + "','" + world.worldname + "');\"></form>\n"
}
/**
* new map button - called by htmlMakeRow()
* @param {*} world
* @returns {string}
*/
function htmlButtonNewMap (world) {
let buttonText = '🗺️ Make new map'
if (world.customConfig) {
buttonText = `${buttonText} with custom config`
}
return '<form class="mapaction" method="post" action="/">' +
'<input type="hidden" name="mapname" value="' + world.name + '">' +
'<input type="hidden" name="op" value="newmap">' +
'<input type="hidden" name="worldname" value="' + world.worldname + '">' +
'<input type="hidden" id="n' + world.name + '" name="scrollpos" value="0">' +
'<input type="hidden" name="publicmap" value="' + world.publicmap.toString() + '">' +
(world.customConfig ? '<input type="hidden" name="customconfig" value="' + world.customConfig.toString() + '">' : '') +
'<input type="submit" value="' + buttonText + '" onclick="return confirmcreate(\'' + world.name + '\',\'' + world.worldname + '\',\'' + world.hasmap.toString() + '\');">' +
htmlSelectDetail(world) +
'</form>'
}
/**
* map detail selection button - called by htmlButtonNewMap()
* @param {*} world
* @returns {string}
*/
function htmlSelectDetail (world) {
let sel0 = ''; let sel1 = ''; let sel2 = ''
const sel = ' selected'
switch (world.mapdetail) {
case '--html':
sel0 = sel
break
case '--html-most':
sel1 = sel
break
default:
sel2 = sel
}
return ' showing: <select name="mapdetail">' +
'<option value="--html"' + sel0 + '>Overviews only</option>' +
'<option value="--html-most"' + sel1 + '>Overviews+Biomes</option>' +
'<option value="--html-all"' + sel2 + '>ALL details</option>' +
'</select>'
}
/**
* map privacy toggle - called by htmlMakeRow()
* @param {*} world
* @returns {string}
*/
function htmlButtonPublicToggle (world) {
const newToggleValue = !world.publicmap
return ' <form class="mapaction" method="post" action="/">' +
'<div class="' + (world.publicmap ? 'public' : 'private') + '">' +
'<input type="hidden" name="mapname" value="' + world.name + '">' +
'<input type="hidden" name="op" value="html_buttonPublicToggle">' +
'<input type="hidden" id="p' + world.name + '" name="scrollpos" value="0">' +
'<input type="hidden" name="publicmap" value="' + newToggleValue.toString() + '"> Map is ' + (world.publicmap ? '<strong>public</strong> ' : '<strong>private</strong> ') +
'<input type="submit" value="' + (world.publicmap ? '🔒️ Make private' : '🔓️ Make public') + '" onclick="return setscrollpos(\'p' + world.name + '\');">' +
'</div>' +
'</form>'
}
/**
* generate the bottom of the main HTML page - this also includes a scroll directive to scroll the page to the last position before it got regenerated
* @param {number} scrollPosition
* @returns {string}
*/
function htmlPageBottom (scrollPosition = 0) {
return '</div>' +
'<div id="spinner" class="modal">' +
'<div class="modal-content"><img src="https://static.wikia.nocookie.net/minecraft_gamepedia/images/c/c0/End_Crystal_%28Slateless%29.gif" width="150" height="160" alt="[please wait]"><br>See console</div>' +
'</div>' +
'<script>window.scrollTo(0,' + scrollPosition + ');</script>' +
'<div id="footer">' +
'<hr>' +
'<p>GUI front end for <a href="https://github.com/bedrock-viz/bedrock-viz" target="_blank">bedrock-viz</a> <cite>Minecraft</cite> map viewer.</p>' +
'<p>Front end by <a href="https://www.nablu.com/p/about.html" target="_blank">Alex Matulich</a>.</p>' +
'<p> Title and \'busy\' graphic assets obtained from the <a href="" target="_blank">Minecraft Wiki</a>.</p>' +
'</div>' +
'</body>' +
'</html>'
}
/**
* Given a world directory name, find its index in the worlds[] list, return -1 if not found.
* @param {string} dir
* @returns {number}
*/
function findWorldIndex (dir) {
let i = 0
const len = worlds.length
while (i < len && dir !== worlds[i].name) ++i
return i >= len ? -1 : i
}
/**
* Build the list of worlds and maps.
* If isManagementUser=true, a complete list of all worlds on the computer is generated, with buttons to delete a map (if it exists) or generate a new map.
* If isManagementUser=false, only worlds with maps are shown, with no options to delete or generate maps.
* The excludeMap parameter specifies a map directory name to exclude when building the list, in case a deletion operation is still ongoing when this function is called.
* @param {boolean} isManagementUser
* @param {string} excludeMap
* @param {number} scrollPosition
* @returns {string}
*/
function makeMapList (isManagementUser, excludeMap = '', scrollPosition = 0) {
let i; let k; let rows = 0; let mapsFound = 0; let img; let stats; let istats; let wrld; let newUnmappable = false; let removeindex = -1; let includemap
let fpath; let ipath; let mapinfopath; let rawmapinfo; let mapinfo; let infotitle; let infodetail = '--html-all'; let publicmap = true
console.log('Making html map list')
let html = htmlPageTop(isManagementUser)
// part 1 - scan mapsPath for maps and update corresponding worlds[] objects with whatever is found, and also find maps for worlds that were deleted from Minecraft
const files = fs.readdirSync(mapsPath)
if (files.length > 0) {
for (i = 0; i < files.length; i++) {
fpath = `${mapsPath}${path.sep}${files[i]}`
try {
stats = fs.statSync(fpath)
} catch (err) {
console.log(`Error getting ${fpath}`)
continue
}
if (!stats.isDirectory()) continue
ipath = `${fpath}${path.sep}index.html`
try {
istats = fs.statSync(ipath)
} catch (err) {
console.log(`Error getting ${ipath}`)
continue
}
mapinfopath = `${fpath}${path.sep}mapinfo.json`
mapinfo = ''
if (fs.existsSync(mapinfopath)) {
rawmapinfo = fs.readFileSync(mapinfopath)
mapinfo = JSON.parse(rawmapinfo)
infotitle = mapinfo.worldname !== undefined ? mapinfo.worldname : 'Unknown - not in Minecraft worlds'
infodetail = mapinfo.mapdetail !== undefined ? mapinfo.mapdetail : '--html-all'
publicmap = mapinfo.publicmap !== undefined ? mapinfo.publicmap : true
} else {
infodetail = '--html-all'
publicmap = true
}
k = findWorldIndex(files[i])
includemap = (files[i] !== excludeMap)
if (k < 0) { // if map doesn't exist in Minecraft worlds, add it to the worlds[] list
worlds[worlds.length] = {
name: files[i],
worldname: infotitle,
mtimeMs: istats.mtimeMs,
mtime: 'Unknown',
hasmap: includemap,
mappable: false,
mapdetail: infodetail,
publicmap
}
newUnmappable = true
} else { // if the map corresponds to a world, just update the worlds[] data with the mapinfo.json contents
worlds[k].hasmap = includemap
worlds[k].mapdetail = infodetail
if (worlds[k].hasmap) worlds[k].publicmap = publicmap
}
if (includemap) ++mapsFound
}
console.log(`Found ${mapsFound} maps`)
if (newUnmappable) {
// re-sort the worlds list if a map was inserted into the list for a deleted world
worlds.sort((a, b) => {
return b.mtimeMs - a.mtimeMs
})
}
// part 2 - loop through all the worlds[] and build the body of the main page
for (i = 0; i < worlds.length; i++) {
wrld = worlds[i]
if (wrld.name === excludeMap && !wrld.hasmap && !wrld.mappable) { // if map was deleted (or is being deleted) and has no world, then get ready to remove it
removeindex = i
continue
}
img = wrld.mappable
? '<img src="/thumbnails/' + wrld.name + '.jpg" style="width:320px;" alt="[thumbnail]" />'
: '<img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/No_sign2.svg" style="width:180px;" alt="[no thumbnail]" />'
if ((wrld.hasmap && wrld.publicmap) || isManagementUser) { html += htmlMakeRow(isManagementUser, wrld, img, wrld.hasmap ? istats.mtime : 0) } // make new row in HTML page
++rows
}
if (removeindex > -1) worlds.splice(removeindex, 1) // remove map from list if deleted and has no world
}
if (rows === 0) html += '<p>No maps here yet!</p>'
return html + htmlPageBottom(scrollPosition)
}
/**
* asynchronous deletion of a map folder and its contents
* @param {string} mapName
*/
function deleteMap (mapName) {
const dir = mapsPath + '/' + mapName
fs.rmSync(dir, { recursive: true })
console.log(`DELETED ${dir}`)
}
/**
* Write mapinfo.json file into map directory. An entire worlds object may be passed but all this needs is name, worldname, mapdetail, and publicmap in the object
* @param {*} world
*/
function writeMetafile (world) {
const mpath = `${mapsPath}${path.sep}${world.name}${path.sep}mapinfo.json`
console.log(`writing ${mpath}`)
try {
fs.writeFileSync(mpath, JSON.stringify({
worldname: world.worldname,
mapdetail: world.mapdetail,
publicmap: world.publicmap
}))
} catch (err) {
console.log(err)
}
}
/**
* @param request
* @returns {{}}
*/
function parseCookies (request) {
const list = {}
const cookieHeader = request.headers?.cookie
if (!cookieHeader) return list
cookieHeader.split(';').forEach(function (cookie) {
let [name, ...rest] = cookie.split('=')
name = name?.trim()
if (!name) return
const value = rest.join('=').trim()
if (!value) return
list[name] = decodeURIComponent(value)
})
return list
}