-
Notifications
You must be signed in to change notification settings - Fork 306
solution #164
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
Open
yurzxw
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
yurzxw:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
solution #164
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,121 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const { pipeline } = require('stream'); | ||
| const zlib = require('zlib'); | ||
| const { IncomingForm } = require('formidable'); | ||
|
|
||
| const compressionMap = { | ||
| gzip: { stream: zlib.createGzip, ext: '.gz' }, | ||
| deflate: { stream: zlib.createDeflate, ext: '.deflate' }, | ||
| br: { stream: zlib.createBrotliCompress, ext: '.br' }, | ||
| }; | ||
|
|
||
| const uploadsDir = path.join(__dirname, 'uploads'); | ||
|
|
||
| if (!fs.existsSync(uploadsDir)) { | ||
| fs.mkdirSync(uploadsDir); | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| const handleCompression = (req, res) => { | ||
| const form = new IncomingForm({ | ||
| multiples: false, | ||
| keepExtensions: true, | ||
| uploadDir: uploadsDir, | ||
| }); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| console.error('Error parsing form data:', err); | ||
| res.writeHead(400); | ||
|
|
||
| return res.end('Error parsing form data'); | ||
| } | ||
|
|
||
| const file = Array.isArray(files.file) ? files.file[0] : files.file; | ||
| const compressionType = Array.isArray(fields.compressionType) | ||
| ? fields.compressionType[0] | ||
| : fields.compressionType; | ||
|
|
||
| if (!file || !compressionType || !compressionMap[compressionType]) { | ||
| console.log('Validation failed:', { file, compressionType }); | ||
| res.writeHead(400); | ||
|
|
||
| return res.end('Invalid form or compression type'); | ||
| } | ||
|
|
||
| const filepath = file.filepath; | ||
| const originalFilename = file.originalFilename; | ||
|
|
||
| if (!filepath) { | ||
| console.log('Missing file path:', file); | ||
| res.writeHead(400); | ||
|
|
||
| return res.end('Missing file path'); | ||
| } | ||
|
|
||
| const { stream: compressor, ext } = compressionMap[compressionType]; | ||
|
|
||
| const finalExt = compressionType === 'gzip' ? '.gzip' : ext; | ||
| const compressedFileName = originalFilename + finalExt; | ||
| // --- КІНЕЦЬ ВИПРАВЛЕНЬ --- | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/octet-stream', | ||
|
|
||
| 'Content-Disposition': `attachment; filename=${compressedFileName}`, | ||
| // --- КІНЕЦЬ ВИПРАВЛЕНЬ ДЛЯ ТЕСТУ --- | ||
| }); | ||
|
|
||
| pipeline(fs.createReadStream(filepath), compressor(), res, (error) => { | ||
| fs.unlink(filepath, (unlinkErr) => { | ||
| if (unlinkErr) { | ||
| console.error( | ||
| `Error deleting temporary file ${filepath}:`, | ||
| unlinkErr, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| if (error) { | ||
| console.error('Compression pipeline failed:', error); | ||
| res.writeHead(500); | ||
| res.end('Compression failed'); | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| function createServer() { | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| const htmlPath = path.join(process.cwd(), 'public', 'index.html'); | ||
|
|
||
| fs.readFile(htmlPath, (err, data) => { | ||
| if (err) { | ||
| console.error('Error loading html:', err); | ||
| res.writeHead(500); | ||
| res.end('Error loading html'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(data); | ||
| }); | ||
| } else if (req.url === '/compress' && req.method === 'POST') { | ||
| handleCompression(req, res); | ||
| } else if (req.url === '/compress') { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Only POST is allowed on /compress'); | ||
| } else { | ||
| res.writeHead(404); | ||
| res.end('Not found'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { createServer }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Capio ex odio suffoco. Voluptatibus id vitium benigne soluta virtus. Depraedor concido denique sed conitor conservo spargo comitatus abbas. | ||
| Adulatio venia viriliter distinctio sollicito artificiose asperiores. Adsidue vergo creo concido commodi suspendo taedium sumo ultra. Varietas censura claudeo blandior amissio patrocinor. | ||
| Uter dolores caries tenax umerus conicio crastinus et tenus tergum. Ipsum congregatio creber cito iure deleniti. Harum cras canis perspiciatis adfero. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Nostrum ab caelestis amaritudo aggero aliqua cito trucido verus admitto. Ex arcesso talis. Crepusculum tenus abbas. | ||
| Certus patria adimpleo crebro conitor appello. Degenero turba appello carus arto. Clamo absque bardus demitto. | ||
| Tondeo tepesco id pecto charisma nisi tutis. Aeneus concedo theatrum curto statua tribuo itaque addo aegrus. Tyrannus vado suppellex textor quae. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Statua pauci demens tyrannus administratio bestia versus. Sulum peior iste auctor capio temeritas. Vesper impedit deduco confido vulticulus sit amo conor. | ||
| Subito autus et cauda calco territo canto admoveo avaritia cimentarius. Eos earum contabesco utique. Cur tolero adipisci illo. | ||
| Tero arx asperiores accedo. Copia careo statua cupiditate coma desparatus. Conventus aufero dapifer aqua cado cum utique. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Aequitas enim colligo thymbra tabgo solium vulnus voluptas patria apparatus. Vergo facilis vox tactus. Vilitas valens torrens magni tolero balbus. | ||
| Corporis triumphus depromo crastinus clamo una stultus. Acies alo speciosus votum attero annus casus adficio. Vitiosus decumbo vestigium vinco amoveo cunctatio turbo clamo. | ||
| Despecto tabesco appello. Certus repudiandae dolor. Cimentarius calcar vociferor aestus. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Issue: The code uses
.gzipas the extension for gzip-compressed files. The standard extension for gzip is.gz. Please changeconst finalExt = compressionType === 'gzip' ? '.gzip' : ext;to use.gzinstead of.gzipfor gzip files.