Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = {
- userId: null,
- imageGroupList: function() {
- return {
- 'secion': 'secion',
- 'user': 'user',
- 'admin': 'admin',
- 'player': 'player'
- }
- },
- imageGroup: null,
- decodeBase64Image: function(dataString, imageGroup, userId) {
- this.userId = userId;
- this.imageGroup = imageGroup;
- if (this.userId == null && this.userId == null) {
- return new Error('Invalid input');
- }
- var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
- response = {};
- if (matches.length !== 3) {
- return new Error('Invalid input');
- }
- response.type = matches[1];
- response.data = new Buffer(matches[2], 'base64');
- var data = this.imageNameData(dataString);
- var imageName = 'storage/' + this.imageGroup + '/' + data;
- require("fs").writeFile(imageName, response.data, function(err) {
- return new Error('Invalid input');
- });
- // return imageName;
- return {
- path: imageName,
- name: data
- };
- },
- imageNameData: function(data) {
- var imageName = this.userId + '_' + this.imageGroup + '_' + Math.random();
- if (data.indexOf('image/jpeg') > -1) {
- return imageName + '.jpeg';
- }
- if (data.indexOf('image/png') > -1) {
- return imageName + '.png';
- }
- if (data.indexOf('image/gif') > -1) {
- return imageName + '.gif';
- }
- },
- getImage: function(host, image) {
- if (image == null) {
- return '';
- }
- return host + image;
- },
- imagePath: function(containor, name) {
- return containor + '/' + name;
- }
- }
- -----------------------
- use of this lib in main api
- -----------------------
- imageProcessing = require('./node_image_save_from_base_64');
- imageProcessing.decodeBase64Image(BASE_64_OF_IMAGE, GROUP_NAMELIKE_SECTION, USER_ID_TO_CHECK_AND_IMAGE_NAME_CREATION);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement