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

add task solution #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 96 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,102 @@
'use strict';

const http = require('http');
const zlib = require('zlib');
const formidable = require('formidable');
const fs = require('fs');

const compressionTypes = ['gzip', 'deflate', 'br'];

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

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

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

if (pathname === '/' && req.method === 'GET') {
res.statusCode = 200;
res.end('Ready');

return;
}

if (pathname !== '/compress') {
res.statusCode = 404;
res.end('Trying to access a non-existing route');

return;
}

if (req.method !== 'POST') {
res.statusCode = 400;
res.end('Use POST request method instead');

return;
}

form.parse(req, (error, { compressionType: fields }, { file: files }) => {
if (error || !files || !fields) {
res.statusCode = 400;
res.end('Bad request');

return;
}

const [compressionType] = fields;
const [file] = files;

if (!compressionTypes.includes(compressionType)) {
res.statusCode = 400;
res.end('Compression type not supported');

return;
}

const compressedFile = handleCompressFile(compressionType);

res.setHeader(
'Content-Disposition',
`attachment; filename=${file.originalFilename}.${compressionType}`,
);

const fileStream = fs.createReadStream(file.filepath);

fileStream
.on('error', (err) => {
res.statusCode = 500;
res.end('Failed to read file', err);
})
.pipe(compressedFile)
.on('error', (err) => {
res.statusCode = 500;
res.end('Failed to compress file', err);
})
.pipe(res)
.on('error', (err) => {
// eslint-disable-next-line no-console
console.error('Error sending response:', err);
});

res.on('close', () => fileStream.destroy());
});
});

return server;
}

module.exports = {
Expand Down
20 changes: 20 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!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="/compress" method="post">
<input type="file" name="file">
<select name="compressionType" id="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>

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