ilhamdev commited on
Commit
8e909a9
·
verified ·
1 Parent(s): 956091d

Update main.coffee

Browse files
Files changed (1) hide show
  1. 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', async (req, res) ->
35
- { file } = req.body
36
- if not file or typeof file isnt 'string' or not isBase64(file)
37
- res.status(400).json message: 'Payload body file must be filled in base64 format'
38
- return
39
-
40
- fileBuffer = Buffer.from(file, 'base64')
41
- ftype = await fromBuffer(fileBuffer) or { mime: 'application/octet-stream', ext: 'bin' }
42
-
43
- randomName = Math.random().toString(36).slice(2)
44
- fileName = "#{ftype.mime.split('/')[0]}-#{randomName}.#{ftype.ext}"
45
- await fs.promises.writeFile "#{tmpFolder}/#{fileName}", fileBuffer
46
-
47
- res.json
48
- name: fileName,
49
- size:
50
- bytes: fileBuffer.length
51
- readable: bytes(fileBuffer.length, unitSeparator: ' ')
52
- ,
53
- type: ftype
54
- url: "https://#{process.env.SPACE_HOST}/file/#{fileName}"
55
-
56
- app.listen 7860, -> console.log 'App running on port', 7860
 
 
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