-
Notifications
You must be signed in to change notification settings - Fork 306
solution #231
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
base: master
Are you sure you want to change the base?
solution #231
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 |
|---|---|---|
| @@ -1,8 +1,97 @@ | ||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
| const multer = require('multer'); | ||
| const zlib = require('zlib'); | ||
| const fs = require('fs'); | ||
| const http = require('http'); | ||
|
|
||
| const upload = multer({ dest: 'uploads/' }); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const html = ` | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>My Server</title> | ||
| </head> | ||
| <body> | ||
| <form action="/compress" method="post" enctype="multipart/form-data"> | ||
| <input type="file" name="file"> | ||
|
|
||
| <select name="compressionType"> | ||
| <option value="none">No Compression</option> | ||
|
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. This option should be removed. The task specifies only |
||
| <option value="gzip">Gzip</option> | ||
| <option value="deflate">Deflate</option> | ||
| <option value="br">Brotli</option> | ||
| </select> | ||
|
|
||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> | ||
| `; | ||
| const app = express(); | ||
|
|
||
| app.get('/', (req, res) => { | ||
| res.send(html); | ||
| }); | ||
|
|
||
| app.get('/compress', (req, res) => { | ||
| res.status(400).send('Bad Request'); | ||
| }); | ||
|
|
||
| app.post('/compress', upload.single('file'), (req, res) => { | ||
| const file = req.file; | ||
| const compressionType = req.body.compressionType; | ||
|
|
||
| if (!file) { | ||
| return res.status(400).send('No file uploaded.'); | ||
| } | ||
|
|
||
| if (!compressionType) { | ||
| return res.status(400).send('No compression type provided.'); | ||
| } | ||
|
|
||
| let transformer; | ||
| let newFilename = file.originalname; | ||
|
|
||
| switch (compressionType) { | ||
| case 'gzip': | ||
| transformer = zlib.createGzip(); | ||
| newFilename += '.gzip'; | ||
|
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. According to requirements, gzip extension should be 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 extension should be |
||
| break; | ||
| case 'deflate': | ||
| transformer = zlib.createDeflate(); | ||
| newFilename += '.deflate'; | ||
|
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. According to requirements, deflate extension should be 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 extension should be |
||
| break; | ||
| case 'br': | ||
| transformer = zlib.createBrotliCompress(); | ||
| newFilename += '.br'; | ||
| break; | ||
| case 'none': | ||
| res.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${newFilename}`, | ||
| ); | ||
|
|
||
| return fs.createReadStream(file.path).pipe(res); | ||
| default: | ||
| return res.status(400).send('Invalid compression type.'); | ||
| } | ||
|
|
||
| res.setHeader('Content-Disposition', `attachment; filename=${newFilename}`); | ||
|
|
||
| const readStream = fs.createReadStream(file.path); | ||
|
|
||
| readStream.pipe(transformer).pipe(res); | ||
| }); | ||
|
|
||
| app.use((req, res) => { | ||
| res.status(404).send('404 - Сторінку не знайдено'); | ||
| }); | ||
|
|
||
| return http.createServer(app); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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 select field should only have options:
gzip,deflate,bras specified in requirements. The 'none' option is not required.