Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykhailo Lapchynskyi authored and Mykhailo Lapchynskyi committed Nov 26, 2024
1 parent 5afc1d4 commit 1cd224f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 46 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"busboy": "^1.6.0"
}
}
80 changes: 35 additions & 45 deletions src/createServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const zlib = require('zlib');
const path = require('path');
const fs = require('fs');
const { Readable } = require('stream');
const busboy = require('busboy');

function createServer() {
const server = http.createServer();
Expand Down Expand Up @@ -47,49 +48,47 @@ function createServer() {
res.statusCode = 400;
res.end('Method not allowed');
} else if (req.method === 'POST' && req.url === '/compress') {
const chunks = [];
const contentType = req.headers['content-type'];

req.on('data', (chunk) => chunks.push(chunk));
if (!contentType || !contentType.includes('multipart/form-data')) {
res.statusCode = 400;
res.end('Invalid or missing Content-Type');

req.on('end', () => {
const data = Buffer.concat(chunks);
return;
}

if (data.length === 0) {
res.statusCode = 400;
res.end('No file provided');
let fileBuffer = null;
let fileName = null;
let compressionType = null;

return;
}
const bb = busboy({ headers: req.headers });

const boundaryMatch = req.headers['content-type']?.match(
/boundary=(?:"([^"]+)"|([^;]+))/i,
);
bb.on('file', (name, file, info) => {
const chunks = [];

if (!boundaryMatch) {
res.statusCode = 400;
res.end('Invalid content type');
fileName = info.filename;

return;
}

const boundary = boundaryMatch[1] || boundaryMatch[2];
const parts = data.toString().split(`--${boundary}`);
file.on('data', (data) => {
chunks.push(data);
});

let compressionType;
let fileName;
file.on('end', () => {
fileBuffer = Buffer.concat(chunks);
});
});

for (const part of parts) {
if (part.includes('name="compressionType"')) {
compressionType = part.split('\r\n\r\n')[1]?.trim();
}
bb.on('field', (name, value) => {
if (name === 'compressionType') {
compressionType = value;
}
});

if (part.includes('filename=')) {
const filenameMatch = part.match(/filename="([^"]+)"/);
bb.on('finish', () => {
if (!fileBuffer || fileBuffer.length === 0) {
res.statusCode = 400;
res.end('No file provided');

if (filenameMatch) {
fileName = filenameMatch[1];
}
}
return;
}

if (!compressionType || !fileName) {
Expand All @@ -112,6 +111,7 @@ function createServer() {
compressionStream = zlib.createBrotliCompress();
break;
default:
console.log(`Unsupported compression type: ${compressionType}`);
res.statusCode = 400;
res.end('Unsupported compression type');

Expand All @@ -132,19 +132,7 @@ function createServer() {
);
res.setHeader('Content-Type', 'application/octet-stream');

const fileContent = parts
.find((part) => part.includes('filename='))
?.split('\r\n\r\n')[1]
?.trim();

if (!fileContent) {
res.statusCode = 400;
res.end('No file content');

return;
}

const fileStream = Readable.from(Buffer.from(fileContent));
const fileStream = Readable.from(fileBuffer);

fileStream
.pipe(compressionStream)
Expand All @@ -155,6 +143,8 @@ function createServer() {
res.end('Server error');
});
});

req.pipe(bb);
} else {
res.statusCode = 404;
res.end('Not found');
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

'use strict';

const { createServer } = require('./createServer.ts');
const { createServer } = require('./createServer.js');

createServer().listen(5700, () => {
console.log('Server started! 🚀');
Expand Down

0 comments on commit 1cd224f

Please sign in to comment.