-1

How would I merge those chunks in order to generate a base64 image that I can consider including those chunks that has the same files_id.

The problem with my current implementation below , the code below , is that there are pairs of chunks that must be joined (chunks with the same files_id) in order to generate a complete base64 but my current code below was not able to consider it that is why some of the base64 are not complete.

How are we going to generate finalFile for each pairs of chunks including those without pairs ?

Current Code implementation

   let fileData = [];
   console.log("Chunks:", chunks)
   for (let i = 0; i < chunks.length; i++) {
     fileData.push(chunks[i].data.toString('base64'));
   }
   //Display the chunks using the data URI format          
   let finalFile = 'data:' + docs[0].contentType + ';base64,' + fileData.join('');

Chunks data

chunks[ 
   [ 
      { 
         _id:5dba69411fe3d41714eafb64,
         files_id:5dba69411fe3d41714eafb63,
         n:0,
         files_id:5dba69411fe3d41714eafb63,
         n:0,
         data:[ 
            Binary
         ]
      }
   ],
   [ 
      { 
         _id:5dba73dcc62fd339acd11759,
         files_id:5dba73dcc62fd339acd11758,
         n:0,
         data:[ 
            Binary
         ]
      },
      { 
         _id:5dba73dec62fd339acd11770,
         files_id:5dba73dcc62fd339acd11758,
         n:1,
         data:[ 
            Binary
         ]
      }
   ],
   [ 
      { 
         _id:5dba73ddc62fd339acd11765,
         files_id:5dba73ddc62fd339acd11764,
         n:0,
         data:[ 
            Binary
         ]
      }
   ]
]
Jhon Caylog
  • 483
  • 8
  • 24

2 Answers2

1

UPDATED

You don't need lodash to achieve this. You might need to change a bit but this should get you started.

let files = [];
for (const i=0; i< chunks.length; i++) {
  let fileData = ""
  for (let j = 0; j < chunks[i].length; j++) {
    // Make sure to import Buffer
    fileData += new Buffer(chunks[i][j].data).toString('base64');
  }

  const finalFile = 'data:' + docs[0].contentType + ';base64,' + fileData;

  files.push(finalFile);
}

Check if docs[0].contentType has a valid value.


You can use lodash. Lodash has a function named groupBy. What it does is, you give a key (files_id), it will group all the data by that key.

Here is an example.

Community
  • 1
  • 1
Shihab
  • 2,641
  • 3
  • 21
  • 29
-1

Chunks that belong to the same file are grouped together in the chunks object, you can see that it's a list of lists. If you iterate over files first, then over chunks, you should get somewhere.

Try something like this

let files = [];
for (const i=0; i<chunks.length; i++) {
  let fileData = ""
  // Collect all chunks of a file
  for (let j = 0; j < chunks[i].length; j++) {
    fileData.push(chunks[i][j].data);
  }

  //Display the chunks using the data URI format          
  const finalFile = 'data:' + docs[0].contentType + ';base64,' + fileData.toString('base64').join('');

  files.push(finalFile);
}

orhanhenrik
  • 1,407
  • 8
  • 11
  • What a great reply that helps me understand why it didn't work for you. Now I can surely give you a much better answer @JhonCaylog. – orhanhenrik Nov 04 '19 at 11:07