Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
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
44 changes: 25 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
83 changes: 81 additions & 2 deletions src/createServer.js
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 }) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compressionType and file are accessed as arrays (e.g., compressionType[0] and file[0]). Ensure that these variables are indeed arrays. If they are not, you should access them directly without indexing.

if (error || !compressionType || !file) {
res.statusCode = 400;
res.end('form parsing error');

return;
}

if (!['gzip', 'deflate', 'br'].includes(compressionType[0])) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for compressionType[0] assumes compressionType is an array. Verify that compressionType is an array. If not, adjust the code to handle it correctly.

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]}`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file[0].originalFilename assumes file is an array. Ensure that file is indeed an array. If not, adjust the code to access the filename correctly.

);

const fileStream = fs.createReadStream(file[0].filepath);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file[0].filepath assumes file is an array. Verify that file is an array. If not, adjust the code to access the filepath correctly.


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 = {
Expand Down
33 changes: 33 additions & 0 deletions src/public/index.html
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>
Loading