Last active
December 10, 2015 02:45
-
-
Save regissoares/c6b9039bdb6161bf2a26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
var formidable = require('formidable'); | |
var Uploader = require('../helpers/s3-uploader'); | |
router.post('/enviar', function (req, res, next) { | |
var form = new formidable.IncomingForm(); | |
form.parse(req, function(err, fields, files) { | |
if (err) { | |
return next(err); | |
} | |
var uploader = new Uploader(); | |
uploader.upload(files.foto, 600) | |
.then(function (urlDaFoto) { | |
var meuObjeto = new MeuObjeto({ | |
urlDaFoto: urlDaFoto | |
}); | |
meuObjeto.save() | |
.then(function () { | |
res.redirect('/'); | |
}) | |
.catch(function (err) { | |
next(err); | |
}); | |
}) | |
.catch(function (err) { | |
next(err); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var os = require('os'); | |
var AWS = require('aws-sdk'); | |
var im = require('gm').subClass({ imageMagick: true }); | |
var uuid = require('node-uuid'); | |
var Promise = require('bluebird'); | |
var config = require('./config'); | |
function S3Uploader() { | |
this.s3 = new AWS.S3({ | |
accessKeyId: config.aws.accessKeyId, | |
secretAccessKey: config.aws.secretAccessKey | |
}); | |
this.tmpdir = os.tmpdir(); | |
} | |
S3Uploader.prototype.upload = function (file, width, height) { | |
var self = this; | |
return new Promise(function (resolve, reject) { | |
self._resize(file.path, width, height) | |
.then(function (fileName) { | |
var params = { | |
Bucket: config.uploadsBucket, | |
Key: path.join('uploads', path.basename(fileName)), | |
Body: fs.createReadStream(fileName), | |
ContentType: file.type | |
}; | |
self.s3.upload(params, function (err, data) { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(data.Location); | |
} | |
}); | |
}) | |
.catch(function (err) { | |
return Promise.reject(err); | |
}); | |
}); | |
}; | |
S3Uploader.prototype._resize = function (file, width, height) { | |
if (!height) { | |
height = width; | |
} | |
var tempName = this._tempName(file); | |
return new Promise(function (resolve, reject) { | |
im(file).resize(width, height).quality(80).write(tempName, function (err) { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(tempName); | |
} | |
}); | |
}); | |
}; | |
S3Uploader.prototype._tempName = function (file) { | |
return path.join(this.tmpdir, uuid.v4() + path.extname(file)); | |
}; | |
module.exports = S3Uploader; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment