expressjs / expressjs/multer

After uploading an image, it looks strange most of the time, but the same images looks OK sometimes, so I'm not sure what's going on. It started happening from last week and before that it was working fine.

Open
#1,183 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

I'm using following dependencies
multer: ^1.4.2
multer-s3: ^2.10.0
aws-sdk: ^2.1002.0
file-type: ^16.5.3

Here is the code I used to upload files.

import AWS from "aws-sdk";
import multer from "multer";
import multerS3 from "multer-s3";
import { fromBuffer } from 'file-type';
import stream from 'stream';

AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
signatureVersion: "s3v4",
});

const s3 = new AWS.S3();
export const basePath = {
post: "post-documents",
gallery: "gallery-documents"
}
const buckets = {
uploads: process.env.S3_BUCKET_UPLOADS
}
const mimeTypes = {
image: [
"image/gif",
"image/jpg",
"image/jpeg",
"image/png",
"image/tiff",
"image/pipeg",
"image/svg+xml",
"image/bmp",
"image/x-xbitmap",
"image/x-icon"
]
}

const storage = (Bucket: any, uploadBasePath: any) => multerS3({
s3,
bucket: Bucket,
acl: 'public-read',
contentType: function (req: any, file: any, callback: any) {
file.stream.once('data', async function (firstChunk: any) {
var type = await fromBuffer(firstChunk)
var mime = (type === null ? 'application/octet-stream' : type?.mime)
var outStream = new stream.PassThrough()
outStream.write(firstChunk)
file.stream.pipe(outStream)
callback(null, mime, outStream)
})
},
metadata: function (req: any, file: any, callback: any) {
callback(null, { fieldName: file.originalname })
},
key: function (req: any, file: any, callback: any) {
let fileName = ${file.fieldname}-${Date.now()}-${file.originalname};
let filePath = uploadBasePath + '/' + fileName;
callback(null, filePath)
}
})

export const uploader = {
post: multer({
storage: storage(buckets.uploads, basePath.post)
}),
gallery: multer({
storage: storage(buckets.uploads, basePath.gallery)
})
};

Is there something i need to upgrade or change?
Thanks

Here is a sample image that I uploaded, and after uploading, it appears as follows.
https://hub-uploads-stage.s3.us-east-2.amazonaws.com/gallery-documents/file-1675056885128-alphabet-gcf3617eaf_1920.jpg

Original Image
https://i.imgur.com/ChbHNn9.png

Contributor guide

Open the contributing guide

Research direction

Begin with the inline multer-s3 upload configuration and compare the sample S3 object with the original image URL, then check the upload stream and detected MIME type behavior across affected images. Done means identifying a reproducible cause and a narrowly defined change or dependency recommendation; no project test or source file is named in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript, nodejs
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.