-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (91 loc) · 3.43 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
const express = require('express');
const multer = require('multer');
const os = require('os');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3001;
function getLocalIpAddress() {
const interfaces = os.networkInterfaces();
for (const iface of Object.values(interfaces)) {
for (const alias of iface) {
if (alias.family === 'IPv4' && !alias.internal) {
return alias.address;
}
}
}
return '0.0.0.0';
}
// Set up multer middleware for handling file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
const originalname = file.originalname;
const filePath = 'uploads/' + originalname;
fs.access(filePath, fs.constants.F_OK, (err) => {
if (!err) {
// File exists, add a number to the filename
let index = 1;
let newFilename = originalname;
while (true) {
const fileNameParts = originalname.split('.');
const extension = fileNameParts.pop();
const baseName = fileNameParts.join('.');
newFilename = baseName + '-' + index + '.' + extension;
if (!fs.existsSync('uploads/' + newFilename)) {
break;
}
index++;
}
cb(null, newFilename);
} else {
cb(null, originalname);
}
});
}
});
const upload = multer({ storage: storage });
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Serve uploaded files
app.use('/myFiles', express.static('uploads'));
// Route for listing and downloading files
app.get('/myFiles', (req, res) => {
fs.readdir('uploads/', (err, files) => {
if (err) {
console.error('Error reading directory:', err);
res.status(500).send('Error reading directory');
} else {
const basePath = req.protocol + '://' + req.get('host'); // Get base path of the server
const fileList = files.map(file => `<li><a href="${basePath}/myFiles/${file}" download="${file}">${file}</a></li>`).join('');
const htmlResponse = `
<html>
<head>
<title>Lista de Arquivos</title>
<link rel="stylesheet" type="text/css" href="/styles.css">
</head>
<body>
<h1>Lista de Arquivos</h1>
<ul>${fileList}</ul>
</body>
</html>
`;
res.send(htmlResponse);
}
});
});
// Route for the root URL, respond with 'index.html'
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Route for handling file uploads
app.post('/upload', upload.single('fileInput'), (req, res) => {
console.log(req.file); // Log the uploaded file information to the console
res.send('Arquivo recebido!'); // Send response indicating successful upload
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Servidor rodando em http://${getLocalIpAddress()}:${port}`); // Log server address to the console
});