-
Notifications
You must be signed in to change notification settings - Fork 305
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 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,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,106 @@ | ||
| /* 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: () => zlib.createGzip(), | ||
| deflate: () => zlib.createDeflate(), | ||
| br: () => zlib.createBrotliCompress(), | ||
| }; | ||
|
|
||
| if (req.url === '/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 createCompressionStream = compressionMap[compressionType]; | ||
|
|
||
| if (!createCompressionStream) { | ||
| res.statusCode = 400; | ||
| res.end('Unsupported compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const readStream = fs.createReadStream(file.filepath); | ||
| const compressStream = createCompressionStream(); | ||
| const outputFileName = `${file.originalFilename}.${compressionType}`; | ||
|
|
||
| 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 (req.url === '/compress' && req.method === 'GET') { | ||
| res.statusCode = 400; | ||
| res.end('Wrong URL'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.url === '/' && 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); | ||
| } else { | ||
| if (!fs.existsSync(realPath)) { | ||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
| } | ||
| } | ||
|
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 logic for handling 404 errors is not quite right. While it correctly returns a 404 for a path that doesn't exist on the filesystem, it fails to send any response for paths that do exist but aren't explicitly handled (e.g., |
||
| }); | ||
| } | ||
|
|
||
| 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 output filename extension does not match the requirements. The task specifies
.gzforgzipcompression and.dflfordeflate. Currently, the code appends the full compression type name (e.g.,file.txt.gzip), which is incorrect. Consider creating a mapping from the compression type to the correct file extension.