Update main.coffee
Browse files- main.coffee +24 -23
main.coffee
CHANGED
|
@@ -31,26 +31,27 @@ app.use '/file', express.static tmpFolder
|
|
| 31 |
|
| 32 |
app.all '/', (_, res) -> res.send 'POST /upload'
|
| 33 |
|
| 34 |
-
app.post '/upload',
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
| 31 |
|
| 32 |
app.all '/', (_, res) -> res.send 'POST /upload'
|
| 33 |
|
| 34 |
+
app.post '/upload', (req, res) ->
|
| 35 |
+
do async ->
|
| 36 |
+
{ file } = req.body
|
| 37 |
+
if not file or typeof file isnt 'string' or not isBase64(file)
|
| 38 |
+
res.status(400).json message: 'Payload body file must be filled in base64 format'
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
fileBuffer = Buffer.from(file, 'base64')
|
| 42 |
+
ftype = await fromBuffer(fileBuffer) or { mime: 'application/octet-stream', ext: 'bin' }
|
| 43 |
+
|
| 44 |
+
randomName = Math.random().toString(36).slice(2)
|
| 45 |
+
fileName = "#{ftype.mime.split('/')[0]}-#{randomName}.#{ftype.ext}"
|
| 46 |
+
await fs.promises.writeFile "#{tmpFolder}/#{fileName}", fileBuffer
|
| 47 |
+
|
| 48 |
+
res.json
|
| 49 |
+
name: fileName,
|
| 50 |
+
size:
|
| 51 |
+
bytes: fileBuffer.length
|
| 52 |
+
readable: bytes(fileBuffer.length, unitSeparator: ' ')
|
| 53 |
+
,
|
| 54 |
+
type: ftype
|
| 55 |
+
url: "https://#{process.env.SPACE_HOST}/file/#{fileName}"
|
| 56 |
+
|
| 57 |
+
app.listen 7860, -> console.log 'App running on port', 7860
|