0

I have a server that's uploading images to the server. I need to optimize the image upload somehow. This is how I'm uploading my files:

uploadImage(file, uid, res) {
    var fs = require('fs');
    mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
    var conn = mongoose.connection;
    Grid.mongo = mongoose.mongo;
    const gfs = Grid(conn.db);
    const writeStream = gfs.createWriteStream({
        filename: file.filename,
    });
    fs.createReadStream(file.path).pipe(writeStream);
    writeStream.on('close', file => {
        const {_id} = file;
        return Account.findByIdAndUpdate(uid, {'employer.logo': _id}).then(() => res.redirect('/employer')).catch(e => console.log(e));
    });
},

How can I optimize the image before the upload? Preferably if there is something gulp-like but for the server?

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119
  • `gulp` is just a way to run JS when certain conditions are met. You're running the JS by calling the `uploadImage` function. What you *do* with the JS is going to be the same. So do whatever you would do in gulp here. – Quentin Nov 21 '18 at 16:03
  • `mongoose.connect()` does not connect synchronously. You shouldn't reference the `connection` object until the promise has resolved. You can either `await` it if you change `uploadImage()` to an `async` function, or you can put the rest of your logic in the `.then()` callback. – Patrick Roberts Nov 21 '18 at 16:08
  • @Quentin So is it possible to run gulp code on a Node server? – Alex Ironside Nov 21 '18 at 16:13
  • @PatrickRoberts Thanks! Fixed it – Alex Ironside Nov 21 '18 at 16:13
  • Gulp calls JavaScript functions. You need to call the same JavaScript functions from Express instead of Gulp. – Quentin Nov 21 '18 at 16:14
  • Ok. What I meant was that some time ago I used a gulp module to optimize images. My question was if I can run the same module in this case. Here's the link https://www.npmjs.com/package/gulp-image-optimization – Alex Ironside Nov 21 '18 at 16:16

1 Answers1

1

I ended up using this https://github.com/imagemin/imagemin. It's pretty straight forward. Here's my code:

async uploadImage(file, uid, res) {
    const imagemin = require('imagemin');
    const imageminJpegtran = require('imagemin-jpegtran');
    const imageminPngquant = require('imagemin-pngquant');
    console.log(1);
    // const newFilePath = `${file.path}optimized`;
    const newFile = await imagemin([file.path], newPath, {
        plugins: [
            imageminJpegtran(),
            imageminPngquant({quality: '65-80'})
        ]
    });
    // newFile.path = newFilePath;
    console.log(2);
    console.log(file);
    console.log(newFile);
    var fs = require('fs');
    await mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
    var conn = mongoose.connection;
    Grid.mongo = mongoose.mongo;
    const gfs = Grid(conn.db);
    const writeStream = gfs.createWriteStream({
        filename: newFile.filename,
    });
    fs.createReadStream(newFile.path).pipe(writeStream);
    writeStream.on('close', file => {
        const {_id} = file;
        return Account.findByIdAndUpdate(uid, {'employer.logo': _id}).then(() => res.redirect('/employer')).catch(e => console.log(e));
    });
},
Alex Ironside
  • 4,658
  • 11
  • 59
  • 119