|
|
|
@ -2,8 +2,7 @@ |
|
|
|
|
|
|
|
|
|
const fs = require('fs'); |
|
|
|
|
const path = require('path'); |
|
|
|
|
const streamifier = require('streamifier'); |
|
|
|
|
|
|
|
|
|
const Readable = require('stream').Readable; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns true if argument is function. |
|
|
|
@ -50,7 +49,7 @@ const copyFile = function(src, dst, callback){ |
|
|
|
|
runCb(err); |
|
|
|
|
}); |
|
|
|
|
writable.on('close', () => runCb()); |
|
|
|
|
//Copieng file via piping streams.
|
|
|
|
|
//Copy file via piping streams.
|
|
|
|
|
readable.pipe(writable); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -64,10 +63,19 @@ const saveBufferToFile = function(buffer, filePath, callback){ |
|
|
|
|
callback(new Error('buffer variable should be a Buffer!')); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const fstream = fs.createWriteStream(filePath); |
|
|
|
|
streamifier.createReadStream(buffer).pipe(fstream); |
|
|
|
|
//Setup readable stream from buffer.
|
|
|
|
|
let streamData = buffer; |
|
|
|
|
let readStream = Readable(); |
|
|
|
|
readStream._read = ()=>{ |
|
|
|
|
readStream.push(streamData); |
|
|
|
|
streamData = null; |
|
|
|
|
}; |
|
|
|
|
//Setup file system writable stream.
|
|
|
|
|
let fstream = fs.createWriteStream(filePath); |
|
|
|
|
fstream.on('error', error => callback(error)); |
|
|
|
|
fstream.on('close', () => callback()); |
|
|
|
|
//Copy file via piping streams.
|
|
|
|
|
readStream.pipe(fstream); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|