Commit 74de83a5 authored by Nilanjan Daw's avatar Nilanjan Daw

fixed and updated file download bug

parent 901024e7
...@@ -77,9 +77,9 @@ libSupport.makeTopic(node_id).then(() => { ...@@ -77,9 +77,9 @@ libSupport.makeTopic(node_id).then(() => {
/** /**
* download and start grunt * download and start grunt
*/ */
libSupport.download(constants.grunt_host, "grunt").then(() => { libSupport.download(constants.grunt_host, "grunt", false).then(() => {
logger.info("Downloaded grunt binary from repository") logger.info("Downloaded grunt binary from repository")
fs.chmod('grunt', 0o555, (err) => { fs.chmod('grunt', 0o755, (err) => {
logger.info("grunt made executable. Starting grunt") logger.info("grunt made executable. Starting grunt")
let grunt = spawn('./grunt', [node_id]) let grunt = spawn('./grunt', [node_id])
grunt.stdout.on('data', data => { grunt.stdout.on('data', data => {
......
const http = require('http'); const fetch = require('node-fetch');
const fs = require('fs'); const fs = require('fs');
const process = require('process') const process = require('process')
const { spawnSync } = require('child_process'); const { spawnSync } = require('child_process');
...@@ -50,28 +50,48 @@ function makeTopic(id) { ...@@ -50,28 +50,48 @@ function makeTopic(id) {
}) })
} }
var download = function (url, dest, cb) { // var download = function (url, dest, check = true, cb) {
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
// console.log(url);
// if (!check || !fs.existsSync(dest)) {
// var file = fs.createWriteStream(dest);
// var request = https.get(url, function (response) {
// response.pipe(file);
// file.on('finish', function () {
// file.close(cb); // close() is async, call cb after close completes.
// resolve();
// });
// }).on('error', function (err) { // Handle errors
// fs.unlink(dest); // Delete the file async. (But we don't check the result)
// logger.error("download failed" + err.message);
// if (cb) cb(err.message);
// reject(err);
// });
// } else {
// resolve();
// }
// })
// };
const download = (async (url, path, check = true) => {
if (!check || !fs.existsSync(path)) {
console.log(url); console.log(url);
if (!fs.existsSync(dest)) { const res = await fetch(url);
var file = fs.createWriteStream(dest); const fileStream = fs.createWriteStream(path);
var request = https.get(url, function (response) { await new Promise((resolve, reject) => {
response.pipe(file); res.body.pipe(fileStream);
file.on('finish', function () { res.body.on("error", (err) => {
file.close(cb); // close() is async, call cb after close completes.
resolve();
});
}).on('error', function (err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
reject(err); reject(err);
}); });
} else { fileStream.on("finish", function () {
resolve(); resolve();
});
});
} }
}) });
};
function makeid(length) { function makeid(length) {
var result = ''; var result = '';
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"kafka-node": "^5.0.0", "kafka-node": "^5.0.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"mqtt": "^3.0.0", "mqtt": "^3.0.0",
"node-fetch": "^2.6.0",
"redis": "^2.8.0", "redis": "^2.8.0",
"request": "^2.88.2", "request": "^2.88.2",
"winston": "^3.2.1" "winston": "^3.2.1"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment