Search code examples
javascriptnode.jsfileexpressbinaryfiles

Update binary files in Node.js


I'm trying to work with binary files using Node.js.

I'm trying to receive a binary file from the client, open the binary file, convert to hexadecimal, replace data and return the new binary file to the client.

app.use('/read-binary-file',(req,res) => {
    try {
        let content = fs.readFileSync(file_path);
        console.log('content', content)
        res.status(200).send({content})
    }
    catch(err) {
        console.error(err);
    }
})

I wrote code that takes an existing file and try to read it. When I print it, I get this in the buffer:

content <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >

I'm not sure how to convert it to hexadecimal and then try to change it... When I read the file via online hex editor, I get them in 16 bit each line and this way is very comfortable.

Enter image description here

I have some questions:

  1. How can convert from binary to hexadecimal?
  2. How can I replace data in a hexadecimal file and then return to the client?
  3. How can I display them in code as 16 bit?
  4. What is the best way to store them in a database? To store the file and then in the database store only the path?

Is there any documentation that can help?


Solution

    1. Convert from binary to hexadecimal:

      const file = fs.readFileSync("./test");
      const str = file.toString("hex");
      
    2. Replace data and return to the client:

      let newStr = str.replace( "00", "FF" );
      let buffer = Buffer.from( newStr, "hex" );
      
    3. Display in 16 bits:

      for ( let i = 0; i < newStr.length; i+=16 ){
        console.log( newStr.slice( i, i+16 ) + "\n" );
      }
      
    4. Store the file in a writable upload folder, and store the URL path to the uploaded in the database. That's my suggestion, from my experience. You can read more about whether you want to choose to store the images in the database (in BLOB format) in this Quora post: Is it a bad design to store images as blobs in a database?


    Here's a basic setup that might help you:

    /test

    ABC
    

    /app.js

    const express = require('express');
    const app = express();
    const fs = require('fs');
    
    app.use("/test", (req, res) => {
    
        const file = fs.readFileSync("./test");     // Read file as binary
        const str = file.toString("hex");           // Convert to hexadecimal
        let newStr = str.replace(/41|43/g, "42");   // Replace hexadecimal characters
        let buffer = Buffer.from(newStr, "hex");    // Create buffer from hexadecimal
    
        // Send to the user as download
        res.setHeader('Content-disposition', 'attachment; filename=test-edited.txt');
        res.setHeader('Content-type', 'text/plain');
        res.charset = 'UTF-8';
        res.write(buffer);
        res.end();
    });
    
    app.listen(3000, () => console.log('Server Running...'));
    

    The test file contains the characters ABC. They are transformed to BBB and then downloaded.

    You can choose to output a different file type, by setting the appropriate filename and MIME type (Content-Type), e.g. for downloading a PNG image:

        res.setHeader('Content-disposition', 'attachment; filename=output.png');
        res.setHeader('Content-type', 'image/png');
    

    Note: for direct binary manipulation, without an intermediary hexadecimal conversion, see Christos Lytras' answer.