-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatch2.js
More file actions
83 lines (72 loc) · 2.55 KB
/
Copy pathpatch2.js
File metadata and controls
83 lines (72 loc) · 2.55 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
const fs = require('fs');
let content = fs.readFileSync('apps/backend/src/routes/upload.ts', 'utf8');
content = content.replace(
` // Get file info
router.get('/:fileId', (req, res) => {
if (!isSafeFileId(req.params.fileId)) {
return res.status(400).json({ error: 'Invalid file ID' });
}
const filePath = path.join(uploadDir, req.params.fileId);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'File not found' });
}
const stats = fs.statSync(filePath);
res.json({
fileId: req.params.fileId,
filePath,
size: stats.size,
created: stats.birthtime
});
});
// Serve file
router.get('/:fileId/download', (req, res) => {
if (!isSafeFileId(req.params.fileId)) {
return res.status(400).json({ error: 'Invalid file ID' });
}
const filePath = path.join(uploadDir, req.params.fileId);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'File not found' });
}
res.sendFile(filePath);
});`,
` // Get file info
router.get('/:fileId', async (req, res, next) => {
if (!isSafeFileId(req.params.fileId)) {
return res.status(400).json({ error: 'Invalid file ID' });
}
const filePath = path.join(uploadDir, req.params.fileId);
try {
const stats = await fs.promises.stat(filePath);
res.json({
fileId: req.params.fileId,
filePath,
size: stats.size,
created: stats.birthtime
});
} catch (err: any) {
if (err.code === 'ENOENT') {
return res.status(404).json({ error: 'File not found' });
}
next(err);
}
});
// Serve file
router.get('/:fileId/download', async (req, res, next) => {
if (!isSafeFileId(req.params.fileId)) {
return res.status(400).json({ error: 'Invalid file ID' });
}
const filePath = path.join(uploadDir, req.params.fileId);
try {
await fs.promises.access(filePath, fs.constants.F_OK);
res.sendFile(filePath, (err) => {
if (err) next(err);
});
} catch (err: any) {
if (err.code === 'ENOENT') {
return res.status(404).json({ error: 'File not found' });
}
next(err);
}
});`
);
fs.writeFileSync('apps/backend/src/routes/upload.ts', content);