Spaces:
Running
Running
fighter-programmer
commited on
Update main.coffee
Browse files- main.coffee +15 -17
main.coffee
CHANGED
@@ -1,49 +1,47 @@
|
|
1 |
fs = require 'fs'
|
2 |
os = require 'os'
|
3 |
bytes = require 'bytes'
|
|
|
4 |
express = require 'express'
|
5 |
{ fromBuffer } = require 'file-type'
|
|
|
6 |
|
7 |
-
limitSize = '
|
8 |
tmpFolder = os.tmpdir()
|
9 |
|
10 |
-
isBase64 = (str) ->
|
11 |
-
try
|
12 |
-
btoa(atob(str)) is str
|
13 |
-
catch
|
14 |
-
false
|
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.
|
32 |
-
if req.
|
33 |
-
res.json message: '
|
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 =
|
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.
|
46 |
-
|
47 |
res.json
|
48 |
name: fileName,
|
49 |
size:
|
|
|
1 |
fs = require 'fs'
|
2 |
os = require 'os'
|
3 |
bytes = require 'bytes'
|
4 |
+
cors = require 'cors'
|
5 |
express = require 'express'
|
6 |
{ fromBuffer } = require 'file-type'
|
7 |
+
multer = require 'multer'
|
8 |
|
9 |
+
limitSize = '5000mb'
|
10 |
tmpFolder = os.tmpdir()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
app = express()
|
13 |
app.set 'json spaces', 4
|
14 |
+
|
15 |
+
app.use cors()
|
16 |
# limit upload file
|
17 |
app.use express.json limit: limitSize
|
18 |
app.use express.urlencoded extended: true, limit: limitSize
|
19 |
+
|
20 |
+
# multer configuration
|
21 |
+
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:
|