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

const express = require('express');
const multer = require('multer');
const zlib = require('zlib');
const fs = require('fs');
const http = require('http');

const upload = multer({ dest: 'uploads/' });

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const html = `
<!DOCTYPE html>
<html>
<head>
<title>My Server</title>
</head>
<body>
<form action="/compress" method="post" enctype="multipart/form-data">
<input type="file" name="file">

<select name="compressionType">
<option value="none">No Compression</option>
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 select field should only have options: gzip, deflate, br as specified in requirements. The 'none' option is not required.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This option should be removed. The task specifies only gzip, deflate, and br options are required for the select field.

<option value="gzip">Gzip</option>
<option value="deflate">Deflate</option>
<option value="br">Brotli</option>
</select>

<button type="submit">Submit</button>
</form>
</body>
</html>
`;
const app = express();

app.get('/', (req, res) => {
res.send(html);
});

app.get('/compress', (req, res) => {
res.status(400).send('Bad Request');
});

app.post('/compress', upload.single('file'), (req, res) => {
const file = req.file;
const compressionType = req.body.compressionType;

if (!file) {
return res.status(400).send('No file uploaded.');
}

if (!compressionType) {
return res.status(400).send('No compression type provided.');
}

let transformer;
let newFilename = file.originalname;

switch (compressionType) {
case 'gzip':
transformer = zlib.createGzip();
newFilename += '.gzip';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to requirements, gzip extension should be .gz, not .gzip

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 extension should be .gz not .gzip. Per task requirements, gzip compression should produce files with .gz extension (e.g., file.txt.gz).

break;
case 'deflate':
transformer = zlib.createDeflate();
newFilename += '.deflate';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to requirements, deflate extension should be .dfl, not .deflate

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 extension should be .dfl not .deflate. Per task requirements, deflate compression should produce files with .dfl extension.

break;
case 'br':
transformer = zlib.createBrotliCompress();
newFilename += '.br';
break;
case 'none':
res.setHeader(
'Content-Disposition',
`attachment; filename=${newFilename}`,
);

return fs.createReadStream(file.path).pipe(res);
default:
return res.status(400).send('Invalid compression type.');
}

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

const readStream = fs.createReadStream(file.path);

readStream.pipe(transformer).pipe(res);
});

app.use((req, res) => {
res.status(404).send('404 - Сторінку не знайдено');
});

return http.createServer(app);
}

module.exports = {
Expand Down
Loading