首页 > 解决方案 > MongoDB GridFSBucket 上传流不返回 base64 图像流的长度

问题描述

我正在重构我的团队代码以从 MongoDB中删除弃用警告。

我删除了gridfs-stream库,而是使用GridFSBucket

但是有一个问题:使用 GridFSBucket 上传流不返回 base64 图像的长度,而是返回 0。这破坏了我们代码中的一个测试


这是使用的代码GridFSBucket

function getGrid() {
  return new mongoose.mongo.GridFSBucket(conn.db);
}

module.exports.store = function(id, contentType, metadata, stream, options, callback) {

  ... // unrelated things

  var strMongoId = mongoId.toHexString(); // http://stackoverflow.com/a/27176168
  var opts = {
    contentType: contentType,
    metadata: metadata
  };

  options = options || {};

  if (options.filename) {
    opts.filename = options.filename;
  }

  if (options.chunk_size) {
    var size = parseInt(options.chunk_size, 10);
    if (!isNaN(size) && size > 0 && size < 255) {
      opts.chunkSizeBytes = chunk_size * size;
    }
  }

  var gfs = getGrid();
  var writeStream = gfs.openUploadStreamWithId(strMongoId, opts.filename, opts);

  writeStream.on('finish', function(file) {
    console.log(file) // <== Notice the log for this line below
    return callback(null, file);
  });

  writeStream.on('error', function(err) {
    return callback(err);
  });

  stream.pipe(writeStream);
};

结果console.log(file)是:

{
    "_id": "5ea7a3ffbc7dd36e7df4561e",
    "length": 0, // <== Notice length is 0 here
    "chunkSize": 10240,
    "uploadDate": "2020-04-28T03:33:19.734Z",
    "filename": "default_profile.png",
    "md5": "d41d8cd98f00b204e9800998ecf8427e",
    "contentType": "image/png",
    "metadata": {
        "name": "default_profile.png",
        "creator": {
            "objectType": "user",
            "id": "5ea7a3febc7dd36e7df4560a"
        }
    }
}

这是使用的旧代码gridfs-stream

var Grid = require('gridfs-stream');
...

function getGrid() {
  return new Grid(mongoose.connection.db, mongoose.mongo);
}

module.exports.store = function(id, contentType, metadata, stream, options, callback) {

  ... // unrelated things

  var strMongoId = mongoId.toHexString(); // http://stackoverflow.com/a/27176168
  var opts = {
    _id: strMongoId,
    mode: 'w',
    content_type: contentType
  };

  options = options || {};

  if (options.filename) {
    opts.filename = options.filename;
  }

  if (options.chunk_size) {
    var size = parseInt(options.chunk_size, 10);
    if (!isNaN(size) && size > 0 && size < 255) {
      opts.chunk_size = chunk_size * size;
    }
  }

  opts.metadata = metadata;

  var gfs = getGrid();
  var writeStream = gfs.createWriteStream(opts);

  writeStream.on('close', function(file) {
    console.log(file) // <== Notice the log for this line below
    return callback(null, file);
  });

  writeStream.on('error', function(err) {
    return callback(err);
  });

  stream.pipe(writeStream);
};

这是结果console.log(file)

{
    "_id": "5ea7a43a5d6eea73e0a3c8b1",
    "filename": "default_profile.png",
    "contentType": "image/png",
    "length": 634, // <== We have the length here
    "chunkSize": 10240,
    "uploadDate": "2020-04-28T03:34:18.069Z",
    "metadata": {
        "name": "default_profile.png",
        "creator": {
            "objectType": "user",
            "id": "5ea7a4395d6eea73e0a3c89d"
        }
    },
    "md5": "c37659eb6a9e741656a8d0348765c668"
}

那么如何使用 GridFSBucket 获取长度呢?谢谢!


更新

stream这是两种情况下的变量日志:

{
    "contentType": "image/png",
    "fileName": "default_profile.png",
    "transferEncoding": "base64",
    "contentDisposition": "attachment",
    "generatedFileName": "default_profile.png",
    "contentId": "216d9aab57544410901f8ba7981e63aa@mailparser",
    "stream": {
        "_events": {},
        "_eventsCount": 0,
        "writable": true,
        "checksum": {
            "_handle": {},
            "writable": true,
            "readable": true
        },
        "length": 0,
        "current": ""
    }
}

标签: mongodbgridfsgridfs-stream

解决方案


推荐阅读