-
Notifications
You must be signed in to change notification settings - Fork 274
Solution #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #197
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,128 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const { pipeline, Readable } = require('stream'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| const COMPRESSION_TYPES = ['gzip', 'deflate', 'br']; | ||
|
|
||
| function sendStatus(res, code) { | ||
| if (!res.headersSent) { | ||
| res.writeHead(code); | ||
| res.end(); | ||
| } | ||
| } | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const { url, method, headers } = req; | ||
|
|
||
| if (url === '/' && method === 'GET') { | ||
| const htmlPath = path.join(__dirname, 'index.html'); | ||
| const readStream = fs.createReadStream(htmlPath, 'utf-8'); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
|
|
||
| pipeline(readStream, res, (err) => { | ||
| if (err) { | ||
| sendStatus(res, 500); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (method === 'GET' && url === '/compress') { | ||
| sendStatus(res, 400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (method === 'POST' && url === '/compress') { | ||
| const contentType = headers['content-type']; | ||
|
|
||
| if (!contentType || !contentType.includes('multipart/form-data')) { | ||
| return sendStatus(res, 400); | ||
| } | ||
|
|
||
| const boundary = '--' + contentType.split('boundary=')[1]; | ||
|
|
||
| let body = Buffer.alloc(0); | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body = Buffer.concat([body, chunk]); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| const parts = body.toString('binary').split(boundary); | ||
|
|
||
| let fileBuffer; | ||
| let fileName; | ||
| let compressionType; | ||
|
|
||
| for (const part of parts) { | ||
| if (part.includes('name="compressionType"')) { | ||
| compressionType = part.split('\r\n\r\n')[1]?.trim(); | ||
| } | ||
|
|
||
| if (part.includes('name="file"')) { | ||
| const match = part.match(/filename="(.+?)"/); | ||
|
|
||
| if (match) { | ||
| fileName = match[1]; | ||
|
|
||
| const fileContent = part.split('\r\n\r\n')[1]; | ||
|
|
||
| fileBuffer = Buffer.from( | ||
| fileContent.slice(0, fileContent.lastIndexOf('\r\n')), | ||
| 'binary', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!fileBuffer || !fileName || !compressionType) { | ||
| return sendStatus(res, 400); | ||
| } | ||
|
|
||
| if (!COMPRESSION_TYPES.includes(compressionType)) { | ||
| return sendStatus(res, 400); | ||
| } | ||
|
|
||
| let compressor; | ||
| let extension; | ||
|
|
||
| if (compressionType === 'gzip') { | ||
| compressor = zlib.createGzip(); | ||
| extension = '.gzip'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The requirement specifies the extension for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file extension for |
||
| } | ||
|
|
||
| if (compressionType === 'deflate') { | ||
| compressor = zlib.createDeflate(); | ||
| extension = '.deflate'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the task description, the file extension for deflate compression should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tests gave error with ".dfl" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the task description, the extension for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file extension for |
||
| } | ||
|
|
||
| if (compressionType === 'br') { | ||
| compressor = zlib.createBrotliCompress(); | ||
| extension = '.br'; | ||
| } | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Disposition': `attachment; filename=${fileName}${extension}`, | ||
| 'Content-Type': 'application/octet-stream', | ||
| }); | ||
|
|
||
| pipeline(Readable.from(fileBuffer), compressor, res, () => {}); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <form action="/compress" method="POST" enctype="multipart/form-data"> | ||
| <label for="file">Select file: </label> | ||
| <input type="file" id="file" name="file" required> | ||
|
|
||
| <label for="compressionType">Comresion</label> | ||
| <select name="compressionType" id="compressionType" required> | ||
| <option value="gzip">Gzip</option> | ||
| <option value="deflate">Deflate</option> | ||
| <option value="br">Brotli</option> | ||
| </select> | ||
| <br /> | ||
|
|
||
| <button type="submit">Compress</button> | ||
| </form> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This closing |
||
| </body> | ||
| </html> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The task requirement specifies the file extension for gzip compression should be
.gz.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests gave error with ".gz"