Skip to content
Open

sol #174

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

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

// Lorem2 ipsum dolor sit amet, consectetur adipiscing elit.
function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Welcome to the File Upload Server</h1>');

return;
}

if (req.url !== '/compress') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');

return;
}

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

return;
}

const form = new formidable.IncomingForm();

form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');

return;
}

const file = Array.isArray(files.file) ? files.file[0] : files.file;
const allowedTypes = ['gzip', 'deflate', 'br'];
const compressionType = Array.isArray(fields.compressionType)
? fields.compressionType[0]
: fields.compressionType;

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

return;
}

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

return;
}

let compressor;
let extension;
let contentTypeName;

switch (compressionType) {
case 'gzip':
compressor = zlib.createGzip();
extension = '.gz'; // коротке розширення
contentTypeName = 'gzip'; // для заголовка
break;
case 'deflate':
compressor = zlib.createDeflate();
extension = '.dfl';
contentTypeName = 'deflate';
break;
case 'br':
compressor = zlib.createBrotliCompress();
// eslint-disable-next-line no-unused-vars
extension = '.br';
contentTypeName = 'br';
break;
default:
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Unsupported compression type');

return;
}

const originalFileName = file.originalFilename || 'uploaded_file';
// const compressedFileName = `${originalFileName}${extension}`;

// Передаємо у Content-Disposition повне ім’я алгоритму
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${originalFileName}.${contentTypeName}`,
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 and .dfl for deflate. Your current implementation uses the full compression type name, resulting in incorrect extensions like .gzip and .deflate. You should use the extension variable, which already holds the correct extension string, to construct the filename in the Content-Disposition header. The value should be formatted as attachment; filename=${originalFileName}${extension}.

});

const inputStream = fs.createReadStream(file.filepath);

inputStream.pipe(compressor).pipe(res);
});
});
}

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form
action="http://localhost:5700/compress"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="file" />
<select name="compressionType" id="">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">Submit</button>
</form>
</body>
</html>