I'm currently uploading files in skipper to skipper-s3, but I want to save multiple resized copies of the file. Is there a way to clone the file stream? I found this solution but can't find a way to spawn the file stream. Maybe there is a way that works. Anyways, here's the code of the upload:
processUpload: function (req, res) {
var type = req.param('type');
if (type !== 'thumbnail' && type !== 'image' || typeof req.file('file')._files[0] == 'undefined') return res.badRequest();
var dir = 'news/'+type+'/',
filename = moment().unix() + '_' + req.file('file')._files[0].stream.filename,
filetype = req.file('file')._files[0].stream.headers['content-type']
filesize = req.file('file')._files[0].stream.byteCount;
// Lowercase and convert spaces to hyphens
filename = filename.toLowerCase()
.replace(/ +/g,'-');
if (filetype != 'image/jpeg' && filetype != 'image/png' && filetype != 'image/gif') return res.badRequest('File must be a JPG, GIF, or PNG');
if (filesize > 2000000) return res.badRequest('File cannot exceed 2MB');
var fs = require('fs')
, gm = require('gm');
var resizeStream = req.file('file')._files[0].stream;
var bs = gm(resizeStream, 'img.jpg')
.resize(100,100)
.write('resize.jpg', function (err) { });
req.file('file').upload({
adapter: require('skipper-s3'),
key: 'Axx',
secret: 'xx',
bucket: 'xx6',
saveAs: dir+filename,
headers: {
'x-amz-acl': 'public-read'
}
}, function (err, filesUploaded) {
if (err) return res.negotiate(err);
return res.ok({
trigger: 'article-'+type+'-uploaded',
fileUploaded: filename,
previewURL: 'https://s3.amazonaws.com/sdf/'+dir+filename,
});
});
}
This doesn't work as once I resize and save the image, the .upload functionality has no stream to go off of. If you know of a good way to clone the stream or work around this problem, let me know. Thanks for checking out my question.