Как преобразовать поток base64 в строку и записать в другой файл? - PullRequest
0 голосов
/ 11 марта 2020

Я получаю поток сообщений из тела письма, и этот поток находится в формате base64. Я хочу декодировать поток. Я использую модуль 'base64-stream', который является npm модулем для декодирования потока, но я получаю ошибку.

Ошибка типа: base64.decode не является функцией

   function buildAttMessageFunction(attachment) {
  var filename = attachment.params.name;
  var encoding = attachment.encoding;

  return function (msg, seqno) {
    var prefix = '(#' + seqno + ') ';
    msg.on('body', function(stream, info) {
      //Create a write stream so that we can stream the attachment to file;
      console.log(prefix + 'Streaming this attachment to file', filename, info);
      var writeStream = fs.createWriteStream(filename);
      writeStream.on('finish', function() {
        console.log(prefix + 'Done writing to file %s', filename);
      });

      //stream.pipe(writeStream); this would write base64 data to the file.
      //so we decode during streaming using 
      if (toUpper(encoding) === 'BASE64') {
        //the stream is base64 encoded, so here the stream is decode on the fly and piped to the write stream (file)
        stream.pipe(base64.decode()).pipe(writeStream);
      } else  {
        //here we have none or some other decoding streamed directly to the file which renders it useless probably
        stream.pipe(writeStream);
      }
    });
    msg.once('end', function() {
      console.log(prefix + 'Finished attachment %s', filename);
    });
  };
}

1 Ответ

0 голосов
/ 11 марта 2020

Полагаю, вам нужно использовать этот способ

const {Base64Decode} = require("base64-stream");
stream.pipe(new Base64Decode()).pipe(writeStream);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...