-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
538 lines (484 loc) · 14.7 KB
/
server.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
/* eslint-disable no-undef */
import {globby} from 'globby'
import express from 'express'
import cors from 'cors'
import fileUpload from 'express-fileupload'
import fs from 'fs'
import {Server} from 'xeue-webserver'
import {Logs} from 'xeue-logs'
import {Config} from 'xeue-config'
import render from './render.js'
import path from 'path'
import {fileURLToPath} from 'url'
import AdmZip from 'adm-zip'
import commandExists from 'command-exists'
import ejs from 'ejs'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const {version} = require('./package.json')
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const logger = new Logs(false, 'CreditsLogging', __dirname, 'W', false)
const config = new Config(logger)
const webServer = new Server(expressRoutes, logger, version, config)
{ /* Config setup */
logger.printHeader('Credits Generator')
config.default('port', 8080)
config.default('installName', 'Unknown Site')
config.default('debugLineNum', false)
config.default('loggingLevel', 'W')
config.default('createLogFile', true)
config.default('devMode', false)
config.default('allowRender', false)
config.require('port')
config.require('installName', [], 'Name of the site, this appears in the tab in browser')
config.require('loggingLevel', ['A','D','W','E'], 'What level of logs should be recorded (A)ll (D)ebug (W)arning (E)rror')
config.require('createLogFile', [true, false], 'Generate log file')
config.require('allowRender', [true, false], 'Allow FFMPEG based rendering')
if (!await config.fromFile(__dirname + '/config.conf')) {
await config.fromCLI(__dirname + '/config.conf')
}
logger.setConf(
config.get('createLogFile'),
'CreditsLogging',
__dirname,
config.get('loggingLevel'),
config.get('debugLineNum')
)
config.userInput(async (command)=>{
switch (command) {
case 'config':
await config.fromCLI(__dirname + '/config.conf')
logger.setConf(
config.get('createLogFile'),
'CreditsLogging',
__dirname,
config.get('loggingLevel'),
config.get('debugLineNum')
)
return true
}
})
logger.log('Running version: v'+version, ['H', 'SERVER', logger.g])
webServer.start(config.get('port'))
logger.log(`Credits Generator can be accessed at http://localhost:${config.get('port')}`, ['C', 'SERVER', logger.g])
config.print()
}
await folderExists(__dirname+'/public/saves', true)
function expressRoutes(app) {
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
app.use(cors())
app.use(express.json())
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(fileUpload({
createParentPath: true
}))
app.get('/', (req, res) => {
doHome(req, res, false)
})
app.get('/vmix', (req, res) => {
doHome(req, res, true)
})
app.get('/frame', (req, res) => {
doFrame(req, res)
})
app.get('/run', (req, res) => {
logger.log('Requesting run dialog', 'A')
res.header('Content-type', 'text/html')
res.render('run', {})
})
app.get('/save', (req, res) => {
getSave(req, res)
})
app.post('/save', (req, res) => {
doSave(req, res)
})
app.get('/fonts', (req, res) => {
logger.log('Request for fonts', 'D')
const project = req.query.project
sendZip(res, ['public/fonts',`public/saves/${project}/fonts`], project+'_fonts')
})
app.post('/fonts', (req, res) => {
doUpload(req, res, 'fonts')
})
app.delete('/fonts', (req, res) => {
doDelete(req, res, 'fonts')
})
app.post('/images', (req, res) => {
doUpload(req, res, 'images')
})
app.get('/images', (req, res) => {
logger.log('Request for images', 'D')
const project = req.query.project
sendZip(res, [`public/saves/${project}/images`], project+'_images')
})
app.delete('/images', (req, res) => {
doDelete(req, res, 'images')
})
app.get('/template', async (req, res) => {
logger.log('Request for template', 'D')
const [saves, fonts] = await getSavesAndFonts()
const project = typeof req.query.project !== 'undefined' ? req.query.project : Object.keys(saves)[0]
const version = typeof req.query.version !== 'undefined' ? req.query.version : saves[project].count
const buffer = await fs.promises.readFile(`${__dirname}/public/saves/${project}/${version}.json`)
const projectObject = JSON.parse(buffer.toString())
projectObject.images = await imageList(project)
const renderParams = {
globalFonts: fonts,
project: project,
version: version,
projectObject: projectObject,
host: req.get('host')
}
ejs.renderFile(__dirname + '/views/template.ejs', renderParams, async (err, html)=>{
const zip = new AdmZip()
zip.addFile('credits.html', Buffer.from(html, 'utf8'), 'Template')
if (await folderExists(`public/saves/${project}/images`)) {
zip.addLocalFolder(`public/saves/${project}/images`, 'images')
}
if (await folderExists(`public/saves/${project}/fonts`)) {
zip.addLocalFolder(`public/saves/${project}/fonts`, 'fonts')
}
zip.addLocalFolder('public/fonts', 'fonts')
zip.addFile(`${project}_v${version}.json`, await fs.promises.readFile(`public/saves/${project}/${version}.json`),'',0o0644)
zip.addLocalFile('public/css/credits.css','lib')
zip.addLocalFile('public/lib/webcg-framework.umd.js','lib')
zip.addLocalFile('public/lib/webcg-devtools.umd.js','lib')
zip.addLocalFile('public/lib/jquery-3.6.0.js','lib')
zip.addLocalFile('public/js/builder.js','lib')
const zipBuffer = zip.toBuffer()
res.set('Content-disposition', `attachment; filename=${project}_v${version}_template.zip`)
res.send(zipBuffer)
})
})
app.get('/render', (req, res) => {
const project = req.query.project
const version = req.query.version
const fps = parseInt(req.query.fps)
const frames = parseInt(req.query.frames)
logger.log(`Starting render of project: ${project} version: ${version} at frame rate: ${fps}`, 'D')
let width = 1920
let height = 1080
switch (parseInt(req.query.resolution)) {
case 1440:
height = 720
width = 1440
break
case 1920:
height = 1080
width = 1920
break
case 3840:
height = 2160
width = 3840
break
case 4096:
height = 2160
width = 4096
break
default:
break
}
render(`http://localhost:${config.get('port')}`, project, version, fps, frames, width, height, true, logsConfig)
.then(()=>{
setTimeout(()=>{
sendZip(res, [`public/saves/${project}/renders/${version}/`], `${project}_credits`)
},1000)
}).catch((err)=>{
logger.object('Rendering error', err, 'E')
})
})
}
/* Request functions */
async function doHome(req, res, vmix) {
logger.log('Client requesting home page', 'A')
res.header('Content-type', 'text/html')
const [saves, fonts] = await getSavesAndFonts()
let hasFFMPEG = false
try {
await commandExists('ffmpeg')
hasFFMPEG = true
} catch (error) {
try {
await commandExists('FFMPEG')
hasFFMPEG = true
} catch (error) {
logger.log('FFMPEG not installed on this server', 'W')
}
}
res.render('home', {
saves: saves,
globalFonts: fonts,
serverName: config.get('installName'),
project: req.query.project,
render: hasFFMPEG,
allowRender: config.get('allowRender'),
version: version,
vmix: vmix
})
}
async function doFrame(req, res) {
logger.log('New Render Page Spawned', 'A')
res.header('Content-type', 'text/html')
const [saves, fonts] = await getSavesAndFonts()
res.render('render', {
globalFonts: fonts,
fps: req.query.fps,
project: req.query.project,
version: req.query.version,
id: req.query.id
})
}
async function getSave(req, res) {
logger.log('Getting saved credits', 'D')
const [saves, font] = await getSavesAndFonts()
const project = typeof req.query.project !== 'undefined' ? req.query.project : Object.keys(saves)[0]
const version = typeof req.query.version !== 'undefined' ? req.query.version : saves[project].count
try {
const buffer = await fs.promises.readFile(`${__dirname}/public/saves/${project}/${version}.json`)
let data = JSON.parse(buffer.toString())
data.images = await imageList(project)
res.json(data)
} catch (error) {
logger.log(`Cannot load save file: ${project}/${version}.json doesn't exist?`, 'W')
logger.object('Error message', error, 'W')
res.status(500)
res.send(JSON.stringify({
'status': 'error',
'message': 'Save not found',
'error': error
}))
}
}
async function doSave(req, res) {
logger.log('Saving uploaded credits', 'D')
const project = req.body.project
let version = req.body.version
if (project === undefined || version === undefined) {
logger.log(`Failed to get Project (${project}) or Version (${version})`)
res.send({
status: false,
message: 'Invalid data'
})
return
}
try {
if(!req.files) {
res.send({
status: false,
message: 'No file uploaded'
})
} else {
let uploaded = req.files.JSON
const projectDir = `public/saves/${project}`
await folderExists(projectDir, true)
if (version == 'new') {
let files = await fs.promises.readdir(projectDir)
let count = 0
files.forEach(file => {
const current = parseInt(file.substring(0, file.indexOf('.json')))
if (current > count) {
count = current
}
})
count++
version = count
}
uploaded.mv(`${projectDir}/${version}.json`)
res.send({
status: true,
message: {
'type': 'success',
'project': project,
'version': version
},
data: {
name: uploaded.name,
mimetype: uploaded.mimetype,
size: uploaded.size
}
})
}
} catch (err) {
logger.object('File upload error', err, 'E')
res.status(500).send(err)
}
}
async function doUpload(req, res, uploadType) {
logger.log('Saving uploaded Images/Fonts', 'D')
const project = req.body.project
const newProject = req.body.new
const returnObj = {}
if (project === undefined) {
logger.log(`Failed to get Project (${project})`)
res.send({
status: false,
message: 'Invalid data'
})
return
}
try {
if(!req.files) {
res.send({
status: false,
message: 'No file uploaded'
})
} else {
const projectDir = `public/saves/${project}`
const imgDir = uploadType == 'fonts' ? `public/${uploadType}` : `public/saves/${project}/${uploadType}`
await folderExists(imgDir, true)
if (newProject) {
const template = '{}'
await fs.promises.writeFile(`${projectDir}/1.json`, template)
returnObj.new = true
returnObj.project = project
} else {
returnObj.new = false
}
const uploadedItems = req.files['files[]']
if (Array.isArray(uploadedItems)) {
uploadedItems.forEach((item)=>{
item.mv(`${imgDir}/${item.name}`)
})
} else {
uploadedItems.mv(`${imgDir}/${uploadedItems.name}`)
}
returnObj.type = 'success'
returnObj.save = await getUpdatedProjects(project)
res.send(returnObj)
}
} catch (err) {
logger.object('File upload error', err, 'E')
res.status(500).send(err)
}
}
async function doDelete(req, res, deleteType) {
const file = req.query.file
const project = req.query.project
logger.log(`Deleting file: ${file} from: ${project}`, 'A')
const returnObj = {}
try {
await fs.promises.unlink(`public/saves/${project}/${deleteType}/${file}`)
const saves = await getUpdatedProjects()
returnObj.type = 'success'
returnObj.save = saves[project]
res.send(returnObj)
} catch (error) {
const saves = await getUpdatedProjects()
logger.object('File error', error, 'W')
res.status(500)
returnObj.type = 'fail'
returnObj.save = saves[project]
returnObj.error = error
res.send(returnObj)
}
}
async function getUpdatedProjects(project) {
logger.log('Getting new projects list', 'A')
const files = await globby(['public/saves'])
let output = {}
files.forEach(function(path) {
path = path.replace('public/saves/', '')
path.split('/').reduce( function(prev, current) {
if (typeof current == 'string' && current.indexOf('.json') !== -1) {
return
} else if (typeof current == 'string' && (current == 'images' || current == 'fonts' )) {
return prev[current] || (prev[current] = [])
} else if (Object.prototype.toString.call(prev) === '[object Array]') {
prev.push(current)
return
}
return prev[current] || (prev[current] = {})
}, output)
})
return typeof project === 'undefined' ? output : output[project]
}
async function getSavesAndFonts() {
logger.log('Getting updated saves and fonts', 'A')
const [_saves, _fonts] = await Promise.all([
globby(['public/saves']),
globby(['public/fonts'])
])
const saves = {}
_saves.forEach(function(path) {
path = path.replace('public/saves/','')
path.split('/').reduce( function(prev, current) {
if (typeof current == 'string' && current.indexOf('.json') !== -1) {
current = parseInt(current.substring(0, current.indexOf('.json')))
let prevCount = parseInt(prev.count)
let count = prevCount
if (prevCount < current || prev.count == null) {
count = current
}
return prev.count = count
} else if (typeof current == 'string' && (current == 'images' || current == 'fonts')) {
return prev[current] || (prev[current] = [])
} else if (Object.prototype.toString.call(prev) === '[object Array]') {
prev.push(current)
return
}
return prev[current] || (prev[current] = {})
}, saves)
})
const fonts = []
_fonts.forEach((font)=>{
fonts.push(font.substring(13))
})
return [saves, fonts]
}
function imageList(project) {
logger.log('Getting list of images', 'A')
const promise = new Promise((resolve) => {
globby([`public/saves/${project}/images`]).then((files)=>{
let output = {}
files.forEach(function(path) {
path = path.replace(`public/saves/${project}/images/`, '')
path.split('/').reduce( function(prev, current) {
return prev[current] || (prev[current] = {})
}, output)
})
if (typeof output == 'undefined') {
resolve([])
} else {
output = Object.keys(output)
resolve(output)
}
})
})
return promise
}
async function sendZip(res, pathArray, zipName) {
logger.log(`Creating and sending zip: ${zipName}`, 'A')
const zip = new AdmZip()
for (const folder of pathArray) {
if (await folderExists(folder)) {
zip.addLocalFolder(folder)
}
}
const zipBuffer = zip.toBuffer()
res.set('Content-disposition', `attachment; filename=${zipName}.zip`)
res.send(zipBuffer)
}
/* Utility Functions */
async function folderExists(path, makeIfNotPresent = false) {
let found = true
try {
await fs.promises.access(path)
} catch (error) {
found = false
if (makeIfNotPresent) {
logger.log(`Folder: ${logger.y}(${path})${logger.reset} not found, creating it`, 'D')
try {
await fs.promises.mkdir(path, {'recursive': true})
} catch (error) {
logger.log(`Couldn't create folder: ${logger.y}(${path})${logger.reset}`, 'W')
logger.object('Message', error, 'W')
}
} else {
logger.log(`Folder: ${logger.y}(${path})${logger.reset} not found`, 'D')
}
}
return found
}