SaengChu2 commited on
Commit
7f7055d
·
verified ·
1 Parent(s): e2f3e36

Update main.coffee

Browse files
Files changed (1) hide show
  1. main.coffee +61 -24
main.coffee CHANGED
@@ -5,6 +5,7 @@ cors = require 'cors'
5
  express = require 'express'
6
  { fromBuffer } = require 'file-type'
7
  multer = require 'multer'
 
8
 
9
  limitSize = '5000mb'
10
  tmpFolder = os.tmpdir()
@@ -22,33 +23,69 @@ upload = multer({ dest: tmpFolder })
22
 
23
  # logger
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
  # allow user to access file in tmpFolder
30
  app.use '/file', express.static tmpFolder
31
 
32
- app.all '/', (_, res) -> res.send 'POST /upload'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  app.post '/upload', upload.single('file'), (req, res) ->
35
- if not req.file
36
- res.json message: 'No file uploaded'
37
-
38
- fileBuffer = fs.readFileSync req.file.path
39
- ftype = await fromBuffer fileBuffer
40
- if not ftype then ftype = mime: 'file', ext: 'bin'
41
-
42
- randomName = Math.random().toString(36).slice(2)
43
- fileName = "#{ftype.mime.split('/')[0]}-#{randomName}.#{ftype.ext}"
44
- await fs.promises.rename req.file.path, "#{tmpFolder}/#{fileName}"
45
- res.json
46
- name: fileName,
47
- size:
48
- bytes: fileBuffer.length,
49
- readable: bytes fileBuffer.length, unitSeparator: ' '
50
- ,
51
- type: ftype,
52
- url: "https://#{process.env.SPACE_HOST}/file/#{fileName}"
53
-
54
- app.listen 7860, () -> console.log 'App running on port', 7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  express = require 'express'
6
  { fromBuffer } = require 'file-type'
7
  multer = require 'multer'
8
+ archiver = require 'archiver'
9
 
10
  limitSize = '5000mb'
11
  tmpFolder = os.tmpdir()
 
23
 
24
  # logger
25
  app.use (req, res, next) ->
26
+ time = new Date().toLocaleString 'id', timeZone: 'Asia/Jakarta'
27
+ console.log "[#{time}] #{req.method}: #{req.url}"
28
+ next()
29
 
30
  # allow user to access file in tmpFolder
31
  app.use '/file', express.static tmpFolder
32
 
33
+ app.all '/', (_, res) ->
34
+ res.send """
35
+ <!doctype html>
36
+ <html>
37
+ <head>
38
+ <title>File Upload</title>
39
+ </head>
40
+ <body>
41
+ <h1>Upload a file</h1>
42
+ <form action="/upload" method="post" enctype="multipart/form-data">
43
+ <input type="file" name="file" />
44
+ <button type="submit">Upload</button>
45
+ </form>
46
+ </body>
47
+ </html>
48
+ """
49
 
50
  app.post '/upload', upload.single('file'), (req, res) ->
51
+ do ->
52
+ return res.json(message: 'No file uploaded') unless req.file
53
+
54
+ fileBuffer = fs.readFileSync req.file.path
55
+ ftype = await fromBuffer fileBuffer
56
+ ftype = { mime: 'file', ext: 'bin' } unless ftype
57
+
58
+ randomName = Math.random().toString(36).slice(2)
59
+ fileName = "#{ftype.mime.split('/')[0]}-#{randomName}.#{ftype.ext}"
60
+ await fs.promises.rename req.file.path, "#{tmpFolder}/#{fileName}"
61
+ res.json
62
+ name: fileName
63
+ size:
64
+ bytes: fileBuffer.length
65
+ readable: bytes(fileBuffer.length, unitSeparator: ' ')
66
+ type: ftype
67
+ url: "https://#{process.env.SPACE_HOST}/file/#{fileName}"
68
+
69
+ # Endpoint to download a zip file containing files from the tmp folder
70
+ app.get '/download-zip', (req, res) ->
71
+ do ->
72
+ archive = archiver 'zip', zlib: level: 9
73
+
74
+ res.attachment 'files.zip'
75
+
76
+ archive.on 'error', (err) ->
77
+ throw err
78
+
79
+ archive.pipe res
80
+
81
+ # Add files from tmp folder to the zip
82
+ fs.readdir tmpFolder, (err, files) ->
83
+ return res.status(500).send 'Could not list the directory.' if err
84
+
85
+ files.forEach (file) ->
86
+ filePath = "#{tmpFolder}/#{file}"
87
+ archive.file filePath, name: file
88
+
89
+ archive.finalize()
90
+
91
+ app.listen 7860, -> console.log 'App running on port', 7860