-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (114 loc) · 3.32 KB
/
server.js
File metadata and controls
140 lines (114 loc) · 3.32 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require('express');
const multer = require('multer');
const cors = require('cors');
const path = require('path');
const fs = require('fs-extra');
const app = express();
const PORT = process.env.PORT || 9071;
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
const uploadsDir = path.join(__dirname, 'uploads');
fs.ensureDirSync(uploadsDir);
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadsDir);
},
filename: (req, file, cb) => {
const timestamp = Date.now();
const sanitizedName = file.originalname.replace(/[^a-zA-Z0-9.-]/g, '_');
cb(null, `${timestamp}-${sanitizedName}`);
}
});
const upload = multer({
storage: storage,
limits: {
fileSize: 50 * 1024 * 1024
}
});
const fileTracker = new Map();
app.post('/api/upload', upload.single('file'), (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
const fileId = req.file.filename;
const fileInfo = {
id: fileId,
originalName: req.file.originalname,
filename: req.file.filename,
size: req.file.size,
uploadedAt: new Date().toISOString(),
status: 'uploaded'
};
fileTracker.set(fileId, fileInfo);
res.json({
success: true,
file: fileInfo
});
} catch (error) {
console.error('Upload error:', error);
res.status(500).json({ error: 'Upload failed' });
}
});
app.post('/api/shred/:fileId', async (req, res) => {
try {
const { fileId } = req.params;
const fileInfo = fileTracker.get(fileId);
if (!fileInfo) {
return res.status(404).json({ error: 'File not found' });
}
fileInfo.status = 'shredding';
fileTracker.set(fileId, fileInfo);
const shreddingTime = Math.floor(Math.random() * 4000) + 2000;
res.json({
success: true,
shreddingTime: shreddingTime,
message: 'Shredding started'
});
setTimeout(async () => {
try {
const filePath = path.join(uploadsDir, fileInfo.filename);
if (await fs.pathExists(filePath)) {
await fs.remove(filePath);
console.log(`File shredded: ${fileInfo.filename}`);
}
fileInfo.status = 'shredded';
fileInfo.shreddedAt = new Date().toISOString();
fileTracker.set(fileId, fileInfo);
} catch (error) {
console.error('Error shredding file:', error);
fileInfo.status = 'error';
fileTracker.set(fileId, fileInfo);
}
}, shreddingTime);
} catch (error) {
console.error('Shred error:', error);
res.status(500).json({ error: 'Shredding failed' });
}
});
app.get('/api/status/:fileId', (req, res) => {
const { fileId } = req.params;
const fileInfo = fileTracker.get(fileId);
if (!fileInfo) {
return res.status(404).json({ error: 'File not found' });
}
res.json({
success: true,
file: fileInfo
});
});
app.get('/api/files', (req, res) => {
const files = Array.from(fileTracker.values());
res.json({
success: true,
files: files
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`File Shredder Server running on port ${PORT}`);
console.log(`Access the app at http://localhost:${PORT}`);
});