From dd3f5fe7aadb2e3bc5e7dd397205c9039cf75f91 Mon Sep 17 00:00:00 2001 From: Alla Serhiienko Date: Sat, 6 Jul 2024 02:04:28 +0200 Subject: [PATCH] Solution --- public/index.html | 27 ++++++++++ src/createServer.js | 117 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 public/index.html diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..27f4a78 --- /dev/null +++ b/public/index.html @@ -0,0 +1,27 @@ + + + + + + Compression App + + +
+ + + +
+ + + + +
+ + +
+ + diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..ab4a84b 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,121 @@ +/* eslint-disable no-console */ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const zlib = require('zlib'); +const formidable = require('formidable'); +const { pipeline } = require('stream'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = new http.Server(); + + server.on('request', (req, res) => { + const { pathname } = new URL(req.url || '', `http://${req.headers.host}`); + + if (pathname !== '/compress' && pathname !== '/') { + res.statusCode = 404; + res.end('Non-existent endpoint'); + + return; + } + + if (req.method === 'GET' && pathname === '/') { + res.statusCode = 200; + res.setHeader('Content-type', 'text/html'); + + fs.readFile('./public/index.html', (err, data) => { + if (err) { + res.statusCode = 404; + res.end('Not found'); + + return; + } + + res.end(data); + }); + } + + if (req.method === 'GET' && pathname === '/compress') { + res.statusCode = 400; + res.end('Not Found'); + + return; + } + + if (req.method === 'POST' && pathname === '/compress') { + const form = new formidable.IncomingForm(); + + form.parse(req, (err, fields, files) => { + if (err) { + res.statusCode = 400; + res.end('Invalid form data'); + + return; + } + + if (!files.file || !fields.compressionType) { + res.statusCode = 400; + + res.end('Missing file or compression type'); + + return; + } + + const file = files.file[0]; + const compressionType = fields.compressionType[0]; + const fileName = file.originalFilename + '.' + compressionType; + + if (!file || !compressionType) { + res.statusCode = 400; + + res.end('Missing file or compression type'); + + return; + } + + const fileStream = fs.createReadStream(file.filepath); + let compressedStream; + + switch (compressionType) { + case 'gzip': + compressedStream = zlib.createGzip(); + break; + case 'deflate': + compressedStream = zlib.createDeflate(); + break; + case 'br': + compressedStream = zlib.createBrotliCompress(); + break; + default: + res.statusCode = 400; + res.end('Unsupported compression type'); + + return; + } + + res.setHeader( + 'Content-Disposition', + `attachment; filename=${fileName}`, + ); + + pipeline(fileStream, compressedStream, res, (error) => { + if (error) { + res.statusCode = 500; + res.end('Server error'); + } + }); + + res.on('close', () => fileStream.destroy()); + }); + } + }); + + server.on('error', (err) => { + console.error('Server error: ', err.message); + }); + + return server; } module.exports = {