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

Solution #139

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
65 changes: 65 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
}
.main {
padding: 1rem;
display: flex;
flex-direction: column;
gap: 2rem;
align-items: center;
margin: 0 auto;
}
.main__title {
margin: 0;
}
.main__form{
display: flex;
flex-direction: column;
gap: 1rem;
}
.main__label-text {
margin: 0;
margin-bottom: 0.5rem;
}
.main__form-submit {
padding: 0.5rem;
font-weight: bold;
border: 1px solid grey;
border-radius: 0.5rem;
transition: opacity 0.4s;
}
.main__form-submit:hover {
cursor: pointer;
opacity: 0.6;
}
</style>
</head>
<body>
<main class="main">
<h1 class="main__title">Compress your file</h1>
<form class="main__form" action="/compress" method="post"
enctype="multipart/form-data">
<label>
<p class="main__label-text">Choose compression type</p>
<select name="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
</label>
<label>
<p class="main__label-text">Choose your file</p>
<input type="file" name="file" />
</label>
<button class="main__form-submit" type="submit">Submit</button>
</form>
</main>
</body>
</html>
115 changes: 113 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,119 @@
'use strict';

const http = require('http');
const path = require('path');
const fs = require('fs');
const mime = require('mime-types');
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) => {
const url = new URL(req.url || '', `http://${req.headers.host}`);
const requestedPath = url.pathname.slice(1) || 'index.html';

if (requestedPath === 'compress' && req.method === 'GET') {
res.writeHead(400, { 'content-type': 'text/plain' });
res.end('Request should be POST method!');

return;
}

if (requestedPath === 'compress' && req.method === 'POST') {
const form = formidable({});

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

form.parse(req, (err, fields, files) => {
if (err) {
res.statusCode = 400;
res.end('Form parsing error');

return;
}

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

if (!compressionType || !compressors.hasOwnProperty(compressionType)) {
res.statusCode = 400;
res.end('Invalid or missing compression type');

return;
}

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

if (!file || !file.filepath || !file.originalFilename) {
res.statusCode = 400;
res.end('Invalid or missing file');

return;
}

Choose a reason for hiding this comment

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

Ensure that file[0] is a valid object with the expected properties. If file is not an array, this line will cause an error.

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

res.writeHead(200, {
'content-disposition': `attachment; filename=${file.originalFilename}.${compressionType}`,
});
Comment on lines +65 to +67

Choose a reason for hiding this comment

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

Consider adding a Content-Type header to the response to ensure that the client handles the file download correctly. This can be done by setting res.setHeader('Content-Type', 'application/octet-stream');.


pipeline(fileStream, gzipStream, res, (error) => {
if (error) {
res.statusCode = 500;
res.end('Compression error');

return;
}
res.end();
});
});

return;
}

const realPath = path.join('public', requestedPath);

if (!fs.existsSync(realPath)) {
res.statusCode = 404;
res.end('Not Found');

return;
}

const contentStream = fs.createReadStream(realPath);
const mimeType = mime.contentType(path.extname(realPath) || 'text/plain');

res.statusCode = 200;
res.setHeader('Content-type', mimeType);

contentStream.on('data', (chunk) => {
res.write(chunk);
});

contentStream.on('end', () => {
res.end();
});

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

contentStream.on('error', () => {
res.statusCode = 500;
res.end('Server error');
});
});

return server;
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
/* eslint-disable no-console */
/* Don't change code below */

Expand Down
Loading