Skip to content

Commit

Permalink
resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-vvd committed Oct 19, 2023
1 parent 8d856dc commit 1fc3e36
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 53 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"type": "module",
"name": "node_compression-app",
"version": "1.0.0",
"description": "Node.js compression app",
Expand Down
45 changes: 9 additions & 36 deletions src/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,19 @@
</head>
<body>
<h1>Choose a File and Compression Type, then Upload</h1>
<div>
<input type="file" id="fileInput" />
<select id="compressionType">
<form
action="/upload"
method="post"
enctype="multipart/form-data"
>
<input type="file" id="fileInput" name="file"/>
<select id="compressionType" name="select">
<option value="none">No Compression</option>
<option value="gzip">GZIP Compression</option>
<option value="deflate">Deflate Compression</option>
</select>
<button onclick="uploadFile()">Upload</button>
</div>
<button>Upload</button>
</form>
<div id="status"></div>

<script>
const uploadFile = () => {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];

if (!file) {
alert('Please choose a file before uploading.');
return;
}

const compressionType = document.getElementById('compressionType').value;

const formData = new FormData();
formData.append('file', file);
formData.append('compressionType', compressionType);
fetch('http://localhost:3000/upload', {
method: 'POST',
body: formData
})
/*.then(response => response.json())
.then(data => {
document.getElementById('status').innerHTML = 'File uploaded successfully!';
console.log(data); // You can handle the server's response here if needed.
})*/
.catch(error => {
document.getElementById('status').innerHTML = 'Error uploading file: ' + error.message;
console.error('Error uploading file:', error);
});
}
</script>
</body>
</html>
40 changes: 24 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
'use strict';

import http from 'http'
import fs from 'fs'
import zlib from 'zlib'
import formidable from 'formidable';

const http = require('http');
const fs = require('fs');
const zlib = require('zlib');
const formidable = require('formidable');
const server = new http.Server();

server.on('request', async (req, res) => {
server.on('request', async(req, res) => {
if (req.url === '/upload') {
const form = formidable({});
let [fields, files] = await form.parse(req);
res.setHeader('Content-Type', 'text/plain');
const [fields, files] = await form.parse(req);

res.setHeader('Content-Disposition', 'attachment; filename="text.txt"');

let compressionStream
switch (fields.compressionType[0]) {
let compressionStream;

switch (fields.select[0]) {
case 'gzip':
compressionStream = zlib.createGzip();
res.setHeader('Content-Encoding', 'gzip');
break;
case 'deflate':
compressionStream = zlib.createDeflate()
compressionStream = zlib.createDeflate();
res.setHeader('Content-Encoding', 'deflate');
break;
}

console.log('files.file[0].filepath', files.file[0].filepath);
console.log('compressionStream', compressionStream);
const fileStream = fs.createReadStream(files.file[0].filepath);

fileStream.pipe(compressionStream).pipe(res);
fileStream
// eslint-disable-next-line
.on('error', (error) => console.log('fileStream ERROR', error))
.pipe(compressionStream)
// eslint-disable-next-line
.on('error', (error) => console.log('compressionStream ERROR', error))
.pipe(res)
// eslint-disable-next-line
.on('error', (error) => console.log('res ERROR', error));
}

if (req.url === '/') {
Expand All @@ -46,9 +52,11 @@ server.on('request', async (req, res) => {
});

server.on('error', (error) => {
console.log('error', error)
})
// eslint-disable-next-line
console.log('server error', error);
});

server.listen(3000, () => {
// eslint-disable-next-line
console.log('server running...');
});

0 comments on commit 1fc3e36

Please sign in to comment.