-
Notifications
You must be signed in to change notification settings - Fork 304
Solution #218
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 #218
Changes from 3 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 |
|---|---|---|
| @@ -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"> | ||
| <option value="gzip">GZIP Compression</option> | ||
| <option value="deflate">DEFLATE Compression</option> | ||
| <option value="br">BR Compression</option> | ||
| </select> | ||
|
|
||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,115 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('node:http'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
| const mime = require('mime-types'); | ||
| const zlib = require('node:zlib'); | ||
| const { formidable } = require('formidable'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const url = new URL(req.url || '', `http://${req.headers.host}`); | ||
| const requestPath = url.pathname.slice(1) || 'index.html'; | ||
| const realPath = requestPath; | ||
|
|
||
| const compressionMap = { | ||
| gzip: { | ||
| 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. According to the task description, the file extension for |
||
| createStream: () => zlib.createGzip(), | ||
| }, | ||
| deflate: { | ||
| 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. The task requires 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 specifies that the file extension for |
||
| createStream: () => zlib.createDeflate(), | ||
| }, | ||
| br: { | ||
| extension: 'br', | ||
| createStream: () => zlib.createBrotliCompress(), | ||
| }, | ||
| }; | ||
|
|
||
| if (url.pathname === '/compress' && req.method === 'POST') { | ||
| const form = formidable({ multiples: false }); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.statusCode = 400; | ||
| res.end('Invalid form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressionType = Array.isArray(fields.compressionType) | ||
| ? fields.compressionType[0] | ||
| : fields.compressionType; | ||
|
|
||
| const file = Array.isArray(files.file) ? files.file[0] : files.file; | ||
|
|
||
| if (!file || !compressionType) { | ||
| res.statusCode = 400; | ||
| res.end('Invalid form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressionConfig = compressionMap[compressionType]; | ||
|
|
||
| if (!compressionConfig) { | ||
| res.statusCode = 400; | ||
| res.end('Unsupported compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const readStream = fs.createReadStream(file.filepath); | ||
| const compressStream = compressionConfig.createStream(); | ||
| const outputFileName = `${file.originalFilename}.${compressionConfig.extension}`; | ||
|
|
||
| res.statusCode = 200; | ||
|
|
||
| res.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${outputFileName}`, | ||
| ); | ||
|
|
||
| readStream.on('error', () => { | ||
| res.statusCode = 500; | ||
| res.end('Server error'); | ||
| }); | ||
|
|
||
| compressStream.on('error', () => { | ||
| res.statusCode = 500; | ||
| res.end('Server error'); | ||
| }); | ||
|
|
||
| readStream.pipe(compressStream).pipe(res); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (url.pathname === '/compress' && req.method === 'GET') { | ||
| res.statusCode = 400; | ||
| res.end('Wrong URL'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (url.pathname === '/' && req.method === 'GET') { | ||
| const fileStream = fs.createReadStream(realPath); | ||
| const mimeType = mime.contentType(path.extname(realPath)) || 'text/plain'; | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', mimeType); | ||
| fileStream.pipe(res); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| 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
gzipcompression should be.gz, not.gzip.