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

Develop #135

Open
wants to merge 3 commits 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
9 changes: 5 additions & 4 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
136 changes: 134 additions & 2 deletions src/createServer.js
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])

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 that compressionType is correctly handled as a string, or verify that it is indeed an array and adjust the logic accordingly.

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 should be a string. Ensure that compressionType is correctly handled as a string.

) {
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;

Choose a reason for hiding this comment

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

This line setting res.statusCode = 200; is redundant because the status code is already set earlier. Consider removing this line to avoid confusion.


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