my requirements are simply described as:
chunkSizemongodb
node.jsn(n)
writeStream
what to do now is
problems encountered:
<0><chunkSize-1>
Node.jsfdC
seek
(downloadpromisePromise):
function download(filedata,username,chunkSize) {
var chunks_n = Math.ceil(filedata.length/filedata.chunkSize);
var file_id = filedata._id;
var DLpromiseall = [];
//createWrite
fs.open(filedata.filename, "a", (err, fs) => {
if (err) {
console.log(err);
}
})
for(let curindex = 0;curindex < chunks_n;curindexPP) {
DLpromiseall.push(downloadpromise(username,file_id,curindex,filedata.filename,chunkSize));
}
var mytimer = setInterval(() => {
if(DLpromiseall.length == chunks_n) {
clearInterval(mytimer);
Promise.all(DLpromiseall).then(vales=>{
console.log(vales);
console.log("all download")
}).catch(err =>{
console.log(err);
console.log("");
})
}
},500)
}
The downloadPromise function is as follows:
*
**/
function downloadpromise(username,file_id,n,filename,chunkSize) {
return new Promise(function (resolve,reject) {
let mydata = {
username: username,
file_id: file_id,
n: n
};
let contents = queryString.stringify(mydata);
let options = {
host: "localhost",
path: "/nodedownload/",
port: 8000,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": contents.length
}
};
let req = http.request(options, function (res) {
let bufs = [];
res.on("data", function (chunk) {
bufs.push(chunk)
});
res.on("end", function (d) {
console.log("end")
var resubuf = Buffer.concat(bufs)
var resu = new Buffer(JSON.parse(resubuf));
try {
let WSoptions = {
start: n*chunkSize,
flags: "r+"
}
let WStream = fs.createWriteStream(filename,WSoptions)
WStream.write(resu,function () {
console.log("This is : "+n);
});
WStream.end();
resolve(filename);
}catch (err) {
console.log(err);
reject(err);
}
});
res.on("error", function (e) {
throw e;
})
});
req.write(contents);
req.end();
})
}