Last active
December 28, 2015 21:19
-
-
Save adrianoxavier/7563781 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // HTML | |
| // <input type='file' onchange="angular.element(this).scope().setImage(this)" style="width:350px; padding:5px; border:1px solid; border-color:#c3c3c3 #ddd #ddd #c3c3c3;" /> | |
| // JS | |
| $scope.maximumImageSize = 5 * 1024 * 1024; | |
| $scope.validImageExtensions = ['jpg', 'jpeg', 'bmp', 'gif', 'png']; | |
| $scope.setImage = function(input) { | |
| if (input.files && input.files[0] && input.files[0].name) { | |
| var name = input.files[0].name; | |
| var extension = name.substring(name.lastIndexOf('.') + 1); | |
| if (!_.contains($scope.validImageExtensions, extension)) { | |
| var message = i18n.get('pageProposal.imageTypeError', {'name': name, 'valids': $scope.validImageExtensions.join(', ')}); | |
| $timeout(function() { | |
| modalService.showMessage(i18n.get('text.info'), message); | |
| }); | |
| return; | |
| } | |
| $timeout(function() { | |
| $scope.imageLoading = true; | |
| }); | |
| var reader = new FileReader(); | |
| reader.onload = function (event) { | |
| $timeout(function() { | |
| var data = event.target.result | |
| var matches = data.match(/^data:.+\/(.+);base64,(.*)$/); | |
| var ext = matches[1]; | |
| var raw = matches[2]; | |
| var size = (String(raw).length * 3 / 4) - (String(raw).match(/=/g) || []).length; | |
| if (size > $scope.maximumImageSize) { | |
| var message = i18n.get('pageProposal.imageSizeError', {'size': (size / (1024 * 1024)), 'max': ($scope.maximumImageSize / (1024 * 1024))}); | |
| return modalService.showMessage(i18n.get('text.warn'), message); | |
| } | |
| $scope.post.image = data; | |
| $scope.imageLoading = false; | |
| }, 1000); | |
| } | |
| reader.readAsDataURL(input.files[0]); | |
| } | |
| }; |
This file contains hidden or 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 _ = require('underscore'); | |
| module.exports.base = { | |
| 'proposal': { | |
| 'imageTypes': ['jpg', 'jpeg', 'bmp', 'gif', 'png'], | |
| 'imageMaximumSize': 5 * 1024 * 1024, | |
| 'imageThumbnailWidth': 150 | |
| }, | |
| 'blankImage': 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAABFUlEQVR4nO3BMQEAAADCoPVP7WsIoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAeAMBPAABPO1TCQAAAABJRU5ErkJggg==', | |
| 'generateTestUsers': true | |
| }; | |
| module.exports.production = _.extend({}, module.exports.base, { | |
| 'generateTestUsers': false | |
| }); | |
| module.exports.appFog = _.extend({}, module.exports.base, { | |
| 'generateTestUsers': false | |
| }); | |
| module.exports.build = function() { | |
| var config = module.exports.base; | |
| if (process.env.NODE_ENV == 'production') { | |
| return module.exports.production; | |
| } | |
| if (process.env && process.env.VCAP_APP_PORT) { | |
| return module.exports.appFog; | |
| } | |
| return config; | |
| }; | |
| module.exports = module.exports.build(); |
This file contains hidden or 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
| exports.save = function(request, response) { | |
| request.assert('text').notNull().notEmpty(); | |
| request.assert('link').notNull().notEmpty(); | |
| request.assert('image').notNull().notEmpty(); | |
| var errors = request.validationErrors(); | |
| if (errors) { | |
| util.log('Nova proposta: parâmetros inválidos. ' + util.inspect(errors)); | |
| return response.json(500, {'message': 'Parâmetros inválidos'}); | |
| } | |
| var error = exports.validaImage(request.param('image')); | |
| if (error) { | |
| util.log('Nova proposta: imagem inválida. ' + error); | |
| return response.json(500, {'message': error}); | |
| } | |
| var text = request.param('text'); | |
| var link = request.param('miniUrl'); | |
| var image = request.param('image'); | |
| var proposal = { | |
| 'id': utils.uid(7) + '-' + utils.uid(5) + '-' + utils.uid(7), | |
| 'post': { | |
| 'text': text, | |
| 'image': image, | |
| 'link': link | |
| } | |
| }; | |
| util.log(util.format('Gerando miniatura para a proposta de "%s" para "%s".', email, id)); | |
| utils.generateThumnail(image, 200, function(err, thumbnail) { | |
| proposal.post.thumbnail = thumbnail; | |
| // salva proposal no BD | |
| }); | |
| }; | |
| exports.validateImage = function(image) { | |
| if (!image) { | |
| return null; | |
| } | |
| var matches = image.match(/^data:.+\/(.+);base64,(.*)$/); | |
| var type = matches[1]; | |
| var data = matches[2]; | |
| var size = (String(data).length * 3 / 4) - (String(data).match(/=/g) || []).length; | |
| if (size > config.proposal.imageMaximumSize) { | |
| return util.format('Tamanho máximo da imagem é %sMB, a imagem tem %sMB.', (config.proposal.imageMaximumSize / (1024 * 1024)), (size / (1024 * 1024))); | |
| } | |
| if (!_.contains(config.proposal.imageTypes, type)) { | |
| return util.format('Imagem inválida. Tipo %s. Tipos válidos %s.', type, config.proposal.imageTypes.join(', ')); | |
| } | |
| return null; | |
| }; |
This file contains hidden or 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
| exports.image = function(request, response) { | |
| request.assert('id').notNull(); | |
| var errors = request.validationErrors(); | |
| if (errors) { | |
| util.log('Imagem: parâmetros inválidos. ' + util.inspect(errors)); | |
| return response.json(500, {'message': 'Parâmetros inválidos'}); | |
| } | |
| var id = request.param('id'); | |
| repository.findOneProto('proposal', {'id': id}, function(err, proposal) { | |
| var imageInBase64 = config.blankImage; | |
| if (!err && proposal && proposal.post && proposal.post.image) { | |
| imageInBase64 = proposal.post.image; | |
| } | |
| var matches = imageInBase64.match(/^data:.+\/(.+);base64,(.*)$/); | |
| var ext = matches[1]; | |
| var data = matches[2]; | |
| var imageBuffer = new Buffer(data, 'base64'); | |
| response.setHeader('Content-Type', 'image/png'); | |
| response.setHeader('Content-Length', imageBuffer.length); | |
| response.end(imageBuffer, 'binary'); | |
| }); | |
| }; |
This file contains hidden or 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 crypto = require('crypto'), | |
| im = require('imagemagick'); | |
| https.globalAgent.options.secureProtocol = 'SSLv3_method'; | |
| https.globalAgent.options.rejectUnauthorized = false; | |
| exports.generateThumnail = function(image, size, callback) { | |
| if (!image) { | |
| return callback(null, null); | |
| } | |
| var matches = image.match(/^data:.+\/(.+);base64,(.*)$/); | |
| var data = matches[2]; | |
| im.resize({ | |
| srcData: new Buffer(data, 'base64').toString('binary'), | |
| width: size | |
| }, function(err, stdout, stderr) { | |
| if (err) { | |
| return callback(err); | |
| } | |
| callback(null, 'data:image/png;base64,' + new Buffer(stdout, 'binary').toString('base64')) | |
| }); | |
| }; | |
| exports.md5 = function(text) { | |
| return exports.digest(text, 'md5', 'hex'); | |
| }; | |
| exports.sha1 = function(text) { | |
| return exports.digest(text, 'sha1', 'hex'); | |
| }; | |
| exports.digest = function(text, hash, mode) { | |
| return crypto.createHash(hash).update(text).digest(mode); | |
| }; | |
| exports.uid = function(size) { | |
| return crypto.randomBytes(parseInt(size/2)).toString('hex'); | |
| }; | |
| exports.gravatarHash = function(email) { | |
| return exports.md5(String(email).trim().toLowerCase()); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment