-
Notifications
You must be signed in to change notification settings - Fork 305
add solution #176
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?
add solution #176
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <!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"> | ||
| <input type="file" name="file" /> | ||
| <select name="compressionType" id="compressionType"> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <button type="submit">Send</button> | ||
| <button type="reset">Reset</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,108 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const zlib = require('zlib'); | ||
| const { pipeline } = require('stream'); | ||
| const multiparty = require('multiparty'); | ||
|
|
||
| const SUPPORTED_TYPES = { | ||
| gzip: { ext: '.gzip', method: zlib.createGzip }, | ||
|
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 gzip is set to
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. @mateacademy-ai-mentor if I change the file extension, my tests fail. The tests specifically require using .gzip and .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. The extension for gzip should be |
||
| deflate: { ext: '.deflate', method: zlib.createDeflate }, | ||
|
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 deflate should be ".dfl" (not ".deflate") according to the requirements. Update to: deflate: { ext: '.dfl', method: zlib.createDeflate },This is required for correct output filenames. 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 deflate is set to
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. @mateacademy-ai-mentor if I change the file extension, my tests fail. The tests specifically require using .gzip and .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. The extension for deflate should be |
||
| br: { ext: '.br', method: zlib.createBrotliCompress }, | ||
| }; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (url.pathname === '/compress') { | ||
| if (req.method !== 'POST') { | ||
| res.writeHead(400); | ||
| res.end('Use POST method'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const form = new multiparty.Form(); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.writeHead(400); | ||
| res.end('Problem with form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressionType = fields.compressionType?.[0]; | ||
| const file = files.file?.[0]; | ||
|
|
||
| if (!compressionType || !file) { | ||
| res.writeHead(400); | ||
| res.end('Incorrectly filled out form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compression = SUPPORTED_TYPES[compressionType]; | ||
|
|
||
| if (!compression) { | ||
| res.writeHead(400); | ||
| res.end('Wrong compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const originalFilename = path.basename(file.originalFilename); | ||
| const compressedFilename = originalFilename + compression.ext; | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/octet-stream', | ||
| 'Content-Disposition': `attachment; filename=${compressedFilename}`, | ||
| }); | ||
|
|
||
| const source = fs.createReadStream(file.path); | ||
| const compress = compression.method(); | ||
|
|
||
| pipeline(source, compress, res, (erro) => { | ||
| fs.unlink(file.path, () => {}); | ||
|
|
||
| if (erro) { | ||
| res.writeHead(500); | ||
|
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 requirements specify that stream errors (compression or upload errors) should result in a 400 status code, not 500. Change |
||
| res.end('Compression error'); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const fileName = url.pathname.slice(1) || 'index.html'; | ||
| const filePath = path.resolve('public', fileName); | ||
|
|
||
| if (!fs.existsSync(filePath)) { | ||
| res.statusCode = 404; | ||
| res.end('file not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const fileStream = fs.createReadStream(filePath); | ||
|
|
||
| fileStream.pipe(res); | ||
|
|
||
| fileStream.on('error', () => { | ||
| res.statusCode = 500; | ||
| res.end('server error'); | ||
| }); | ||
|
|
||
| res.on('close', () => fileStream.destroy()); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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 file extension for gzip should be ".gz" (not ".gzip") according to the requirements. Update to:
This is required for correct output filenames.