Advertisement
Guest User

Untitled

a guest
May 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. module.exports = {
  2. userId: null,
  3. imageGroupList: function() {
  4. return {
  5. 'secion': 'secion',
  6. 'user': 'user',
  7. 'admin': 'admin',
  8. 'player': 'player'
  9. }
  10. },
  11. imageGroup: null,
  12. decodeBase64Image: function(dataString, imageGroup, userId) {
  13. this.userId = userId;
  14. this.imageGroup = imageGroup;
  15. if (this.userId == null && this.userId == null) {
  16. return new Error('Invalid input');
  17. }
  18. var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
  19. response = {};
  20.  
  21. if (matches.length !== 3) {
  22. return new Error('Invalid input');
  23. }
  24.  
  25. response.type = matches[1];
  26. response.data = new Buffer(matches[2], 'base64');
  27. var data = this.imageNameData(dataString);
  28. var imageName = 'storage/' + this.imageGroup + '/' + data;
  29. require("fs").writeFile(imageName, response.data, function(err) {
  30. return new Error('Invalid input');
  31. });
  32. // return imageName;
  33. return {
  34. path: imageName,
  35. name: data
  36. };
  37. },
  38.  
  39. imageNameData: function(data) {
  40. var imageName = this.userId + '_' + this.imageGroup + '_' + Math.random();
  41. if (data.indexOf('image/jpeg') > -1) {
  42. return imageName + '.jpeg';
  43. }
  44. if (data.indexOf('image/png') > -1) {
  45. return imageName + '.png';
  46. }
  47. if (data.indexOf('image/gif') > -1) {
  48. return imageName + '.gif';
  49. }
  50. },
  51.  
  52. getImage: function(host, image) {
  53. if (image == null) {
  54. return '';
  55. }
  56. return host + image;
  57. },
  58.  
  59. imagePath: function(containor, name) {
  60. return containor + '/' + name;
  61. }
  62.  
  63.  
  64. }
  65.  
  66.  
  67.  
  68.  
  69. -----------------------
  70.  
  71. use of this lib in main api
  72. -----------------------
  73.  
  74. imageProcessing = require('./node_image_save_from_base_64');
  75. 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