0

This is not a question asking how to replace all the broken images with the default images. That question is answered here.

What I want to know is how to replace all the broken images with newly updated images. On my web chat application, users can update their profile pictures and the comments that are posted after the update will have the new pictures shown next to them. However, the new pictures won't be shown next to old comments that are posted before the update.

For example, when you change your profile picture on twitter, not only your profile picture will be updated, but also pictures beside your old tweets will be changed into the new pictures. This is what I want to do.

Does anyone know how to do that with node.js?

tet
  • 1,287
  • 1
  • 17
  • 36
  • Updating all old record may not be the best approach. Try this. Even if the image is updated try to keep the link (hash) same. i.e. if your profile picture was saved as xyz.jpg instead of try to save as user_name.jpg and replace the image itself. – Rajesh Dan Jul 27 '17 at 06:47
  • @tet you should stick around a while after asking your question even if it didn't receive an answer after 5 minutes – Luca Kiebel Jul 27 '17 at 07:16
  • @LucaKiebel Sorry about that. I will try to stick around longer next time. I found an easy solution after posting this question by myself. All I had to do was overwrite the existing image with the new image with the same name. So, when a user changes the profile picture, store the new picture with the same name as before and all the old pictures become new pictures. – tet Jul 27 '17 at 10:04

2 Answers2

0

What @RajeshDan wrote is propably your best bet, use your fs module to update a user's image, while keeping both file-path and -name.

If you have your profile-images in a folder ./public/images/profile/ you can update one with a form, that posts the image and the users unique ID (hopefully that's acutally unique).

fs.writeFile('./public/images/profile/'+userID+'.png', userImage, (err) => {
  if (err) throw err;
  console.log('New image for user with id ' + userID);
});

this also adds the image if there hasn't been one at that point

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
0

Overwrite the old profile picture with the new profile picture with the same name as the old picture.

That way, you don't have to change img src and all the old pictures become new pictures.

tet
  • 1,287
  • 1
  • 17
  • 36