-
Notifications
You must be signed in to change notification settings - Fork 200
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
base: master
Are you sure you want to change the base?
Solution #139
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,108 @@ | ||
'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, { compressionType }, { file }) => { | ||
if (err || !file || !compressionType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
res.statusCode = 400; | ||
res.end('Form error'); | ||
|
||
return; | ||
} | ||
|
||
if (!compressors.hasOwnProperty(compressionType[0])) { | ||
res.statusCode = 400; | ||
res.end('No such compression type'); | ||
|
||
return; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
const uploadFile = file[0]; | ||
const gzipStream = compressors[compressionType[0]](); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
const fileStream = fs.createReadStream(uploadFile.filepath); | ||
|
||
res.writeHead(200, { | ||
'content-disposition': `attachment; filename=${uploadFile.originalFilename}.${compressionType[0]}`, | ||
}); | ||
|
||
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 = { | ||
|
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 */ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destructuring of
compressionType
andfile
from the parsed form data might not work as expected if the form fields are not named exactly as assumed. Ensure that the form fields are correctly named and thatcompressionType
andfile
are accessed correctly.