diff --git a/package.json b/package.json index 1d03d64..9f3cce9 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "devDependencies": { "@faker-js/faker": "^8.4.1", "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", @@ -30,5 +30,8 @@ }, "mateAcademy": { "projectType": "javascript" + }, + "dependencies": { + "busboy": "^1.6.0" } } diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..2b9f0d2 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,100 @@ +/* eslint-disable prettier/prettier */ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const Busboy = require('busboy'); +const zlib = require('zlib'); +const { pipeline } = require('stream'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.method === 'GET' && req.url === '/') { + const filePath = path.join(__dirname, 'index.html'); + + fs.readFile(filePath, (error, content) => { + if (error) { + res.statusCode = 500; + + return res.end('Server Error'); + } + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(content); + }); + + return; + } + + if (req.method === 'GET' && req.url === '/compress') { + res.statusCode = 400; + + return res.end('Bad Request: use POST'); + } + + if (req.method === 'POST' && req.url === '/compress') { + const bb = Busboy({ headers: req.headers }); + let filename = ''; + let compressionType = ''; + const supportedTypes = { + gzip: { fn: zlib.createGzip, ext: '.gz' }, + deflate: { fn: zlib.createDeflate, ext: '.dfl' }, + br: { fn: zlib.createBrotliCompress, ext: '.br' }, + }; + + bb.on('field', (name, value) => { + if (name === 'compressionType') { + compressionType = value; + } + }); + + bb.on('file', (name, file, info) => { + filename = info.filename; + + if (name !== 'file') { + res.statusCode = 400; + + return res.end('Bad Request: invalid form field'); + } + + if (!filename) { + res.statusCode = 400; + + return res.end('Bad Request: no File'); + } + + if (!supportedTypes[compressionType]) { + res.statusCode = 400; + + return res.end('Bad Request: unsupported compression type'); + } + + const { fn, ext } = supportedTypes[compressionType]; + const compressedName = filename + ext; + + res.writeHead(200, { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename="${compressedName}"`, + }); + // file.pipe(fn()).pipe(res); + + console.log(`file`, file); + console.log('compressionType', compressionType); + + pipeline(file, fn(), res, (err) => { + if (err) { + res.statusCode = 500; + res.end('Stream error'); + } + }); + }); + req.pipe(bb); + + return; + } + res.statusCode = 404; + res.end('Not Found Data'); + }); } module.exports = { diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..68287f5 --- /dev/null +++ b/src/index.html @@ -0,0 +1,105 @@ + + +
+ + +