You make things a lot harder than they should be. The node buffer object accepts base64 as input and does all this for you.
You can simply delete the data: image ... from the base64 string and pass that data to your WriteFileAssistant.
var strData = this.drawingCanvas.getContext().canvas.toDataURL(); var imgData = strData.replace(/^data:image\/\w+;base64,/, ""); this.call( { filePath:'/media/internal/Collage/image.png', data: imgData }, { method:"writeFile" } );
WriteFileAssistant just has to take a base64 string and pass this as an argument to the Buffer constructor. In addition, having a โ+โ in an openSync call will also break.
var WriteFileAssistant = function(){}; WriteFileAssistant.prototype.run = function(future, subscription) { var fs = IMPORTS.require('fs'); var filePath = this.controller.args.filePath; var fd = fs.openSync('<path>/image.png', 'w'); var buff = new Buffer(this.controller.args.data, 'base64'); fs.write(fd, buff, 0, buff.length, 0, function(err,written){ }); }
The buffer takes a string and an encoding, and then uses the encoding value to process the string into a series of bytes, so when you report that the string is base64, it will decode base64 for you and create a proper decoded array of bytes to write to the file.
loganfsmyth
source share