ilhamdev commited on
Commit
38c7d40
·
verified ·
1 Parent(s): 8e909a9

Update main.coffee

Browse files
Files changed (1) hide show
  1. main.coffee +28 -29
main.coffee CHANGED
@@ -15,43 +15,42 @@ isBase64 = (str) ->
15
 
16
  app = express()
17
  app.set 'json spaces', 4
18
-
19
- # Limit upload file
20
  app.use express.json limit: limitSize
21
  app.use express.urlencoded extended: true, limit: limitSize
22
-
23
- # Logger middleware
24
  app.use (req, res, next) ->
25
  time = new Date().toLocaleString 'id', timeZone: 'Asia/Jakarta'
26
  console.log "[#{time}] #{req.method}: #{req.url}"
27
  next()
28
-
29
- # Serve static files from tmpFolder
30
  app.use '/file', express.static tmpFolder
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
 
15
 
16
  app = express()
17
  app.set 'json spaces', 4
18
+ # limit upload file
 
19
  app.use express.json limit: limitSize
20
  app.use express.urlencoded extended: true, limit: limitSize
21
+ # logger
 
22
  app.use (req, res, next) ->
23
  time = new Date().toLocaleString 'id', timeZone: 'Asia/Jakarta'
24
  console.log "[#{time}] #{req.method}: #{req.url}"
25
  next()
26
+ # allow user to access file in tmpFolder
 
27
  app.use '/file', express.static tmpFolder
28
 
29
  app.all '/', (_, res) -> res.send 'POST /upload'
30
 
31
+ app.all '/upload', (req, res) ->
32
+ if req.method isnt 'POST'
33
+ res.json message: 'Method not allowed'
34
+
35
+ { file } = req.body
36
+ if not file and typeof file isnt 'string' and not isBase64 file
37
+ res.json message: 'Payload body file must be filled in base64 format'
38
+
39
+ fileBuffer = Buffer.from file, 'base64'
40
+ ftype = await fromBuffer fileBuffer
41
+ if not ftype then ftype = mime: 'file', 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