Send buffer as real image - node.js

Well I have a buffer inside mongoDB, and I want to send it like an image.
I tried many different things like res.send(buffer) or fs.createReadStream(buffer).pipe(res) and others. I really don't want to write this to real file in the server and then send it.
How can I send the buffer to the client without needing to write it to a file first?

You could encode the buffer/image to base64 or whatever encoding you want and send that.
var encodedBuffer = buffer.toString('base64');
res.send(encodedBuffer);

Related

Is there a way to create a video, and store it as a Buffer in Node.JS without saving it as a file?

I'm in a bit of an odd situation where I have several images (currently stored as base64 strings in buffers), and I'd like to be able to combine them into a video. The catch is that I need this video file in a buffer, and if I have to write files to the disk, and then read them back as a buffer, the program will run too slowly.
I've looked into several other posts on stackoverflow, and libraries that I've found online. However, everything that I've managed to find uses FFMPEG, which would require me to write everything to a file.
For the sake of clarity, what I'm essentially looking for is something that would be able to make the following pseudocode work:
var frames = .... //I have this already
var videoEncoder = new VideoEncoder();
for(var frame in frames)
{
videoEncoder.add(frames[frame]);
}
var buffer = videoEncoder.toBuffer();

nodejs write files which do not open

let dataCer = '0�\u0007\u00060�\u0006��\u0003\u0002\u0001\u0002\u0002\u0010Q��\u0000����K��Z�Q��0\n\u0006\b*�\u0003\u0007\u0001\u0001\u0003\u00020�\u0001l1\u001e0\u001c\u0006\.............'
fs.writeFile('111.cer', dataCer);
let dataPdf = '%PDF-1.4\r\n1 0 obj\r\n<< \r\n/Length 9947\r\n/Filter /FlateDecode\r\n>>\r\nstream\r\nX��]�n#9p}���\u000f���\u0005\b\u0002X��<\'X \u001f�\u001b\u0010 \u0001���H�,6�R�Z�\u0014�N`�\n�T�t�ڼT\u0015���?ԋz��_�{IN_Bz�����O.............'
fs.writeFile('111.pdf', dataPdf);
The data dataCer and dataPdf I get from the application using the GET requests. I can only get this data in this encoding.
And now I need to save them as files.
Also, I will need to then save any data to the file in the same way (zip, rar, png, jpeg, ...).
When i use fs.writeFile, I get files that do not open.
fs.writeFile, can not keep the original state data, ignoring the encoding does not give me the desired result.
Please tell me how to get around this error?
Or which library can save data to any file in node.js, while ignoring the encoding?

Nodejs fs.readfile vs new buffer binary

I have a situation where I receive a base64 encoded image, decode it, then want to use it in some analysis activity.
I can use Buffer to go from base64 to binary but i seem to be unable to use that output as expected (as an image).
The solution now is to convert to binary, write it to a file, then read that file again. The FS output can be used as an image but this approach seems a bit inefficient and additional steps as i would expect the buffer output to also be a usable image as it has the same data?
my question, is how does the fs.readfile output differ from the buffer output? And is there a way i can use the buffer output as i would the fs output?
Buffer from a base64 string:
var bin = new Buffer(base64String, 'base64').toString('binary');
Read a file
var bin = fs.readFileSync('image.jpg');
Many thanks

How to Encode a text using base64_encode() , So that it can't be decoded

I just want to know how to secure a text using base64_encode() , so that it can't be decoded properly from base64_decode().
If you have a text and encode it this way you simply change the byte sequence but it always can be transformed back into its original form; so you cannot stop people from doing it.
You could introduce errors into the base64 encoded form to stop the decode function to work but this is not at all secure.
To stop people from reading the text you could encrypt it and then base64 encode the encrypted form.
Here's one way to make sure your data can never be decoded.
import random
def secureEncode(s):
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/+'
length = int(len(s)*4./3)
return ''.join(random.choice(chars) for _ in xrange(length))
In all seriousness, you seem to be completely missing the point. Base64 is a data format. The whole point is that you can get the data back. If you want to destroy data, just destroy it.
If you want to hide your data in a way that it can only be read if you know a secret, you need to look into encryption.

Zip in j2me using jazzlib

I am using jazzlib package in j2me application to compress the xml file in zip format using ZipOutputStream and the send the compress stream to the server as a string . I am able to do unzip the in mobile using ZipInputStream. But in server i am not able to unzip , i got
EOF exception. when i copy the compressed stream from console and put into browser, the empty space put special character like [] in compressed stream. I didnt understand what happened. Plz help
You send the compressed stream as a String? That's your problem(s) right there:
compressed data is binary data (i.e. byte[]).
String is designed to handle textual (Unicode) data and not arbitrary binary data
converting arbitrary binary data to String is bound to lead to problems
So if you want to handle (send/receive/...) binary data, make sure you never use a String/Reader/Writer to handle the data anywhere in the process. Stay with byte[]/InputStream/OutputStream.

Resources