-
Notifications
You must be signed in to change notification settings - Fork 306
Implement compression app solution #175
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?
Changes from 2 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,103 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const { IncomingForm } = require('formidable'); | ||
| const zlib = require('zlib'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'POST' && req.url === '/compress') { | ||
| const form = new IncomingForm({ multiples: false }); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Parsing form error'); | ||
| } | ||
|
|
||
| const compressionType = Array.isArray(fields.compressionType) | ||
| ? fields.compressionType[0] | ||
| : fields.compressionType; | ||
| const file = files.file; | ||
|
|
||
| if (!file) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing file'); | ||
| } | ||
|
|
||
| if (!compressionType) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing compression type'); | ||
| } | ||
|
|
||
| const originalFileName = file[0].originalFilename; | ||
| const filePath = file[0].filepath.replace(/\\/g, '/'); | ||
|
|
||
| let compressor; | ||
| let ext; | ||
|
|
||
| switch (compressionType) { | ||
| case 'gzip': | ||
| compressor = zlib.createGzip(); | ||
| ext = '.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. According to the task description, the extension for 'gzip' compression should be '.gz', not '.gzip'. |
||
| break; | ||
|
|
||
| case 'deflate': | ||
| compressor = zlib.createDeflate(); | ||
| ext = '.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 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 task description requires the file extension for 'deflate' compression to be '.dfl', not '.deflate'. |
||
| break; | ||
|
Comment on lines
+43
to
+52
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 compression type values submitted by the form are 'gzip' and 'deflate', but the |
||
|
|
||
| case 'br': | ||
| compressor = zlib.createBrotliCompress(); | ||
| ext = '.br'; | ||
| break; | ||
|
|
||
| default: | ||
| res.statusCode = 400; | ||
| res.end('Unsupported compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressedFileName = originalFileName + ext; | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/octet-stream'); | ||
|
|
||
| res.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${compressedFileName}`, | ||
| ); | ||
|
|
||
| const readStream = fs.createReadStream(filePath); | ||
|
|
||
| readStream.pipe(compressor).pipe(res); | ||
| }); | ||
| } else if (req.method === 'GET' && req.url === '/') { | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-type', 'text/html'); | ||
|
|
||
| res.end(`<form method="POST" action="/compress" enctype="multipart/form-data"> | ||
| <input name="file" type="file"> | ||
| <select name="compressionType"> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <button type="submit">Submit</button> | ||
| </form>`); | ||
| } else if (req.method === 'GET' && req.url === '/compress') { | ||
| res.statusCode = 400; | ||
| res.end('GET not allowed'); | ||
| } else { | ||
| res.statusCode = 404; | ||
| res.end('Not found'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| 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.
According to the task description, the file extension for
gzipshould be.gz, not.gzip.