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 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
148 changes: 146 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,152 @@
'use strict';

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

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') {
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) {
res.statusCode = 500;
res.end('Error loading the form');

return;
}

res.setHeader('Content-Type', 'text/html');
res.statusCode = 200;
res.end(data);
});

return;
}

const ext = path.extname(pathname);
const staticFiles = ['.css', '.js', '.png', '.jpg', '.gif'];

if (staticFiles.includes(ext)) {
const filePath = path.join(__dirname, pathname);

fs.readFile(filePath, (err, data) => {
if (err) {
res.statusCode = 404;
res.end('File not found');

return;
}

res.setHeader('Content-Type', getContentType(ext));
res.statusCode = 200;
res.end(data);
});

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, fields, files) => {
if (error || !files.file || !fields.compressionType) {
res.statusCode = 400;
res.end('Bad request');

return;
}

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

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;
}

function getContentType(ext) {
switch (ext) {
case '.css':
return 'text/css';
case '.js':
return 'application/javascript';
case '.png':
return 'image/png';
case '.jpg':
return 'image/jpeg';
case '.gif':
return 'image/gif';
default:
return 'text/plain';
}
}

module.exports = {
Expand Down
26 changes: 26 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./main.css">
</head>
<body>
<form
id="compressionForm"
action="/compress"
method="post"
enctype="multipart/form-data"
>
<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>
15 changes: 15 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
margin: 0;
}

#compressionForm {
display: flex;
flex-direction: column;
gap: 5px;
}
Loading