From 66c1baa5e5fcd52f84770f49cf112782d5f1c491 Mon Sep 17 00:00:00 2001 From: Vlad Buldenko <98160877+vbuldenko@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:11:14 +0300 Subject: [PATCH] Update 1 --- src/1 | 60 ----------------------------------------------------------- 1 file changed, 60 deletions(-) diff --git a/src/1 b/src/1 index dfc03f9..8b13789 100644 --- a/src/1 +++ b/src/1 @@ -1,61 +1 @@ -if (req.method === 'POST' && req.url === '/compress') { - const form = new formidable.IncomingForm(); - form.parse(req, (err, fields, files) => { - if (err) { - res.writeHead(400, { 'Content-Type': 'text/plain' }); - res.end('Error parsing the form'); - - return; - } - - const file = files.file; - const compressionType = fields.compressionType; - - if (!file || !compressionType) { - res.writeHead(400, { 'Content-Type': 'text/plain' }); - res.end('Form is invalid'); - - return; - } - - let compressor; - let extension; - - switch (compressionType) { - case 'gzip': - compressor = zlib.createGzip(); - extension = '.gz'; - break; - case 'deflate': - compressor = zlib.createDeflate(); - extension = '.dfl'; - break; - case 'br': - compressor = zlib.createBrotliCompress(); - extension = '.br'; - break; - default: - res.writeHead(400, { 'Content-Type': 'text/plain' }); - res.end('Unsupported compression type'); - - return; - } - - const inputFile = fs.createReadStream(file.filepath); - const outputFileName = file.originalFilename + extension; - - res.writeHead(200, { - 'Content-Disposition': `attachment; filename="${outputFileName}"`, - 'Content-Type': 'application/octet-stream', - }); - - inputFile.pipe(compressor).pipe(res); - }); -} else if (req.method === 'GET' && req.url === '/compress') { - res.writeHead(400, { 'Content-Type': 'text/plain' }); - res.end('GET method not supported for /compress endpoint'); -} else { - res.writeHead(404, { 'Content-Type': 'text/plain' }); - res.end('Not Found'); -}