question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple file fails / corrupted image uploaded

See original GitHub issue

Multer work great for single image, but fails for multiple file uploads.

Problems I am facing with multiple uploads:

  1. Multer is configured to upload maximum of 6 photos, I get only 4 or 5 photos in my folder.
  2. I got corrupted images in my folder

Corrupted Image 1 k5oohdsh

Corrupted Image 2 k5ooytxt

Original Image k5oohhio

Multer Config

const MulterStorage = multer.diskStorage({
  destination: async function (req, file, callback) {
    const uploadPath = `public/images/${req.userId}`;
    callback(null, uploadPath);
  },
  filename: async function (req, file, callback) {
    const fileExt = file.originalname.substring(file.originalname.lastIndexOf('.')).toLowerCase();
    callback(null, `${await RandomUtils.uuid()}${fileExt}`);
  }
});
const uploadProfilePhoto = multer({
  storage: MulterStorage,
  fileFilter: function (req, file, callback) {
    const fileName = file.originalname.toLowerCase();
    if (!fileName.match(/\.(jpg|jpeg|png)$/)) {
      callback(new Error('Only JPG, JPEG & PNG Image Formats are only allowed!'));
    }
    callback(null, true);
  },
  limits: { fileSize: 800 * 800 },
  preservePath: true
}).array('photo', 6);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jonchurchcommented, Jan 22, 2020

I was able to reproduce this with the code you supplied and the original image you supplied.

Unfortunately I think your original image is corrupted. Even when uploading it as a single image, the saved image was corrupted:

image Note: Idk why but github is coercing this image to a png when I upload it

When running the original image through ImageMagick’s identify tool to check if the image has any issues, per this Stack Overflow answer I got this output which seems to suggest the original image is corrupted:

identify: Corrupt JPEG data: premature end of data segment `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Corrupt JPEG data: 1 extraneous bytes before marker 0xc4 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Inconsistent progression sequence for component 0 coefficient 0 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Inconsistent progression sequence for component 1 coefficient 0 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Inconsistent progression sequence for component 2 coefficient 0 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Corrupt JPEG data: bad Huffman code `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Corrupt JPEG data: 5761 extraneous bytes before marker 0xda `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Inconsistent progression sequence for component 0 coefficient 0 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Inconsistent progression sequence for component 1 coefficient 0 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Inconsistent progression sequence for component 2 coefficient 0 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Corrupt JPEG data: bad Huffman code `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Corrupt JPEG data: bad Huffman code `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
identify: Corrupt JPEG data: 8313 extraneous bytes before marker 0xd9 `bad_image.jpg' @ warning/jpeg.c/JPEGWarningHandler/398.
0reactions
ml170722dcommented, Feb 6, 2022

I know this issue is resolved, but I wanted to share my experience with this problem.

I’m not and expert, but from my testing it looks like multer isn’t handling async functions for filename and destination. I have had the same problem with corrupted files, but I was using async functions. When I turned them into regular function there was not problem and everything was working fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Corrupted Image Repair Guide to Recover Damaged Photos ...
Are your photos corrupted? Here's is a guide on why your photos get damaged, how to perform corrupted image repair, and how you...
Read more >
How to Fix Corrupted Files - Lifewire
Corrupt file errors can be unpredictable and can happen when least expected. A corrupt file can be repaired only about half the time....
Read more >
Getting corrupted PDF files on multiple uploads - Jotform
Getting corrupted PDF files on multiple uploads. Profile Image ... I want to continue with Jotform but cannot with these types of failures....
Read more >
How to repair corrupted JPEG file - YouTube
how to repair corrupted jpeg filehow to repair damaged jpg filehow to fix shifted colour color changed imagehow to recover image with grey ......
Read more >
How to Repair Corrupt Photos in Windows 10
Further, there could be a case where the recovered photos may be corrupt. That happens due to file overwriting as you continue using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found