Update main.coffee
Browse files- main.coffee +18 -18
main.coffee
CHANGED
|
@@ -15,42 +15,42 @@ isBase64 = (str) ->
|
|
| 15 |
|
| 16 |
app = express()
|
| 17 |
app.set 'json spaces', 4
|
| 18 |
-
|
|
|
|
| 19 |
app.use express.json limit: limitSize
|
| 20 |
app.use express.urlencoded extended: true, limit: limitSize
|
| 21 |
-
|
|
|
|
| 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 |
-
|
|
|
|
| 27 |
app.use '/file', express.static tmpFolder
|
| 28 |
|
| 29 |
app.all '/', (_, res) -> res.send 'POST /upload'
|
| 30 |
|
| 31 |
-
app.
|
| 32 |
-
if req.method isnt 'POST'
|
| 33 |
-
res.json message: 'Method not allowed'
|
| 34 |
-
|
| 35 |
{ file } = req.body
|
| 36 |
-
if not 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
|
| 52 |
,
|
| 53 |
-
type: ftype
|
| 54 |
url: "https://#{process.env.SPACE_HOST}/file/#{fileName}"
|
| 55 |
|
| 56 |
-
app.listen 7860,
|
|
|
|
| 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', 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
|