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

Develop #109

Open
wants to merge 2 commits 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
7,825 changes: 7,685 additions & 140 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.7.3",
"@mate-academy/scripts": "^1.8.1",
"axios": "^1.6.7",
"eslint": "^7.32.0",
"eslint-plugin-jest": "^22.4.1",
Expand Down
34 changes: 34 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Compression App</title>
</head>
<body>
<form id="compressionForm" enctype="multipart/form-data" method="post" action="/compress">
<div>
<label for="file">
Select a file:
<input type="file" id="file" name="file" required>
</label>
</div>


<div>
<label for="compressionType">
Select compression type:
<select id="compressionType" name="compressionType" required>
<option value="gzip">GZIP</option>
<option value="deflate">Deflate</option>
<option value="br">Brotli</option>
</select>
</label>
</div>


<div>
<button type="submit">Compress</button>
</div>
</form>
</body>
</html>
10 changes: 10 additions & 0 deletions src/controllers/AbstractController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

class AbstractController {
constructor(request, response) {
this.request = request;
this.response = response;
}
}

module.exports = AbstractController;
73 changes: 73 additions & 0 deletions src/controllers/CompressController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

const fs = require('fs');
const zlib = require('zlib');
const { pipeline } = require('stream');
const formidable = require('formidable');

const AbstractController = require('./AbstractController');

class CompressController extends AbstractController {
index() {
const form = new formidable.IncomingForm();

form.uploadDir = '/tmp';
form.keepExtensions = true;

// eslint-disable-next-line max-len
form.parse(
this.request,
(err, { compressionType: fields }, { file: files }) => {
if (err || !fields || !files) {
this.response.writeHead(400, { 'Content-Type': 'text/plain' });
this.response.end('Error uploading file');

return;
}

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

if (!['gzip', 'deflate', 'br'].includes(compressionType)) {
this.response.statusCode = 400;
this.response.end('Compression type not supported');

return;
}

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

switch (compressionType) {
case 'deflate':
gzipStream = zlib.createDeflate();
break;
case 'br':
gzipStream = zlib.createBrotliCompress();
break;
default:
gzipStream = zlib.createGzip();
}

pipeline(fileStream, gzipStream, this.response, () => {
this.response.statusCode = 500;
this.response.end('Server Error');
});

this.response.on('close', () => {
fileStream.destroy();
});

this.response.on('error', () => fileStream.destroy());
this.response.statusCode = 200;

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

module.exports = CompressController;
23 changes: 23 additions & 0 deletions src/controllers/IndexController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const fs = require('fs');
const AbstractController = require('./AbstractController');

class IndexController extends AbstractController {
index() {
fs.readFile(`./public/index.html`, (err, data) => {
if (!err) {
this.response.statusCode = 200;
this.response.setHeader('Content-Type', 'text/html; charset=utf-8');
this.response.end(data);

return;
}

this.response.statusCode = 404;
this.response.end('File not found');
});
}
}

module.exports = IndexController;
7 changes: 7 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const IndexController = require('./IndexController');
const CompressController = require('./CompressController');

module.exports = {
IndexController,
CompressController,
};
21 changes: 19 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
'use strict';

const http = require('http');

const RouteResolver = require('./router/RouteResolver');
const NotFoundException = require('./exceptions/NotFoundException');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
try {
const routeResolver = new RouteResolver(req, res);

const controller = routeResolver.resolve();

controller.index();
} catch (error) {
if (error instanceof NotFoundException) {
res.statusCode = error.statusCode;
res.end(error.message);
}
}
});
}

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

class NotFoundException extends Error {
constructor(message, statusCode = 404) {
super(message);
this.name = this.constructor.name;
this.statusCode = statusCode;
Error.captureStackTrace(this, this.constructor);
}
}

module.exports = NotFoundException;
48 changes: 48 additions & 0 deletions src/router/RouteResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const NotFoundException = require('../exceptions/NotFoundException');
const { IndexController, CompressController } = require('../controllers');

class RouteResolver {
constructor(request, response) {
this.request = request;
this.response = response;

this.routes = [
{
path: ['/', '/index.html'],
method: 'GET',
controller: IndexController,
code: 404,
},
{
path: ['/compress'],
method: 'POST',
controller: CompressController,
code: 400,
},
];

const protocol = this.request.socket.encrypted ? 'https' : 'http';
const hostName = `${protocol}://${this.request.headers.host}${this.request.url}`;

this.parsedUrl = new URL(hostName);
}
resolve() {
for (const route of this.routes) {
if (route.path.includes(this.parsedUrl.pathname)) {
if (route.method !== this.request.method) {
throw new NotFoundException('Wrong method', route.code);
}

const ControllerName = route.controller;

return new ControllerName(this.request, this.response);
}
}

throw new NotFoundException('Route Not Found');
}
}

module.exports = RouteResolver;
Loading