Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
'use strict';

const http = require('http');
const { Readable } = require('stream');
const { getCompression } = require('./getCompression');
const multer = require('multer');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server();

server.on('request', (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const pathName = url.pathname;

if (pathName === '/' && req.method === 'GET') {
res.statusCode = 200;
res.end('Trying send a GET request to / endpoint');

return;
};

if (pathName !== '/compress') {
res.statusCode = 404;
res.end('Trying access a non-existent endpoint');

return;
};

if (pathName === '/compress' && req.method === 'GET') {
res.statusCode = 400;
res.end('Trying send a GET request to /compress endpoint');

return;
};

const download = multer().single('file');

download(req, res, (err) => {
if (err) {
res.statusCode = 400;
res.end('An unsupported compression type is provided');

return;
}

if (!req.file) {
res.statusCode = 400;
res.end('No file is provided');

return;
}

const compressionType = req.body.compressionType;
const compress = getCompression(compressionType);
const buffer = req.file.buffer;

if (!compress) {
res.statusCode = 400;
res.end('No compression type is provided');

return;
}

const fileName = `${req.file.originalname}.${compressionType}`;
const stream = new Readable();

stream.push(buffer);
stream.push(null);

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${fileName}`,
});

stream.pipe(compress).pipe(res);
});
});

return server;
}

module.exports = {
Expand Down
20 changes: 20 additions & 0 deletions src/getCompression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const zlib = require('zlib');

function getCompression(compressionType) {
switch (compressionType) {
case 'gzip':
return zlib.createGzip();
case 'deflate':
return zlib.createDeflate();
case 'br':
return zlib.createBrotliCompress();
default:
return null;
}
};

module.exports = {
getCompression,
};
29 changes: 29 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!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>
<h1>Compression types</h1>

<form action="/compress" method="post">
<label for="index">Enter file name</label>
<input type="text" id="index">

<br>

<label for="select">Select compression type</label>
<select name="compressionType" id="select">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>

<br>

<button type="submit">send</button>
</form>
</body>
</html>
Loading