How to convert req.files.resume.data to createReadStream in nodejs - node.js

I am uploading .mp4/mov file using express-uploader, in my api call i received req.files.resume object which is having [data] value like < Buffer 00 98 09 99 88 77 66 ... > of type object.
I have tried so many thing but not able to convert that object to pass into fs.createReadStream(), here's my code
router.post('/update-resume', authUtil.ensureAuthenticated,
function(req, res, next){
if(!req.files){
res.status(400).json({message: "No file"});
} else if(req.files.resume){
const resume = req.files.resume;
if(resume.mimetype === "video/mp4" || resume.mimetype === "video/quicktime"){
// Create the streams
var read =
fs.createReadStream(Buffer.from(resume.data.toString())); //this is giving error of TypeError [ERR_INVALID_ARG_VALUE]
}
}
});
got stuck with this, If anyone can help me!
I am using this code as aws lambda function with api gateway, to upload large file to s3 bucket.

Related

Downloading excel file from Google Cloud storage returns a buffer, how can I return the actual excel file so I can write to it in my handler?

I'm trying to retrieve an excel file from my bucket using the Firebase Admin SDK using the following code:
export const getTemplate = async (fileName: string) => {
const filePath = admin
.storage()
.bucket()
.file('sheets/template.xlsx')
const [file] = await filePath.download()
return file
}
I then pass what's returned from this function, to this function:
export const writeToTemplate = (
objects: any[],
sheetNames: string[],
file
) => {
objects.forEach((dataObject, index) => {
const sheetName = sheetNames[index]
const worksheet = templateFile.Sheets[sheetName]
utils.sheet_add_json(worksheet, dataObject)
})
return file
}
However this throws an error that it cannot read my file. When I console log the file it shows a buffer
<Buffer 50 4b 03 04 14 00 06 00 08 00.........>
How can I actually write to the file? Cheers :)
The solution was to read the Buffer with the read function from the xlsx package.

text2wav MIME type error wasm compile issue?

I am trying to use the text2wav.js node module to convert a string into an audio file. Whenever I try to run the function in the example I get the error:
backend.js:6 wasm streaming compile failed: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
I am using this code inside of a component inside a react application
I am not sure where to Begin as I am not familiar with wasm types or how WebAssembly compiles them
recognize = async () => { //bound to my react class called Body
(async () => {
const text2wav = require('text2wav')
let out = await text2wav('test')
// out is of type Uint8Array
const assert = require('assert')
assert.equal(out[0], 82) //R
assert.equal(out[1], 73) //I
assert.equal(out[2], 70) //F
assert.equal(out[3], 70) //F
})()
};
I am getting this error:
wasm streaming compile failed: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
falling back to ArrayBuffer instantiation
failed to asynchronously prepare wasm: CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 44 4f #+0
By text2wav I assume you mean text2wav.node.js. As the full name suggested, the module only works on node.js. React application runs on browser rather than on node.js, therefore it won't work.

Using multer in middleware and passing the file object to the next

I am using multer function and expect to access the file object in the next middleware. In the first case I succeed and in the second one I fail and I am wondering why
/*The below code works perfectly req.file object is printed successfully */
app.post('/api/v1/tasks/insertproduct', multerUpload.single('productImage'),(req,res,next) => {
console.log(req.file);
});
/*However the following code , I dont have any luck to get the file object printed */
CloudinaryUpload = (req,res,next) => {
console.log('Iam in cloudinary middleware');
console.log('REQ: ' + req.file)
}
app.post('/api/v1/tasks/insertproduct', multerUpload.single('productImage'),CloudinaryUpload);

NodeJS Convert Int16Array binary Buffer to LINEAR16 encoded raw stream for Google Speech API

I'm trying to convert speech to text in node server where speech recording happens in the browser using AudioContext. I'm Able to send int16Array buffer(recorded data) to my node server through a WebSocket connection of binaryType:arraybuffer.
this.processor.onaudioprocess = (e) => {
// this.processAudio(e)
for (
var float32Array = e.inputBuffer.getChannelData(0) || new Float32Array(this.bufferSize),
len = float32Array.length,
int16Array = new Int16Array(len);
len--;)
int16Array[len] = 32767 * Math.min(1, float32Array[len]);
this.socket.send(int16Array.buffer);
};
In server, data is received as
<Buffer 66 6f 6f ...>
Now I would like to parse or convert to a readable stream so that I can pipe to Google speech recognizeStream.
function processAudioBuffer(int16ArrayBuffer) {
console.log("Received stream :", int16ArrayBuffer, typeof
recognizeStreams[userId]);
const recognizer = getGoogleSpeechStreamRecognizer();
if (recognizer) {
/* HERE I NEED SOMETHING WHICH MAKES MY BUFFER COMPATIBLE WITH GOOGLE SPEECH API */
// tried with streamifier but no luck
// streamifier.createReadStream(int16ArrayBuffer).pipe(recognizer);
// also tried with Record which is used in google-cloud-node-samples to record stream from connected mic device, but no luck
var file = new Record({
path: `${userId}.raw`,
encoding: 'arraybuffer',
contents: int16ArrayBuffer
});
file.pipe(recognizer);
} else {
console.log('user stream is not yet created');
}
}
recognizer throws following error:
Error: write after end
at writeAfterEnd (/Users/demo/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:222:12)
at Writable.write (/Users/demo/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:262:20)
at Duplexify.end (/Users/demo/node_modules/duplexify/index.js:223:18)
at Record.pipe (/Users/demo/node_modules/record/index.js:70:14)
at processAudioBuffer (/Users/demo/app.js:87:10)
at WebSocket.incoming (/Users/demo/app.js:104:7)
at emitTwo (events.js:106:13)
at WebSocket.emit (events.js:191:7)
at Receiver._receiver.onmessage (/Users/demo/node_modules/ws/lib/WebSocket.js:146:54)
at Receiver.dataMessage (/Users/demo/node_modules/ws/lib/Receiver.js:380:14)
Solved it !!! We can write the buffer directly to recognizerStream which created from GoogleSpeech as follows:
const recognizer = getGoogleSpeechStreamRecognizer();
recognizer.write(int16ArrayBuffer)

Sending compressed data in from as3 to node.js express

I'm trying to send a compressed string to a node.js express server from Flash AS3. In AS3 I have something like this:
// AS3 Code
function save(data:ByteArray, theURL:String, compress:Boolean):void {
if (compress == true)
{
data.compress();
}
try
{
var request:URLRequest=new URLRequest(theURL);
request.data= data;
request.method=URLRequestMethod.POST;
request.contentType = "application/octet-stream";
var loader:URLLoader = new URLLoader();
loader.load(request);
}
catch (ex)
{
}
}
Testing it like this:
save("This is a test string", "http://example.com/flash/", true);
Node Code
app.post('/flash/', bodyParser.raw({limit: '5mb'}), function(req,res){
console.log(req.body);
zlib.gunzip(req.body, function(err, unzipped_body){
if (!err)
{
console.log(unzipped_body);
}
console.log(err);
});
});
req.body ends up having a byte buffer in it but when I try to zlib.gunzip I get an incorrect header check.
<Buffer 78 da 0b c9 c8 2c 56 00 a2 dc 4a 85 94 c4 92 44 00 2a 56 05 55>
x?♂??,V ??J??ED *V♣U
{ [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }
If I don't do the data.compress() in as3 then req.body in Node shows the original string. What am I not understanding about the zlib compression, or node, or whatever? :)
Turns out it should be zlib.unzip and not zlib.gunzip. I suppose the as3 compress() function isn't "gzip" format but rather "zlib" format?

Resources