-
Notifications
You must be signed in to change notification settings - Fork 293
Solution #205
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 #205
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,101 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const zlib = require('zlib'); | ||
| const { pipeline } = require('node:stream'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const formidable = require('formidable'); | ||
|
|
||
| function handleGetIndex(res) { | ||
| const filePath = path.join(__dirname, 'index.html'); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| fs.createReadStream(filePath).pipe(res); | ||
| } | ||
|
|
||
| function handleCompress(req, res) { | ||
| const form = new formidable.IncomingForm(); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid form data'); | ||
|
|
||
| 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.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Missing file or compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressors = { | ||
| gzip: { ext: '.gzip', stream: zlib.createGzip }, | ||
| deflate: { ext: '.deflate', stream: zlib.createDeflate }, | ||
| br: { ext: '.br', stream: zlib.createBrotliCompress }, | ||
| }; | ||
|
|
||
| const config = compressors[compressionType]; | ||
|
|
||
| if (!config) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Unsupported compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/octet-stream', | ||
| 'Content-Disposition': `attachment; filename=${file.originalFilename}${config.ext}`, | ||
|
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 |
||
| }); | ||
|
|
||
| pipeline( | ||
| fs.createReadStream(file.filepath), | ||
| config.stream(), | ||
| res, | ||
| (error) => { | ||
| if (error) { | ||
| if (!res.headersSent) { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('Internal Server Error'); | ||
| } | ||
| res.destroy(); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const { method, url } = req; | ||
|
|
||
| if (method === 'GET' && (url === '/' || url === '/index.html')) { | ||
| return handleGetIndex(res); | ||
| } | ||
|
|
||
| if (method === 'GET' && url === '/compress') { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
|
|
||
| return res.end('GET not allowed'); | ||
| } | ||
|
|
||
| if (method === 'POST' && url === '/compress') { | ||
| return handleCompress(req, res); | ||
| } | ||
|
|
||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not found'); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Compression App</title> | ||
| </head> | ||
| <body> | ||
| <h1>File Compression</h1> | ||
| <form action="/compress" method="POST" enctype="multipart/form-data"> | ||
| <div> | ||
| <label for="file">Select file:</label> | ||
| <input type="file" name="file" id="file" required> | ||
| </div> | ||
| <div> | ||
| <label for="compressionType">Compression type:</label> | ||
| <select name="compressionType" id="compressionType" required> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| </div> | ||
| <button type="submit">Compress</button> | ||
| </form> | ||
| </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
compressorsmapping uses.gzipand.deflateas extensions. The task requires exact extensions.gzfor gzip and.dflfor deflate (keep.brfor brotli). Update theextvalues accordingly so generated filenames match the requirement.