-
Notifications
You must be signed in to change notification settings - Fork 200
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
add solution #140
base: master
Are you sure you want to change the base?
add solution #140
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 |
---|---|---|
@@ -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 |
---|---|---|
@@ -1,8 +1,87 @@ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
const formidable = require('formidable'); | ||
|
||
function createServer() { | ||
/* Write your code here */ | ||
// Return instance of http.Server class | ||
const server = new http.Server(); | ||
|
||
server.on('request', async (req, res) => { | ||
const { pathname } = new URL(req.url, `http://${req.headers.host}`); | ||
|
||
if (req.method === 'POST' && pathname === '/compress') { | ||
const form = new formidable.IncomingForm(); | ||
|
||
form.parse(req, (error, { compressionType }, { file }) => { | ||
if (error || !compressionType || !file) { | ||
res.statusCode = 400; | ||
res.end('form parsing error'); | ||
|
||
return; | ||
} | ||
|
||
if (!['gzip', 'deflate', 'br'].includes(compressionType[0])) { | ||
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 check for |
||
res.statusCode = 400; | ||
res.end('Unknown compression type'); | ||
|
||
return; | ||
} | ||
|
||
let compressor; | ||
|
||
switch (compressionType[0]) { | ||
case 'gzip': | ||
compressor = zlib.createGzip(); | ||
break; | ||
case 'deflate': | ||
compressor = zlib.createDeflate(); | ||
break; | ||
case 'br': | ||
compressor = zlib.createBrotliCompress(); | ||
break; | ||
default: | ||
compressor = null; | ||
} | ||
|
||
res.setHeader( | ||
'Content-Disposition', | ||
`attachment; filename=${file[0].originalFilename}.${compressionType[0]}`, | ||
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 |
||
); | ||
|
||
const fileStream = fs.createReadStream(file[0].filepath); | ||
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 |
||
|
||
fileStream | ||
.on('error', (err) => { | ||
res.statusCode = 500; | ||
res.end(String(err)); | ||
}) | ||
.pipe(compressor) | ||
.on('error', (err) => { | ||
res.statusCode = 500; | ||
res.end(String(err)); | ||
}) | ||
.pipe(res) | ||
.on('error', (err) => { | ||
res.end(String(err)); | ||
}); | ||
|
||
res.on('close', () => fileStream.destroy()); | ||
}); | ||
} else if (req.method === 'GET' && pathname === '/compress') { | ||
res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
res.end('GET method not supported for /compress endpoint'); | ||
} else if (req.method === 'GET' && pathname === '/') { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.end('root endpoint reached'); | ||
} else { | ||
res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
res.end('Not Found'); | ||
} | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!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> | ||
<form | ||
id="compressionForm" | ||
enctype="multipart/form-data" | ||
method="post" | ||
action="/compress" | ||
> | ||
<div> | ||
<label for="file">Select a file:</label> | ||
<input type="file" id="file" name="file" required /> | ||
</div> | ||
|
||
<div> | ||
<label for="compressionType">Choose compression type:</label> | ||
<select id="compressionType" name="compressionType" required> | ||
<option value="gzip">GZIP</option> | ||
<option value="deflate">Deflate</option> | ||
<option value="br">Brotli</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
compressionType
andfile
are accessed as arrays (e.g.,compressionType[0]
andfile[0]
). Ensure that these variables are indeed arrays. If they are not, you should access them directly without indexing.