Skip to content
Open

task #222

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
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>File Compression</title>
</head>
<body>
<form action="/compress" method="POST" enctype="multipart/form-data">
<div>
<label for="file">Select a file:</label>
<input type="file" id="file" name="file" />
</div>
<div>
<label for="compressionType">Compression type:</label>
<select id="compressionType" name="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>
103 changes: 101 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,107 @@
'use strict';

const http = require('http');
const fs = require('fs');
const zlib = require('zlib');
const { IncomingForm } = require('formidable');

const SUPPORTED_TYPES = new Set(['gzip', 'deflate', 'br']);

function getFirstValue(value) {
return Array.isArray(value) ? value[0] : value;
}

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

function handleCompressRequest(req, res) {
if (req.method !== 'POST') {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');

return;
}

const form = new IncomingForm();

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

return;
}

const file = getFirstValue(files.file);
const compressionType = getFirstValue(fields.compressionType);
const filename = file?.originalFilename || file?.filename;

if (!file || !compressionType || !filename) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');

return;
}

if (!SUPPORTED_TYPES.has(compressionType)) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');

return;
}

const compressionStream = createCompressionStream(compressionType);

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${filename}.${compressionType}`,
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 task requires the compressed file to have specific extensions: .gz for gzip, .dfl for deflate, and .br for brotli. Currently, the code uses the full compressionType value (gzip, deflate, br) as the extension.

You'll need to map the compressionType to the correct extension as specified in the description.

});

fs.createReadStream(file.filepath)
.on('error', () => {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
})
.pipe(compressionStream)
.on('error', () => {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
})
.pipe(res);
});
}

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
if (req.url === '/') {
const stream = fs.createReadStream('public/index.html');

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

stream.on('error', () => {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error reading file');
});
} else if (req.url === '/compress') {
handleCompressRequest(req, res);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
});

return server;
}

module.exports = {
Expand Down
Loading