Categories
amazon-s3 express foreach javascript node.js

httpUploadProgress is not working as expected for Buffer data?

I am new to node js/express .I am trying to make multiple image uploading app with cloudfront and s3 bucket.And I want to show progress bar for user I am using socket io for that.Photo uploading progress will be in loop.but the problem is when photo uploading starts it is always show 100% completion not from the beginning.Don’t think the upload is completed it is not, My file is 20MB .I don’t what’s happening did i miss Something

this is my code

app.post('/posttodb',(req,res) => {
let isLoggedIn = req.cookies['check'];
let token = req.cookies['peace'];
const bucketName="awsBucketName";
console.log(isLoggedIn);
if(isLoggedIn == "true"){
if(token){
jwt.verify(token,JWTPASS,(err,decode) => {
if(err){
console.log(err)
res.json({error:true})
}else{
console.log('========================>',decode)
let postOwneranme = decode.user.username;
let postTags = req.body.data.postTags;
let photosBlob = req.body.data.photos;
let postId = req.body.data.postId;
let nepostAwsPhots = [];
let OwnerPic = decode.user.propic;
let postOwnerFullName = decode.user.name;
let isMature = req.body.data.isMature;
let postThumbUrl = req.body.data.thumnailUrl;
let time = new Date();
let tagSlug = req.body.data.tagSlug;
function savetodb() {
console.log('Inserting all into DB');
r.connect({db:'image'}).then(conn => {
r.table('posts').insert({postId:postId,username:postOwneranme,tag:postTags,postUrlsAndCaptions:nepostAwsPhots,comments:[],postOwnerPic:OwnerPic,likesCount:0,whoLikedIt:[],views:0,postedTime:time,postOwnerFullName:postOwnerFullName,isMature:isMature,thumNailUrl:postThumbUrl,tagSlug:tagSlug}).run(conn).then(response => {
console.log(response)
if(response.inserted > 0){
console.log('Done Bro')
res.json({okva:true,postId:postId,username:postOwneranme})
}else{
res.json({okva:false})
}
})
})

}
function seemsToHaveNetworkProblem() {
res.json({okva:false,message:"Seems To Have Network Problem"})
}
forEachOf(photosBlob,(value,key,callback) => {
console.log(value.id);
let newImageUriWillBe = value.blobData;
let newImageNamewillBe = value.id;
let imageType = value.ImageType;
let caption = value.caption;
console.log(imageType)
let buf = new Buffer(newImageUriWillBe.replace(/^data:image\/\w+;base64,/, ""),'base64');
s3.createBucket({Bucket:bucketName},() => {
let params = {Bucket: bucketName, Key: postOwneranme+'/'+newImageNamewillBe, Body: buf,ContentType:imageType,ContentLength:buf.length,ACL:'public-read'};
s3.upload(params,(err,data) => {
if(err){
callback(err);
}else{
// console.log("Successfully uploaded data to " + bucketName + "/" + id);
// console.log(`https://s3.amazonaws.com/${bucketName}/${username}/${id}`);
let response = {
picUrl:`https://s3.amazonaws.com/${bucketName}/${postOwneranme}/${newImageNamewillBe}`,
cation:caption
}
nepostAwsPhots.push(response);
callback()
// console.log(nepostAwsPhots)
res.writeHead(200, {'content-type': 'text/plain'});
res.end('Ok');

}
})
//Problem Comes here
.on('httpUploadProgress', function(evt) {
let per = Math.round((evt.loaded * 100) / evt.total)
console.log('Progress:',per);
Socket.emit('Scoket',{proccesing:per})

})
});
},(err) => {
if(err){
console.log("From Node Loop error",err);
seemsToHaveNetworkProblem()
}else {
savetodb()
}
})
}
})
}else {
res.json({error:true});
console.log('OMG')
}
}else{
res.json({error:true});
console.log('OMG')
}

});