elkf-backend commited on
Commit
105a31e
·
verified ·
1 Parent(s): 2951158

Upload index.js

Browse files
Files changed (1) hide show
  1. index.js +11 -17
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', express.static(tmpDir, {
31
- setHeaders: (res) => {
32
- res.set('Cache-Control', 'no-store')
33
- }
34
- }))
35
-
36
- app.use((req, res, next) => {
37
- const fiveMinutes = 5 * 60 * 1000
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(`Deleted old file: ${file}`)
 
 
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) => {