Skip to content
Open

sol #174

Show file tree
Hide file tree
Changes from 4 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
88 changes: 86 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,92 @@
'use strict';

const http = require('http');
const zlib = require('zlib');
const fs = require('fs');
const formidable = require('formidable');

// Lorem2 ipsum dolor sit amet, consectetur adipiscing elit.
function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Welcome to the File Upload Server</h1>');

return;
}

if (req.url !== '/compress') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');

return;
}

if (req.method !== 'POST') {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: POST method required');

return;
}

const form = new formidable.IncomingForm();

form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');

return;
}

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

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

return;
}

let compressor;
let extension;

switch (compressionType) {
case 'gzip':
compressor = zlib.createGzip();
extension = '.gzip';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the task description, the extension for gzip compression should be .gz.

break;
case 'deflate':
compressor = zlib.createDeflate();
extension = '.deflate';
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 task description specifies the extension for deflate compression should be .dfl.

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 file extensions for gzip and deflate compression types do not match the task description. The description specifies .gz for gzip and .dfl for deflate.

break;
case 'br':
compressor = zlib.createBrotliCompress();
extension = '.br';
break;
default:
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: Unsupported compression type');

return;
}

const originalFileName = file.originalFilename || 'uploaded_file';
const compressedFileName = `${originalFileName}${extension}`;

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

const inputStream = fs.createReadStream(file.filepath);

inputStream.pipe(compressor).pipe(res);
});
});
}

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!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>
<form
action="http://localhost:5700/compress"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="file" />
<select name="compressionType" id="">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">Submit</button>
</form>
</body>
</html>