Guest User

Untitled

a guest
Feb 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const util = require('util');
  4. const fs = require('fs');
  5. const write = util.promisify(fs.writeFile);
  6.  
  7. module.exports = ({ image, identifier, directory }) => {
  8. try {
  9. const extension = image.split(';')[0].match(/jpeg|png|gif/)[0];
  10. const data = image.replace(/^data:image\/\w+;base64,/, '');
  11. const encoding = 'base64';
  12. const file = `${identifier}.${extension}`;
  13. const path = path.join(directory, file);
  14. await writeFile(path, data, encoding);
  15. return path;
  16. } catch (error){
  17. console.error(error);
  18. return null;
  19. }
  20. }
Add Comment
Please, Sign In to add comment