Combine multer and tinypng API in node

独自空忆成欢 提交于 2020-01-02 17:50:13

问题


does anyone know how to use tinyPNG's API with multer? The docs seem deceptively simple:

var source = tinify.fromFile("unoptimized.jpg");
source.toFile("optimized.jpg");

though there's no clear indication of where this is meant to go, especially in something as convoluted as this:

var storage =  multer.diskStorage(
    {
      destination: function (req, file, callback) {
        callback(null, './uploads');
      },

      filename: function (req, file, callback) {
        //use date to guarantee name uniqueness
        callback(null, file.originalname + '-' + Date.now());
      }
    }
);

//.any() allows multiple file uploads
var upload = multer({ storage : storage}).any()

app.post('/api/photo', function(req,res){

    upload(req,res,function(err) {

        if(err) {
            return res.end("Error uploading file.");
        }

        res.end("File is uploaded");
    });
});

Where am I meant to "intercept" the file uploaded by multer so that I can compress it with tinyPNG?

Thanks in advance for the help!


回答1:


Use following basic sample that changes uploaded photo/gallery files:

// Import express and multer.
var express = require('express');
var multer  = require('multer');

// Setup upload.
var upload = multer({ dest: 'uploads/' });
var multipleFiles = upload.fields([{ name: 'photo', maxCount: 1 }, 
                                   { name: 'gallery', maxCount: 8 }]);

// Setup tinify.
var tinify = require("tinify");
tinify.key = "YOUR_API_KEY";

// Get request handler for '/' path.
var app = express();
app.get('/', function (req, res) {
    res.setHeader("Content-Type", "text/html");
    res.end(
        "<form action='/api/photo' method='post' enctype='multipart/form-data'>" +
            "<input type='file' name='photo' />" +
            "<input type='file' name='gallery' multiple/>" +
            "<input type='submit' />" +
        "</form>"
    );
});

// Upload file handler with '/api/photo' path.
app.post('/api/photo', multipleFiles, function (req, res) {
    req.files['gallery'].forEach(function(file) {
       // Your logic with tinify here.
       var source = tinify.fromFile(file.path);
       source.toFile(file.path + "_optimized.jpg");
    });

    res.end("UPLOAD COMPLETED!");
});

Feel free to change express middleware how you need it, just make sure you use upload.fields and authenticate using tinify.key = "YOUR_API_KEY";

  • https://github.com/expressjs/multer
  • https://tinypng.com/developers/reference/nodejs#compressing-images


来源:https://stackoverflow.com/questions/37633991/combine-multer-and-tinypng-api-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!