-
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
Develop #135
base: master
Are you sure you want to change the base?
Develop #135
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,140 @@ | ||
'use strict'; | ||
|
||
const http = require('node:http'); | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const zlib = require('node:zlib'); | ||
const formidable = require('formidable'); | ||
|
||
function createServer() { | ||
/* Write your code here */ | ||
// Return instance of http.Server class | ||
const compressionTypes = { | ||
gzip: 'gzip', | ||
deflate: 'deflate', | ||
br: 'br', | ||
}; | ||
|
||
const server = new http.Server(); | ||
|
||
server.on('request', async (req, res) => { | ||
const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
||
if (url.pathname === '/' && req.method === 'GET') { | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/html'); | ||
|
||
const htmlPage = fs.createReadStream( | ||
path.resolve('src/public/', 'index.html'), | ||
); | ||
|
||
htmlPage.pipe(res); | ||
|
||
return; | ||
} | ||
|
||
if (url.pathname === '/compress' && req.method === 'GET') { | ||
res.statusCode = 400; | ||
res.end('Invalid method.'); | ||
|
||
return; | ||
} | ||
|
||
if (url.pathname === '/compress' && req.method === 'POST') { | ||
const form = new formidable.IncomingForm(); | ||
|
||
form.parse(req, (err, fields, files) => { | ||
if (err) { | ||
res.statusCode = 400; | ||
res.end('Invalid form data'); | ||
|
||
return; | ||
} | ||
|
||
const compressionType = fields.compressionType; | ||
|
||
if ( | ||
!compressionType || | ||
!Object.keys(compressionTypes).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 |
||
) { | ||
res.statusCode = 400; | ||
res.end('Invalid compression type'); | ||
|
||
return; | ||
} | ||
|
||
let uploadedFile = files.file; | ||
|
||
if (!uploadedFile) { | ||
res.statusCode = 400; | ||
res.end('No file provided'); | ||
|
||
return; | ||
} | ||
|
||
if (Array.isArray(uploadedFile)) { | ||
uploadedFile = uploadedFile[0]; | ||
} | ||
|
||
const filePath = uploadedFile.filepath; | ||
const originalFileName = uploadedFile.originalFilename; | ||
|
||
if (!filePath || !fs.existsSync(filePath)) { | ||
res.statusCode = 400; | ||
res.end('Compression file not provided'); | ||
|
||
return; | ||
} | ||
|
||
let compressionStream; | ||
let compressedExtension; | ||
|
||
switch (compressionType[0]) { | ||
case compressionTypes.gzip: | ||
compressionStream = zlib.createGzip(); | ||
compressedExtension = '.gzip'; | ||
break; | ||
case compressionTypes.deflate: | ||
compressionStream = zlib.createDeflate(); | ||
compressedExtension = '.deflate'; | ||
break; | ||
case compressionTypes.br: | ||
compressionStream = zlib.createBrotliCompress(); | ||
compressedExtension = '.br'; | ||
break; | ||
default: | ||
res.statusCode = 400; | ||
res.end('Invalid compression type'); | ||
|
||
return; | ||
} | ||
|
||
res.statusCode = 200; | ||
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. This line setting |
||
|
||
res.setHeader( | ||
'Content-Disposition', | ||
`attachment; filename=${originalFileName}${compressedExtension}`, | ||
); | ||
|
||
const fileStream = fs.createReadStream(filePath); | ||
|
||
fileStream.pipe(compressionStream).pipe(res); | ||
|
||
compressionStream.pipe( | ||
fs.createWriteStream(filePath + compressedExtension), | ||
); | ||
|
||
res.on('close', () => { | ||
fileStream.destroy(); | ||
}); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
res.statusCode = 404; | ||
res.end('non-existing endpoint'); | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!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> | ||
<h1>Compression App</h1> | ||
<br /> | ||
<form action="http://localhost:5700/compress" method="post"> | ||
<input type="file" name="file" /> | ||
<br /> | ||
<p>Select compression type</p> | ||
<br /> | ||
<select name="compressionType" id="compressionType"> | ||
<option value="selectCompressionType" disabled selected> | ||
Select compression type | ||
</option> | ||
<option value="gzip">gzip</option> | ||
<option value="deflate">deflate</option> | ||
<option value="br">br</option> | ||
</select> | ||
<br /> | ||
<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
is being accessed as an array (compressionType[0]
), but it is likely a string. Ensure thatcompressionType
is correctly handled as a string, or verify that it is indeed an array and adjust the logic accordingly.