I'm running a small Node.js express application that is using multer-s3-transform and sharp packages along with simple-thumbnail package to upload images and videos to to my S3 bucket. My code works perfectly for images, the image is passed, sharp resizes it, then continues to upload it to my bucket. However, I am trying to implement uploading a videos thumbnail to my s3 bucket, using simple-thumbnail, but I am unsure how to go about passing the stream to the genThumbnail function of this package. In the documentation it states you can pass streams to and from it, would like to know how to do this.
Packages of concern:
https://github.com/gmenih341/multer-s3
https://github.com/ScottyFillups/simple-thumbnail
const express = require('express');
const app = express();
const aws = require('aws-sdk');
const multerS3 = require('multer-s3');
const sharp = require('sharp');
const ffmpeg = require('ffmpeg-static');
const genThumbnail = require('simple-thumbnail');
let s3 = new aws.S3({});
let upload = multer({
storage: multerS3({
s3: s3,
bucket: MY_BUCKET,
acl: 'public-read',
cacheControl: 'max-age=31536000',
shouldTransform: true,
contentType: multerS3.AUTO_CONTENT_TYPE,
transforms: [{
id: 'original',
key: function (req, file, cb) {
let fileName = Date.now().toString();
cb(null, fileName + '-original')
},
transform: function (req, file, cb) {
if (file.mimetype == 'video/mp4') {
//HERE IS THE PROBLEM, first two arguments of genThumbnail can take in a stream.Readable, and stream.Writable
cb(null, genThumbnail(null, null, '150x100', { path: ffmpeg.path }))
} else {
//THIS WORKS (WHEN NOT VIDEO)
cb(null, sharp().jpeg())
}
}
}]
})
});
app.post('/upload', upload.array('theFile'), (req, res) => {
//response
});
I would like genThumbnail() to create a thumbnail and continue passing along the stream of the file like in the working case of images with sharp().