-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (100 loc) · 3.17 KB
/
Copy pathserver.js
File metadata and controls
118 lines (100 loc) · 3.17 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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = 3000;
const uploadDir = path.join(__dirname, 'uploads');
const tempDir = path.join(__dirname, 'temp');
[uploadDir, tempDir].forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log(`Created directory: ${dir}`);
}
});
async function deleteIfExists(filename) {
const filePath = path.join(uploadDir, filename);
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(`Deleted existing file: ${filename}`);
}
} catch (err) {
console.error(`Error deleting file ${filename}:`, err);
throw err;
}
}
const upload = multer({
dest: tempDir,
fileFilter: (req, file, cb) => {
if (
file.mimetype === 'text/csv' ||
file.originalname.toLowerCase().endsWith('.csv')
) {
cb(null, true);
} else {
cb(new Error('Only CSV files are allowed'), false);
}
}
});
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(uploadDir));
app.post('/uploads', upload.single('dataFile'), async (req, res) => {
console.log('Upload request received');
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
const uploadType = req.body.uploadType;
let targetFilename;
if (uploadType === 'csvData') {
targetFilename = 'data.csv';
} else if (uploadType === 'csvPitScouting') {
targetFilename = 'pit_scouting.csv';
} else if (uploadType === 'csvSchedule') {
targetFilename = 'schedule.csv';
} else {
fs.unlinkSync(req.file.path);
return res.status(400).json({ error: 'Invalid upload type' });
}
try {
await deleteIfExists(targetFilename);
const newPath = path.join(uploadDir, targetFilename);
fs.copyFileSync(req.file.path, newPath);
fs.unlinkSync(req.file.path);
console.log(`Saved new file as ${targetFilename}`);
return res.status(200).json({
message: 'File uploaded successfully',
filename: targetFilename,
size: req.file.size
});
} catch (error) {
console.error('Upload failed:', error);
if (req.file && fs.existsSync(req.file.path)) {
fs.unlinkSync(req.file.path);
}
return res.status(500).json({ error: 'Upload failed' });
}
});
app.delete('/uploads/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(uploadDir, filename);
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(`Deleted file: ${filename}`);
return res.status(200).json({ message: 'File deleted successfully' });
} else {
return res.status(404).json({ error: 'File not found' });
}
} catch (err) {
console.error(`Error deleting file ${filename}:`, err);
return res.status(500).json({ error: 'Error deleting file' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});