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 #106

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
27 changes: 27 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compression App</title>
</head>
<body>
<form action="./compress" method="post" enctype="multipart/form-data">
<label for="file">Select a file:</label>
<input type="file" name="file" id="file" required>

<br>

<label for="compressionType">Choose a compression type:</label>
<select name="compressionType" id="compressionType" required>
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>

<br>

<input type="submit" value="Compress">
</form>
</body>
</html>
117 changes: 115 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,121 @@
/* 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');

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

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

if (pathname !== '/compress' && pathname !== '/') {
res.statusCode = 404;
res.end('Non-existent endpoint');

return;
}

if (req.method === 'GET' && pathname === '/') {
res.statusCode = 200;
res.setHeader('Content-type', 'text/html');

fs.readFile('./public/index.html', (err, data) => {
if (err) {
res.statusCode = 404;
res.end('Not found');

return;
}

res.end(data);
});
}

if (req.method === 'GET' && pathname === '/compress') {
res.statusCode = 400;
res.end('Not Found');

return;
}

if (req.method === 'POST' && pathname === '/compress') {
const form = new formidable.IncomingForm();

form.parse(req, (err, fields, files) => {
if (err) {
res.statusCode = 400;
res.end('Invalid form data');

return;
}

if (!files.file || !fields.compressionType) {
res.statusCode = 400;

res.end('Missing file or compression type');

return;
}

const file = files.file[0];
const compressionType = fields.compressionType[0];
const fileName = file.originalFilename + '.' + compressionType;

if (!file || !compressionType) {
res.statusCode = 400;

res.end('Missing file or compression type');

return;
}

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

switch (compressionType) {
case 'gzip':
compressedStream = zlib.createGzip();
break;
case 'deflate':
compressedStream = zlib.createDeflate();
break;
case 'br':
compressedStream = zlib.createBrotliCompress();
break;
default:
res.statusCode = 400;
res.end('Unsupported compression type');

return;
}

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

pipeline(fileStream, compressedStream, res, (error) => {
if (error) {
res.statusCode = 500;
res.end('Server error');
}
});

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

server.on('error', (err) => {
console.error('Server error: ', err.message);
});

return server;
}

module.exports = {
Expand Down
Loading