-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (100 loc) · 3.08 KB
/
index.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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const app = express();
const os = require('os');
// definining colors
FgBlue = "\x1b[34m"
Reset = "\x1b[0m"
Bright = "\x1b[1m"
FgGreen = "\x1b[32m"
const storage = multer.diskStorage({
destination: 'uploads/',
filename: (req, file, cb) => {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
app.use(express.static('public'));
app.post('/upload', upload.single('file'), (req, res) => {
res.redirect('/');
});
app.get('/files', (req, res) => {
fs.readdir('uploads', (err, files) => {
if (err) {
res.status(500).send('Error reading files');
} else {
console.log(files)
res.json(files);
}
});
});
// Route zum Herunterladen von Dateien
app.get('/download/:filename', (req, res) => {
const filename = req.params.filename;
const file = path.join(__dirname, 'uploads', filename);
res.download(file);
});
// Route zum Umbenennen von Dateien
app.post('/rename', express.json(), (req, res) => {
const oldName = path.join(__dirname, 'uploads', req.body.oldName);
const newName = path.join(__dirname, 'uploads', req.body.newName);
fs.rename(oldName, newName, (err) => {
if (err) {
res.status(500).send("Fehler beim Umbenennen der Datei");
} else {
res.send("Datei erfolgreich umbenannt");
}
});
});
app.post('/create-folder', express.json(), (req, res) => {
const folderName = path.join(__dirname, 'uploads', req.body.folderName);
fs.mkdir(folderName, (err) => {
if (err) {
res.status(500).send("Fehler beim Erstellen des Ordners");
} else {
res.send("Ordner erfolgreich erstellt");
}
});
});
app.get('/list', (req, res) => {
fs.readdir('./uploads', { withFileTypes: true }, (err, items) => {
if (err) {
res.status(500).send("Fehler beim Abrufen der Dateien");
} else {
res.json(items.map(item => ({ name: item.name, isDirectory: item.isDirectory() })));
}
});
});
app.delete('/delete/:filename', (req, res) => {
const filename = req.params.filename;
const file = path.join(__dirname, 'uploads', filename);
fs.unlink(file, (err) => {
if (err) {
res.status(500).send("Fehler beim Löschen der Datei");
} else {
res.send("Datei erfolgreich gelöscht");
}
});
});
app.get('/download/:filename', (req, res) => {
res.download(path.join(__dirname, 'uploads', req.params.filename));
});
app.listen(80, () => {
const networkInterfaces = os.networkInterfaces();
const addresses = [];
for (const interfaceName in networkInterfaces) {
const interfaces = networkInterfaces[interfaceName];
for (const iface of interfaces) {
if (iface.family === 'IPv4' && !iface.internal) {
addresses.push(iface.address);
}
}
}
console.clear()
console.log(FgBlue+Bright+"Server running on port 80 ("+Reset+FgGreen+Bright+"http://localhost:80"+Reset+FgBlue+Bright+")")
addresses.forEach((address) => {
console.log(FgBlue+Bright+"Host-IP Address: "+Reset+FgGreen+Bright+address+Reset);
});
});