Skip to content
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
97 changes: 95 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,101 @@
'use strict';

const http = require('http');
const zlib = require('zlib');
const { pipeline } = require('node:stream');
const fs = require('fs');
const path = require('path');
const formidable = require('formidable');

function handleGetIndex(res) {
const filePath = path.join(__dirname, 'index.html');

res.writeHead(200, { 'Content-Type': 'text/html' });
fs.createReadStream(filePath).pipe(res);
}

function handleCompress(req, res) {
const form = new formidable.IncomingForm();

form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Invalid form data');

return;
}

const compressionType = Array.isArray(fields.compressionType)
? fields.compressionType[0]
: fields.compressionType;

const file = Array.isArray(files.file) ? files.file[0] : files.file;

if (!file || !compressionType) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Missing file or compression type');

return;
}

const compressors = {
gzip: { ext: '.gzip', stream: zlib.createGzip },
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compressors mapping uses .gzip and .deflate as extensions. The task requires exact extensions .gz for gzip and .dfl for deflate (keep .br for brotli). Update the ext values accordingly so generated filenames match the requirement.

deflate: { ext: '.deflate', stream: zlib.createDeflate },
br: { ext: '.br', stream: zlib.createBrotliCompress },
};

const config = compressors[compressionType];

if (!config) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Unsupported compression type');

return;
}

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${file.originalFilename}${config.ext}`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Content-Disposition header currently builds the filename without quotes and relies on config.ext (which needs fixing above). Quote the filename and use the configured extension, for example: attachment; filename="${file.originalFilename}${config.ext}" so the download filename is exactly file.txt.gz / file.txt.dfl / file.txt.br.

});

pipeline(
fs.createReadStream(file.filepath),
config.stream(),
res,
(error) => {
if (error) {
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
}
res.destroy();
}
},
);
});
}

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const { method, url } = req;

if (method === 'GET' && (url === '/' || url === '/index.html')) {
return handleGetIndex(res);
}

if (method === 'GET' && url === '/compress') {
res.writeHead(400, { 'Content-Type': 'text/plain' });

return res.end('GET not allowed');
}

if (method === 'POST' && url === '/compress') {
return handleCompress(req, res);
}

res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
});
}

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>Compression App</title>
</head>
<body>
<h1>File Compression</h1>
<form action="/compress" method="POST" enctype="multipart/form-data">
<div>
<label for="file">Select file:</label>
<input type="file" name="file" id="file" required>
</div>
<div>
<label for="compressionType">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>
</div>
<button type="submit">Compress</button>
</form>
</body>
</html>