-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
0 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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,61 +1 @@ | ||
if (req.method === 'POST' && req.url === '/compress') { | ||
const form = new formidable.IncomingForm(); | ||
|
||
form.parse(req, (err, fields, files) => { | ||
if (err) { | ||
res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
res.end('Error parsing the form'); | ||
|
||
return; | ||
} | ||
|
||
const file = files.file; | ||
const compressionType = fields.compressionType; | ||
|
||
if (!file || !compressionType) { | ||
res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
res.end('Form is invalid'); | ||
|
||
return; | ||
} | ||
|
||
let compressor; | ||
let extension; | ||
|
||
switch (compressionType) { | ||
case 'gzip': | ||
compressor = zlib.createGzip(); | ||
extension = '.gz'; | ||
break; | ||
case 'deflate': | ||
compressor = zlib.createDeflate(); | ||
extension = '.dfl'; | ||
break; | ||
case 'br': | ||
compressor = zlib.createBrotliCompress(); | ||
extension = '.br'; | ||
break; | ||
default: | ||
res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
res.end('Unsupported compression type'); | ||
|
||
return; | ||
} | ||
|
||
const inputFile = fs.createReadStream(file.filepath); | ||
const outputFileName = file.originalFilename + extension; | ||
|
||
res.writeHead(200, { | ||
'Content-Disposition': `attachment; filename="${outputFileName}"`, | ||
'Content-Type': 'application/octet-stream', | ||
}); | ||
|
||
inputFile.pipe(compressor).pipe(res); | ||
}); | ||
} else if (req.method === 'GET' && req.url === '/compress') { | ||
res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
res.end('GET method not supported for /compress endpoint'); | ||
} else { | ||
res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
res.end('Not Found'); | ||
} |