-
Notifications
You must be signed in to change notification settings - Fork 305
sol #174
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?
sol #174
Changes from 3 commits
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,91 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const zlib = require('zlib'); | ||
| const fs = require('fs'); | ||
| const formidable = require('formidable'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| if (req.url === '/') { | ||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end('<h1>Welcome to the File Upload Server</h1>'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.url !== '/compress') { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not Found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method !== 'POST') { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: POST method required'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const form = new formidable.IncomingForm(); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('Internal Server Error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const file = Array.isArray(files.file) ? files.file[0] : files.file; | ||
| const compressionType = Array.isArray(fields.compressionType) | ||
| ? fields.compressionType[0] | ||
| : fields.compressionType; | ||
|
|
||
| if (!file || !file.filepath || !compressionType) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: No file uploaded'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let compressor; | ||
| let extension; | ||
|
|
||
| switch (compressionType) { | ||
| case 'gzip': | ||
| compressor = zlib.createGzip(); | ||
| extension = '.gzip'; | ||
| break; | ||
| case 'deflate': | ||
| compressor = zlib.createDeflate(); | ||
| extension = '.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. The task description specifies the extension for 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 file extensions for |
||
| break; | ||
| case 'br': | ||
| compressor = zlib.createBrotliCompress(); | ||
| extension = '.br'; | ||
| break; | ||
| default: | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request: Unsupported compression type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const originalFileName = file.originalFilename || 'uploaded_file'; | ||
| const compressedFileName = `${originalFileName}${extension}`; | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'text/plain', | ||
| 'Content-Disposition': `attachment; filename=${compressedFileName}`, | ||
| }); | ||
|
|
||
| const inputStream = fs.createReadStream(file.filepath); | ||
|
|
||
| inputStream.pipe(compressor).pipe(res); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <form | ||
| action="http://localhost:5700/compress" | ||
| method="post" | ||
| enctype="multipart/form-data" | ||
| > | ||
| <input type="file" name="file" /> | ||
| <select name="compressionType" id=""> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> |
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.
According to the task description, the extension for
gzipcompression should be.gz.