-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
126 lines (111 loc) · 4.02 KB
/
server.mjs
File metadata and controls
126 lines (111 loc) · 4.02 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
import express from 'express';
const app = express();
app.use(express.static('static'));
app.listen(3000, () => console.log('Sound Board running on http://localhost:3000'));
const VALID_REGEX = /^[a-zA-ZöäüÖÄÜß0-9\s_-]+$/; // Regex Check aus dem Frontend
function setResponseAndStatus(res, data, status) {
res.type('application/json');
res.status(status);
res.send(JSON.stringify(data));
}
function validateRequest(data, requiredFields = ['projectName', 'fileName', 'date', 'importance', 'audioBuffer']) {
for (const field of requiredFields) {
if (!data.hasOwnProperty(field) || data[field] === '') {
return `Property '${field}' is required and mustn't be empty!`;
}
}
if (
data.hasOwnProperty('projectName') &&
(!VALID_REGEX.test(data.projectName) || data.projectName.length < 3 || data.projectName.length > 25)
)
return 'Project name contains invalid characters! Only letters, numbers, spaces, underscores and hyphens allowed. And the project name should contain between 3 and 25 characters!';
if (
data.hasOwnProperty('fileName') &&
(!VALID_REGEX.test(data.fileName) || data.fileName.length < 3 || data.fileName.length > 25)
)
return 'File name contains invalid characters! Only letters, numbers, spaces, underscores and hyphens allowed. And the file name should contain between 3 and 25 characters!';
if (
data.hasOwnProperty('importance') &&
(!Number.isInteger(data.importance) || data.importance < 1 || data.importance > 5)
) {
return 'Importance must be an integer value between 1 and 5';
}
if (data.hasOwnProperty('date') && (isNaN(Date.parse(data.date)) || new Date(data.date) > Date.now()))
return 'The date property is invald!';
if (data.hasOwnProperty('audioBuffer') && !/^[A-Za-z0-9+/]*={0,2}$/.test(data.audioBuffer))
return 'Invalid audio data';
return null;
}
app.post('/save', async function (req, res) {
try {
const body = JSON.parse(req.body.toString('utf-8'));
const error = validateRequest(body);
if (error != null) {
setResponseAndStatus(res, {error: error}, 400);
return;
}
const sound = {
projectName: body.projectName,
fileName: body.fileName,
date: body.date,
importance: body.importance,
location: body.location,
audioBuffer: body.audioBuffer
};
// TODO -> IndexedDB
const collection = db.collection('sounds');
await collection.insertOne(sound);
res.cookie('lastUsedProject', body.projectName);
const result = {
message: 'Sound saved successfully',
fileName: body.fileName,
projectName: body.projectName,
timestamp: new Date().toISOString()
};
res.send(JSON.stringify(result));
} catch (error) {
console.error('Error saving sound:', error);
res.status(500);
res.type('application/json');
res.send(
JSON.stringify({
error: 'Server error while saving sound',
message: error.message
})
);
}
});
app.get('/project-names', async function (req, res) {
try {
// TODO -> IndexedDB
const projects = await db.collection('sounds').distinct('projectName');
const lastUsedProject = req.cookies.lastUsedProject || null;
res.json({
projects: projects.filter(p => p),
lastUsedProject: lastUsedProject || null
});
} catch (error) {
console.error('Database error:', error);
res.status(500).json({error: 'Server error'});
}
});
app.get('/projects', async function (req, res) {
try {
// TODO -> IndexedDB
const projectName = req.query.projectName?.toString().trim();
if (projectName && projectName.length > 30) return res.status(400).json({error: 'Project name is too long!'});
const sounds = await db
.collection('sounds')
.find(projectName ? {projectName} : {})
.toArray();
res.json({
selectedProject: projectName || 'all',
sounds: sounds,
numSounds: sounds.length
});
} catch (error) {
console.error('Database error:', error);
res.status(500).json({error: 'Server error'});
}
});
export default app;