From ce82dbf107f8eee8746487dd9ea3c8d65ce36f79 Mon Sep 17 00:00:00 2001 From: Andrii IEvych Date: Thu, 29 May 2025 11:50:52 +0300 Subject: [PATCH] add task solution --- src/createServer.js | 135 +++++++++++++++++++++++++++++++++++++++++++- src/index.html | 28 +++++++++ 2 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 src/index.html diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..ed716e8 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,139 @@ 'use strict'; +const http = require('http'); +const zlib = require('zlib'); +const path = require('path'); +const fs = require('fs'); +const { pipeline } = require('stream'); +const formidable = require('formidable'); + +/** + * Creates an HTTP server that handles file compression requests. + * @returns {http.Server} The configured HTTP server. + */ function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = http.createServer((req, res) => { + // Обробляємо запит на головну сторінку + if (req.method === 'GET' && req.url === '/') { + const indexPath = path.join(__dirname, 'index.html'); + + fs.readFile(indexPath, (err, data) => { + if (err) { + res.writeHead(404, 'Not Found', { 'Content-Type': 'text/plain' }); + res.end('404 Not Found: index.html not found'); + + return; + } + res.writeHead(200, 'OK', { 'Content-Type': 'text/html' }); + res.end(data); + }); + + return; + } + + // Обробляємо неіснуючі маршрути (404) + if (req.url !== '/compress') { + res.writeHead(404, 'Not Found', { 'Content-Type': 'text/plain' }); + res.end('404 Not Found'); + + return; + } + + // Обробляємо GET-запити до /compress (400) + if (req.method === 'GET') { + res.writeHead(400, 'Bad Request', { 'Content-Type': 'text/plain' }); + res.end('400 Bad Request: Use POST method to /compress'); + + return; + } + + // Обробляємо POST-запит до /compress + const form = new formidable.IncomingForm({ + multiples: false, + keepExtensions: true, + }); + + form.parse(req, (err, fields, files) => { + if (err) { + res.writeHead(400, 'Bad Request', { 'Content-Type': 'text/plain' }); + res.end('400 Bad Request: Error parsing form'); + + return; + } + + // Перевіряємо валідність форми + const compressionType = Array.isArray(fields.compressionType) + ? fields.compressionType[0] + : fields.compressionType; + const file = Array.isArray(files.file) ? files.file[0] : files.file; + + if (!compressionType || !file) { + res.writeHead(400, 'Bad Request', { 'Content-Type': 'text/plain' }); + + res.end( + '400 Bad Request: Missing required fields (file or compressionType)', + ); + + return; + } + + const supportedTypes = ['gzip', 'deflate', 'br']; + + if (!supportedTypes.includes(compressionType)) { + res.writeHead(400, 'Bad Request', { 'Content-Type': 'text/plain' }); + res.end('400 Bad Request: Unsupported compression type'); + + return; + } + + const originalName = path.basename( + file.originalFilename || 'unknown.txt', + ); + const extension = + compressionType === 'gzip' + ? '.gzip' + : compressionType === 'deflate' + ? '.deflate' + : '.br'; + const compressedFileName = `${originalName}${extension}`; + + // Визначаємо метод стиснення + let compressor; + + if (compressionType === 'gzip') { + compressor = zlib.createGzip(); + } else if (compressionType === 'deflate') { + compressor = zlib.createDeflate(); + } else { + compressor = zlib.createBrotliCompress(); + } + + // Налаштовуємо заголовки для скачування файлу без лапок + res.writeHead(200, 'OK', { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename=${compressedFileName}`, // Видалено лапки + }); + + // Створюємо потоки для читання, стиснення і запису у відповідь + const readStream = fs.createReadStream(file.filepath); + + // eslint-disable-next-line no-shadow + pipeline(readStream, compressor, res, (err) => { + if (err) { + if (!res.writableEnded) { + res.writeHead(500, 'Internal Server Error', { + 'Content-Type': 'text/plain', + }); + res.end('500 Internal Server Error'); + } + } else if (!res.writableEnded) { + res.end(); // Гарантуємо закриття відповіді + } + }); + }); + }); + + return server; } module.exports = { diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..cad72e1 --- /dev/null +++ b/src/index.html @@ -0,0 +1,28 @@ + + + + + + File Compression App + + +

File Compression App

+
+
+ + +
+
+ + +
+
+ +
+
+ +