Spaces:
Runtime error
Runtime error
Upload index.js
Browse files
index.js
CHANGED
|
@@ -27,27 +27,21 @@ app.use(express.json())
|
|
| 27 |
app.use(express.urlencoded({ extended: true }))
|
| 28 |
app.use(favicon(path.join(import.meta.dirname, 'favicon.ico')))
|
| 29 |
app.use(morgan('combined'))
|
| 30 |
-
app.use('/file',
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
fs.readdirSync(tmpDir).forEach(file => {
|
| 39 |
-
const filePath = path.join(tmpDir, file)
|
| 40 |
-
try {
|
| 41 |
-
const stat = fs.statSync(filePath)
|
| 42 |
-
if (stat.isFile() && (Date.now() - stat.mtimeMs > fiveMinutes)) {
|
| 43 |
fs.unlinkSync(filePath)
|
| 44 |
-
console.log(`
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
} catch (err) {
|
| 47 |
-
console.error(`Error cleaning up file ${file}:`, err)
|
| 48 |
}
|
| 49 |
})
|
| 50 |
-
next()
|
| 51 |
})
|
| 52 |
|
| 53 |
app.post('/upload', upload.single('file'), (req, res) => {
|
|
|
|
| 27 |
app.use(express.urlencoded({ extended: true }))
|
| 28 |
app.use(favicon(path.join(import.meta.dirname, 'favicon.ico')))
|
| 29 |
app.use(morgan('combined'))
|
| 30 |
+
app.use('/file', (req, res, next) => {
|
| 31 |
+
const filePath = path.join(tmpDir, req.path.replace(/^\/+/, ''))
|
| 32 |
+
|
| 33 |
+
res.sendFile(filePath, (err) => {
|
| 34 |
+
if (err) {
|
| 35 |
+
next(err)
|
| 36 |
+
} else {
|
| 37 |
+
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
fs.unlinkSync(filePath)
|
| 39 |
+
console.log(`File ${filePath} deleted`)
|
| 40 |
+
} catch (unlinkErr) {
|
| 41 |
+
console.error(`Error deleting file ${filePath}:`, unlinkErr)
|
| 42 |
}
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
})
|
|
|
|
| 45 |
})
|
| 46 |
|
| 47 |
app.post('/upload', upload.single('file'), (req, res) => {
|