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 #128

Open
wants to merge 2 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
10 changes: 6 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
44 changes: 44 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!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>
<div
style="
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
"
>
<h1>Compression App</h1>
<form
action="/compress"
method="POST"
style="display: flex; flex-direction: column; gap: 16px"
enctype="multipart/form-data"
id="uploadForm"
name="fileUpload"
>
<label for="file">Upload file: </label>
<input
style="cursor: pointer"
type="file"
name="file"
id="fileInput"
required
/>
<label for="select">Choose compression type: </label>
<select name="compressionType" id="select">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>
91 changes: 89 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,95 @@
'use strict';

const http = require('http');
const fs = require('fs');
const zlib = require('zlib');
const formidable = require('formidable');
const { pipeline } = require('stream');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server();

server.on('request', (req, res) => {
if (req.url === '/' && req.method === 'GET') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
fs.createReadStream('./public/index.html').pipe(res);

return;
}

if (req.url === '/compress' && req.method !== 'POST') {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');

res.end('Requests to /compress need to be POST method');

return;
}

if (req.url === '/compress' && req.method === 'POST') {
const form = new formidable.IncomingForm();

form.parse(
req,
(err, { compressionType: compressionTypes }, { file: files }) => {
if (!files || !compressionTypes || err) {
res.statusCode = 400;
res.end('Form error');

return;
}

const compressionType = compressionTypes[0];
const file = files[0];

const compressors = {
gzip: zlib.createGzip,
br: zlib.createBrotliCompress,
deflate: zlib.createDeflate,
};

if (!compressors.hasOwnProperty(compressionType)) {
res.statusCode = 400;
res.end('Unsupported compression type');

return;
}

const compressorType = compressors[compressionType]();
const fileStream = fs.createReadStream(file.filepath);

res.statusCode = 200;

res.setHeader(
'Content-Disposition',
`attachment; filename=${file.originalFilename}.${compressionType}`,
);

pipeline(fileStream, compressorType, res, (error) => {
if (error) {
// eslint-disable-next-line no-console
console.error('Pipeline failed', error);
}
});

res.on('close', () => {
compressorType.destroy();
});
},
);

return;
}

res.statusCode = 404;
res.end('Page not found');
});

// eslint-disable-next-line no-console
server.on('error', (error) => console.log(error));

return server;
}

module.exports = {
Expand Down
50 changes: 50 additions & 0 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable no-console */

const BASE = 'http://localhost:5700';

const form = document.getElementById('uploadForm');
const statusMessage = document.getElementById('status');

console.log('js is work!');

form.addEventListener('submit', async (event) => {
event.preventDefault();

const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];

if (!file) {
statusMessage.innerText = 'Please select a file!';

return;
}

const formData = new FormData();

formData.append('file', file);

try {
// eslint-disable-next-line no-undef
const response = await axios.post(`${BASE}/compress`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
responseType: 'blob',
});

const blob = response.data;
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');

link.href = downloadUrl;
link.download = `compressed_${file.name}.gz`;
document.body.appendChild(link);
link.click();
link.remove();

statusMessage.innerText = 'File compressed and downloaded!';
} catch (error) {
console.error('Upload error:', error);
statusMessage.innerText = 'An error occurred!';
}
});
Loading