Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(npm test:*)",
"Bash(npm run:*)"
]
}
}
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": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
131 changes: 129 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,135 @@
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
// Handle root path - serve HTML file
if (req.url === '/' && req.method === 'GET') {
const htmlPath = path.join(__dirname, 'public', 'index.html');

fs.readFile(htmlPath, 'utf8', (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');

return;
}

res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});

return;
}

// Handle CSS file
if (req.url === '/styles.css' && req.method === 'GET') {
const cssPath = path.join(__dirname, 'public', 'styles.css');

fs.readFile(cssPath, 'utf8', (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');

return;
}

res.writeHead(200, { 'Content-Type': 'text/css' });
res.end(data);
});

return;
}

// Handle /compress endpoint
if (req.url === '/compress') {
if (req.method !== 'POST') {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Only POST method is allowed');

return;
}

const form = formidable({});

form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Could not parse form data');

return;
}

const compressionType = Array.isArray(fields.compressionType)
? fields.compressionType[0]
: fields.compressionType;

const file = Array.isArray(files.file) ? files.file[0] : files.file;

if (!file) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: No file provided');

return;
}

if (!compressionType) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: No compression type provided');

return;
}

const validTypes = ['gzip', 'deflate', 'br'];

if (!validTypes.includes(compressionType)) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Unsupported compression type');

return;
}

const fileName = file.originalFilename;
const compressedFileName = `${fileName}.${compressionType}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The way the compressed filename is constructed doesn't fully align with the task requirements. The requirement specifies extensions .gz for gzip, .dfl for deflate, and .br for br. This implementation uses the compressionType value directly, which results in incorrect extensions like .gzip and .deflate. You should create a mapping between the compression type and its corresponding extension to generate the correct filename.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This line still doesn't produce the correct file extensions as required by the task. For a compressionType of 'gzip', the extension should be .gz, and for 'deflate', it should be .dfl. Currently, this will result in filenames like file.txt.gzip. Consider creating a map or a switch statement to associate the compression type with the correct extension string.


let compressionStream;

switch (compressionType) {
case 'gzip':
compressionStream = zlib.createGzip();
break;

case 'deflate':
compressionStream = zlib.createDeflate();
break;

case 'br':
compressionStream = zlib.createBrotliCompress();
break;
}

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${compressedFileName}`,
});

fs.createReadStream(file.filepath).pipe(compressionStream).pipe(res);
});

return;
}

// Handle 404 for all other routes
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
});

return server;
}

module.exports = {
Expand Down
37 changes: 37 additions & 0 deletions src/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Compression App</title>
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<div class="container">
<h1>🗜️ File Compression App</h1>
<form action="/compress" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="file">Select File:</label>
<input type="file" name="file" id="file" reqiered>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a typo in the required attribute. It should be spelled required for the browser to enforce that a file must be selected before submitting the form.

</div>

<div class="form-group">
<label for="compressionType">Compression Type:</label>
<select name="compressionType" id="compressionType" required>
<option value="gzip">Gzip (.gz)</option>
<option value="deflate">Deflate (.dfl)</option>
<option value="br">Brotli (.br)</option>
</select>
</div>

<button type="submit">Compress File</button>
</form>

<div class="info">
<strong>ℹ️ Info:</strong>
Select a file and choose a compression algorithm.
The compressed file will be downloaded automatically.
</div>
</div>
</body>
</html>
62 changes: 62 additions & 0 deletions src/public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}

.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
color: #333;
margin-bottom: 20px;
}

.form-group {
margin-bottom: 20px;
}

label {
display: block;
margin-bottom: 5px;
color: #555;
font-weight: bold;
}

input[type="file"],
select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}

button {
background-color: #4CAF50;
color: white;
padding: 12px 30px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
}

button:hover {
background-color: #45a049;
}

.info {
margin-top: 20px;
padding: 15px;
background-color: #e7f3fe;
border-left: 4px solid #2196F3;
color: #333;
}