我正在寻找一种方法来调整图像大小(通过标准html表单提交上传),然后再将其发送到云端(openstack,s3或其他)。
我使用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)之间的转换,但我无法弄清楚如何在将文件发送到云之前调整文件大小。
有没有人知道如何处理?
由于
答案 0 :(得分:0)
我找到了一种方法,只需从文件创建一个流并用pkgcloud实例管道:
var fstream = fs.createReadStream(path_to_file);
fstream.pipe(client.upload({
container: 'container-id',
remote: 'remote-name'
});
此代码中缺少:所有成功和错误回调。但这个想法就在这里。