diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..10e5013 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -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('

Welcome to the File Upload Server

'); + + 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}`, + }); + + const inputStream = fs.createReadStream(file.filepath); + + inputStream.pipe(compressor).pipe(res); + }); + }); } module.exports = { diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..437eee5 --- /dev/null +++ b/src/index.html @@ -0,0 +1,23 @@ + + + + + + Document + + +
+ + + +
+ +