【问题标题】:NodeJS + Multer + PkgCloud: resize image before sending it to cloudNodeJS + Multer + PkgCloud:在将图像发送到云端之前调整图像大小
【发布时间】:2015-09-01 20:16:15
【问题描述】:

我正在寻找一种在将图像发送到云(openstack、s3 或其他)之前调整图像大小的方法(通过标准 html 表单提交上传)。

我正在使用 NodeJS 和 3 个插件:Multer(上传管理)、lwip(图像调整大小)和 PkgCloud(传输到云端)。

我可以用 multer 处理上传,我可以用 lwip 在服务器中写入一个新的调整大小的文件。

var fs = require('fs'),
    multer  = require('multer'),
    upload = multer({ dest: 'uploads/medias/' }),
    pkgcloud = require('pkgcloud'),
    client = pkgcloud.storage.createClient({/* some config */});

// "router" is an instance of a route from Express
router.post('/media/:id/image', upload.single('file'), function (req) {
    var lwip = require('lwip'),
        type = 'png',
        npath = req.file.path + '_150x150';

    // the file exists and the log shows information
    console.log(req.file);

    // open image
    lwip.open(req.file.path, type, function (err, image) {
        image.batch()
            .resize(150, 150)
            .writeFile(npath, type, function () {
                // remove raw file
                fs.unlink(req.file.path, function () {
                    // how do I send the new resized file to the pkgcloud client?
                });
            });
    });
});

但是我遗漏了一些东西,我找不到将这个新文件发送到云端的方法。我发现了一个插件,它管理 multer 和配置的 pkgcloud 实例 (multer-storage-pkgcloud) 之间的传输,但我不知道如何在将文件发送到云端之前调整文件大小。

有人知道如何处理吗?

谢谢

【问题讨论】:

    标签: node.js multer pkgcloud


    【解决方案1】:

    我找到了一种方法,只需从文件中创建一个流并使用 pkgcloud 实例对其进行管道传输:

    var fstream = fs.createReadStream(path_to_file);
    fstream.pipe(client.upload({
        container: 'container-id',
        remote: 'remote-name'
    });
    

    此代码中缺少:所有成功和错误回调。但想法就在这里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 2019-04-04
      • 2018-05-07
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多