-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e29bd82
commit cad2c5e
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,115 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
const formidable = require('formidable'); | ||
const { pipeline } = require('stream'); | ||
|
||
const PORT = process.env.PORT || '3000'; | ||
|
||
const server = http.Server(); | ||
|
||
server.on('request', (req, res) => { | ||
if (req.url === '/download' && req.method === 'POST') { | ||
const form = new formidable.IncomingForm(); | ||
|
||
form.parse(req, (err, fields, files) => { | ||
if (err) { | ||
res.statusCode = 400; | ||
res.end(JSON.stringify(err)); | ||
|
||
return; | ||
} | ||
|
||
const { file } = files; | ||
const { compression } = fields; | ||
|
||
if (!file || !compression) { | ||
res.statusCode = 400; | ||
res.end('Please select a file and compression type'); | ||
|
||
return; | ||
} | ||
|
||
const readStream = fs.createReadStream(file.filepath); | ||
|
||
let compressionStream; | ||
let extension; | ||
|
||
switch (compression) { | ||
case 'gzip': | ||
extension = '.gzip'; | ||
compressionStream = zlib.createGzip(); | ||
break; | ||
case 'deflate': | ||
extension = '.dfl'; | ||
compressionStream = zlib.createDeflate(); | ||
break; | ||
case 'br': | ||
extension = '.br'; | ||
compressionStream = zlib.createBrotliCompress(); | ||
break; | ||
} | ||
|
||
res.setHeader('Content-Encoding', extension); | ||
|
||
const fileName = file.originalFilename + extension; | ||
|
||
pipeline(readStream, compressionStream, res, (error) => { | ||
if (error) { | ||
console.log(error); | ||
|
||
res.end(JSON.stringify(err)); | ||
} | ||
}); | ||
|
||
res.setHeader('Content-Type', 'application/octet-stream'); | ||
|
||
res.setHeader( | ||
'Content-Disposition', | ||
`attachment; filename=${fileName}` | ||
); | ||
|
||
res.statusCode = 200; | ||
res.end(); | ||
}); | ||
} else if (req.url === '/' && req.method === 'GET') { | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
|
||
res.end(` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<form action="/download" method="POST" enctype="multipart/form-data"> | ||
<label for="file"> | ||
Select file: | ||
<input id="file" type="file" name="file"/> | ||
</label> | ||
<label for="type"> | ||
Select type of compression: | ||
<select name="compression"> | ||
<option value="gzip">Gzip</option> | ||
<option value="deflate">Deflate</option> | ||
<option value="br">Brotli</option> | ||
</select> | ||
</label> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> | ||
`); | ||
} | ||
}); | ||
|
||
server.on('error', () => {}); | ||
|
||
server.listen(PORT, () => { | ||
console.log(`Server running at http://localhost:${PORT}`); | ||
}); |