-
Notifications
You must be signed in to change notification settings - Fork 304
solution #211
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 #211
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,8 @@ | ||
| { | ||
| "permissions": { | ||
| "allow": [ | ||
| "Bash(npm test:*)", | ||
| "Bash(npm run:*)" | ||
| ] | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,135 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const zlib = require('zlib'); | ||
| const { formidable } = require('formidable'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = http.createServer((req, res) => { | ||
| // Handle root path - serve HTML file | ||
| if (req.url === '/' && req.method === 'GET') { | ||
| const htmlPath = path.join(__dirname, 'public', 'index.html'); | ||
|
|
||
| fs.readFile(htmlPath, 'utf8', (err, data) => { | ||
| if (err) { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('Internal Server Error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(data); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Handle CSS file | ||
| if (req.url === '/styles.css' && req.method === 'GET') { | ||
| const cssPath = path.join(__dirname, 'public', 'styles.css'); | ||
|
|
||
| fs.readFile(cssPath, 'utf8', (err, data) => { | ||
| if (err) { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/css' }); | ||
| res.end(data); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Handle /compress endpoint | ||
| if (req.url === '/compress') { | ||
| if (req.method !== 'POST') { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: Only POST method is allowed'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const form = formidable({}); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: Could not parse form data'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressionType = Array.isArray(fields.compressionType) | ||
| ? fields.compressionType[0] | ||
| : fields.compressionType; | ||
|
|
||
| const file = Array.isArray(files.file) ? files.file[0] : files.file; | ||
|
|
||
| if (!file) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: No file provided'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!compressionType) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: No compression type provided'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const validTypes = ['gzip', 'deflate', 'br']; | ||
|
|
||
| if (!validTypes.includes(compressionType)) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: Unsupported compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const fileName = file.originalFilename; | ||
| const compressedFileName = `${fileName}.${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. This line still doesn't produce the correct file extensions as required by the task. For a |
||
|
|
||
| let compressionStream; | ||
|
|
||
| switch (compressionType) { | ||
| case 'gzip': | ||
| compressionStream = zlib.createGzip(); | ||
| break; | ||
|
|
||
| case 'deflate': | ||
| compressionStream = zlib.createDeflate(); | ||
| break; | ||
|
|
||
| case 'br': | ||
| compressionStream = zlib.createBrotliCompress(); | ||
| break; | ||
| } | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/octet-stream', | ||
| 'Content-Disposition': `attachment; filename=${compressedFileName}`, | ||
| }); | ||
|
|
||
| fs.createReadStream(file.filepath).pipe(compressionStream).pipe(res); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Handle 404 for all other routes | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>File Compression App</title> | ||
| <link rel="stylesheet" href="/styles.css"> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <h1>🗜️ File Compression App</h1> | ||
| <form action="/compress" method="POST" enctype="multipart/form-data"> | ||
| <div class="form-group"> | ||
| <label for="file">Select File:</label> | ||
| <input type="file" name="file" id="file" reqiered> | ||
|
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. There's a typo in the |
||
| </div> | ||
|
|
||
| <div class="form-group"> | ||
| <label for="compressionType">Compression Type:</label> | ||
| <select name="compressionType" id="compressionType" required> | ||
| <option value="gzip">Gzip (.gz)</option> | ||
| <option value="deflate">Deflate (.dfl)</option> | ||
| <option value="br">Brotli (.br)</option> | ||
| </select> | ||
| </div> | ||
|
|
||
| <button type="submit">Compress File</button> | ||
| </form> | ||
|
|
||
| <div class="info"> | ||
| <strong>ℹ️ Info:</strong> | ||
| Select a file and choose a compression algorithm. | ||
| The compressed file will be downloaded automatically. | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| max-width: 600px; | ||
| margin: 50px auto; | ||
| padding: 20px; | ||
| background-color: #f5f5f5; | ||
| } | ||
|
|
||
| .container { | ||
| background: white; | ||
| padding: 30px; | ||
| border-radius: 8px; | ||
| box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); | ||
| } | ||
|
|
||
| h1 { | ||
| color: #333; | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| .form-group { | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| label { | ||
| display: block; | ||
| margin-bottom: 5px; | ||
| color: #555; | ||
| font-weight: bold; | ||
| } | ||
|
|
||
| input[type="file"], | ||
| select { | ||
| width: 100%; | ||
| padding: 10px; | ||
| border: 1px solid #ddd; | ||
| border-radius: 4px; | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| button { | ||
| background-color: #4CAF50; | ||
| color: white; | ||
| padding: 12px 30px; | ||
| border: none; | ||
| border-radius: 4px; | ||
| cursor: pointer; | ||
| font-size: 16px; | ||
| width: 100%; | ||
| } | ||
|
|
||
| button:hover { | ||
| background-color: #45a049; | ||
| } | ||
|
|
||
| .info { | ||
| margin-top: 20px; | ||
| padding: 15px; | ||
| background-color: #e7f3fe; | ||
| border-left: 4px solid #2196F3; | ||
| color: #333; | ||
| } |
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 way the compressed filename is constructed doesn't fully align with the task requirements. The requirement specifies extensions
.gzforgzip,.dflfordeflate, and.brforbr. This implementation uses thecompressionTypevalue directly, which results in incorrect extensions like.gzipand.deflate. You should create a mapping between the compression type and its corresponding extension to generate the correct filename.