Wednesday, 31 May 2017

Check if Buffer is Zipped File

A system I am using will only take base64 encoded files. Currently I have a script running which is given a URL. This URL is a download link to a file. The script gets the file and base64 encodes it as another service I am using requires files to be base64 encoded.

The service that passes me the URL in some cases will zip up multiple files if there is more than one file present. The script I am trying to write needs to be able to do the following:

  1. Determine if the download URL is a zipped file or not.
  2. If the download URL contains a zipped folder - the script needs to open the zipped folder in memory and loop through the files. Any files smaller than 2kb need to be removed before the zipped file is subsequently base64 encoded.
  3. If the download URL is a single file (not a zipped folder) - the script can run as is shown below.

Example zipped file URL (Puppy Pictures).

Example single file URL (Puppy Picture).

I believe I need to first determine if the buffered data from the link is a "zip" folder (this service zips all files with a ".zip" extension). NPM file-type library seems appropriate for this. From there is where I get lost - if the buffer is a zipped folder - I need to iterate through the files and remove files based on their file size. Once completed I need to base64 the zipped files once more. Any ideas or guidance would be much appreciated.

Current code which grabs the file/zipped files from the given URL and base64 encodes them. This code currently works to accomplish that:

var fileURL = (one of two options outlined above);

https.get(fileURL, function(response) {
  var data = [];


  response.on('data', function(chunk) {
      data.push(chunk);
  }).on('end', function() {
      //build the base64 endoded file
      var buffer = Buffer.concat(data).toString('base64');
      //data to return
      var returnData = {
          base64File: buffer
      };
  });
});



via Jim M

No comments:

Post a Comment