Update index.js
Browse files
index.js
CHANGED
|
@@ -3,12 +3,18 @@ import bytes from 'bytes'
|
|
| 3 |
import express from 'express'
|
| 4 |
import { fileTypeFromBuffer } from 'file-type'
|
| 5 |
|
|
|
|
| 6 |
express()
|
| 7 |
.set('json spaces', 4)
|
| 8 |
.use(express.json({ limit: '500mb' }))
|
| 9 |
.use(express.urlencoded({ extended: true, limit: '500mb' }))
|
| 10 |
.all('/', (_, res) => res.send('POST /upload'))
|
| 11 |
.use('/file', express.static('/tmp'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
.all('/upload', async (req, res) => {
|
| 13 |
if (req.method !== 'POST') return res.json({ message: 'Method not allowed' })
|
| 14 |
|
|
@@ -18,7 +24,7 @@ express()
|
|
| 18 |
|
| 19 |
const fileBuffer = Buffer.from(file, 'base64')
|
| 20 |
const ftype = await fileTypeFromBuffer(fileBuffer) || { mime: 'file', ext: 'bin' }
|
| 21 |
-
const fileName = `${ftype.mime.split('/')[0]}-${Date.now().toString(
|
| 22 |
await fs.promises.writeFile(`/tmp/${fileName}`, fileBuffer)
|
| 23 |
|
| 24 |
res.json({
|
|
@@ -28,7 +34,7 @@ express()
|
|
| 28 |
readable: bytes(+fileBuffer.length, { unitSeparator: ' ' })
|
| 29 |
},
|
| 30 |
type: ftype,
|
| 31 |
-
url: `https://${
|
| 32 |
})
|
| 33 |
})
|
| 34 |
.listen(7860, () => console.log('App running on port', 7860))
|
|
@@ -39,4 +45,4 @@ function isBase64(str) {
|
|
| 39 |
} catch {
|
| 40 |
return false
|
| 41 |
}
|
| 42 |
-
}
|
|
|
|
| 3 |
import express from 'express'
|
| 4 |
import { fileTypeFromBuffer } from 'file-type'
|
| 5 |
|
| 6 |
+
const { SPACE_HOST } = process.env
|
| 7 |
express()
|
| 8 |
.set('json spaces', 4)
|
| 9 |
.use(express.json({ limit: '500mb' }))
|
| 10 |
.use(express.urlencoded({ extended: true, limit: '500mb' }))
|
| 11 |
.all('/', (_, res) => res.send('POST /upload'))
|
| 12 |
.use('/file', express.static('/tmp'))
|
| 13 |
+
.get(['/list', '/files'], (_, res) => {
|
| 14 |
+
const files = fs.readdirSync('/tmp')
|
| 15 |
+
.map(name => `https://${SPACE_HOST}/file/${name}`)
|
| 16 |
+
res.send(files)
|
| 17 |
+
})
|
| 18 |
.all('/upload', async (req, res) => {
|
| 19 |
if (req.method !== 'POST') return res.json({ message: 'Method not allowed' })
|
| 20 |
|
|
|
|
| 24 |
|
| 25 |
const fileBuffer = Buffer.from(file, 'base64')
|
| 26 |
const ftype = await fileTypeFromBuffer(fileBuffer) || { mime: 'file', ext: 'bin' }
|
| 27 |
+
const fileName = `${ftype.mime.split('/')[0]}-${Date.now().toString(16)}.${ftype.ext}`
|
| 28 |
await fs.promises.writeFile(`/tmp/${fileName}`, fileBuffer)
|
| 29 |
|
| 30 |
res.json({
|
|
|
|
| 34 |
readable: bytes(+fileBuffer.length, { unitSeparator: ' ' })
|
| 35 |
},
|
| 36 |
type: ftype,
|
| 37 |
+
url: `https://${SPACE_HOST}/file/${fileName}`
|
| 38 |
})
|
| 39 |
})
|
| 40 |
.listen(7860, () => console.log('App running on port', 7860))
|
|
|
|
| 45 |
} catch {
|
| 46 |
return false
|
| 47 |
}
|
| 48 |
+
}
|