我正在寻找一种方法来调整图片的大小(由标准的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实例之间的传输,但我不知道如何在将文件发送到云之前调整文件的大小。
有人知道怎么处理吗?
谢谢
发布于 2015-09-01 12:16:15
我找到了一种方法,只需从文件中创建一个流并使用pkgcloud实例对其进行管道:
var fstream = fs.createReadStream(path_to_file);
fstream.pipe(client.upload({
container: 'container-id',
remote: 'remote-name'
});在此代码中缺少:所有成功和错误回调。但这个主意就在这里。
https://stackoverflow.com/questions/32327664
复制相似问题